Försöker konvertera text till morse i C++ (nybörjare)
Hej. Har i uppgift att konvertera text från en txt-fil till morse i en ny 'output'-textfil. Uppgiften säger att koden ska ta statistik från input fil till output fil vilket jag har lyckats med. Men har problem med hela morse delen. Felet ligger vid min switch statement enligt visual studio. VS säger även att alla mina case-conditions är illegal. Har endast hållt på med C++ i ca 2-3 veckor.
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *filename; //opening the filename provided by the user later on in the code. Opened in read-mode.
FILE *output = fopen("statistics.txt", "w"); //Created a output file and opened it in write mode. Named it 'statistics.txt'
char name[800];
char stream;
int words, chars, lines;
char statistics[400];
char morse[1000];
int count;
count = 1000;
words = 0;
lines = 0; // we initialize the variables with 0 since an empty txt-file would have the value 0 of the statistics
chars = 0;
printf("Enter the full pathname of your file, don't forget the file-extension name :");// Prompt user to enter filename, note that when the code is being ran within visual basic (i.e debugging)...
//... the full pathname and filename must be given. However, if program is ran from built executible the code will look for filename in the same directory as executible unless other path given.
//i.e if a txt file was placed on the desktop the full pathname would be C:\Users\USERNAME\Desktop\NAMEOFTEXTFILE.txt
gets_s(name); //here we use gets_s to take the input of the user in the command window and putting it in to the character array 'name'
filename = fopen(name, "r"); //opening the filename provided by the user earlier on in the code. Opened in read-mode.
if (filename)
{
while ((stream = getc(filename)) != EOF) //This while loop keeps adding each character of the stream/text file until it reaches the end (EOF = end of file)
{
if (stream != ' ' && stream != '\n')
{
++chars;
}//using if-commands to add +1 to each variable. In this if-command I used &&. What it does is it checks if BOTH statements are true and thereafter does action.
//so in this case if there is no space or line it will add +1 to 'chars'.
if (stream == ' ' || stream == '\n')
{
++words;
}// Here I used almost the same if-statements as before. However EITHER OF the statements could be true for it to add +1 to 'words'
if (stream == '\n')
{
++lines;
} // adding +1 to integer 'lines' if a new line is found
}
if (chars > 0) {
++lines;
++words;
}
}
else
{
printf("Could not find file\n"); //if it cant find the file the program will state so
}
fclose(filename); //we close the user created file
fprintf(output, "Number of lines is : %d \n", lines); //printing statistics to the output file
fprintf(output, "Number of words is : %d \n", words);
fprintf(output, "Number of characters is : %d \n", chars);
for (int i = 0; i < count; i++)
{
switch (char stream[])
{
case 'a':
case 'A':
{
fprintf(output, ".- ");
break;
}
case 'b':
case 'B':
{
fprintf(output, "-... ");
break;
}
case 'c':
case 'C':
{
fprintf(output, "-.-. ");
break;
}
case 'd':
case 'D':
{
fprintf(output, "-.. ");
break;
}
case 'e':
case 'E':
{
fprintf(output, ". ");
break;
}
case 'f':
case 'F':
{
fprintf(output, "..-. ");
break;
}
case 'g':
case 'G':
{
fprintf(output, "--. ");
break;
}
case 'h':
case 'H':
{
fprintf(output, ".... ");
break;
}
case 'i':
case 'I':
{
fprintf(output, ".. ");
break;
}
case 'j':
case 'J':
{
fprintf(output, ".--- ");
break;
}
case 'k':
case 'K':
{
fprintf(output, "-.- ");
break;
}
case 'l':
case 'L':
{
fprintf(output, ".-.. ");
break;
}
case 'm':
case 'M':
{
fprintf(output, "-- ");
break;
}
case 'n':
case 'N':
{
fprintf(output, "-. ");
break;
}
case 'o':
case 'O':
{
fprintf(output, "--- ");
break;
}
case 'p':
case 'P':
{
fprintf(output, ".--. ");
break;
}
case 'q':
case 'Q':
{
fprintf(output, "--.- ");
break;
}
case 'r':
case 'R':
{
fprintf(output, ".-. ");
break;
}
case 's':
case 'S':
{
fprintf(output, "... ");
break;
}
case 't':
case 'T':
{
fprintf(output, "- ");
break;
}
case 'u':
case 'U':
{
fprintf(output, "..- ");
break;
}
case 'v':
case 'V':
{
fprintf(output, "...- ");
break;
}
case 'w':
case 'W':
{
fprintf(output, ".-- ");
break;
}
case 'x':
case 'X':
{
fprintf(output, "-..- ");
break;
}
case 'y':
case 'Y':
{
fprintf(output, "-.-- ");
break;
}
case 'z':
case 'Z':
{
fprintf(output, "--.. ");
break;
}
case ' ':
{
fprintf(output, " / ");
break;
}
case '0':
{
fprintf(output, "----- ");
break;
}
case '1':
{
fprintf(output, ".---- ");
break;
}
case '2':
{
fprintf(output, "..--- ");
break;
}
case '3':
{
fprintf(output, "...-- ");
break;
}
case '4':
{
fprintf(output, "....- ");
break;
}
case '5':
{
fprintf(output, "..... ");
break;
}
case '6':
{
fprintf(output, "-.... ");
break;
}
case '7':
{
fprintf(output, "--... ");
break;
}
case '8':
{
fprintf(output, "---.. ");
break;
}
case '9':
{
fprintf(output, "----. ");
break;
}
}
}
fclose(output); //closing the output file
printf("Number of lines : %d \n", lines); //printing the statistics inside the command window
printf("Number of words : %d \n", words);
printf("Number of characters is : %d \n", chars);
_getch();
return(0);
}
Dator: CPU: Intel Core i5 8600k @4.3GHz | Kylare: Noctua NH-L12 | Moderkort: Gigabyte Z370n WiFi | GPU: EVGA GTX 1080ti FTW3 | RAM: Corsair LPX 16GB 3000MHz | Chassi: Louqe Ghost S1 | Lagring: SAMSUNG 960 Evo m.2 512GB | PSU: Corsair sf600