Alla fyndtips från mellandagsrean

Försöker konvertera text till morse i C++ (nybörjare)

Permalänk
Medlem

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); }

Dold text
Visa signatur

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

Permalänk
Medlem

@breakwin: Du menade nog att skriva

switch (stream[i])

och inte

switch (char stream[])

Ett tips för att korta ner koden är att använda en uppslagstabell istället, d.v.s. lagra alla morsetecken i en array och använd de inlästa tecknen som index för att slå upp rätt tecken istället för en stor switch-sats. Men det kanske är lite överkurs.

Din kod är för övrigt C och inte C++

Permalänk
Medlem
Skrivet av perost:

@breakwin: Du menade nog att skriva

switch (stream[i])

och inte

switch (char stream[])

Ett tips för att korta ner koden är att använda en uppslagstabell istället, d.v.s. lagra alla morsetecken i en array och använd de inlästa tecknen som index för att slå upp rätt tecken istället för en stor switch-sats. Men det kanske är lite överkurs.

Din kod är för övrigt C och inte C++

tack för svar men använde en annan metod. använde if-condition istället. Och det funkar galant. Nu försöker jag bara ta reda på hur jag ska göra mitt sista steg. Programmet skall fråga användaren vad den skapade filen ska heta istället för "statistics.txt". Är fast....

kolla vid toppen. Istället för att ta user-input vid "char chosen[100]" så skapar den bara en fil som heter "%s"
kolla vid "FILE *output = fopen("%s", "w");". varför tar den inte string i char chosen?

extremt svårt för mig att förklara på svenska då jag studerar allt på engelska haha, sorry.

#include <stdio.h> #include <conio.h> int main() { FILE *filename; char chosen[100]; printf("write output name of file"); gets_s(chosen); FILE *output = fopen("%s", "w"); //Created a output file and opened it in write mode. Named it 'statistics.txt' char name[800]; char stream; int words, chars, lines; 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 input 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 (stream == 'a' || stream == 'A') { fprintf(output, ".- "); printf(".- "); } if (stream == 'b' || stream == 'B') { fprintf(output, "-... "); printf("-... "); } if (stream == 'c' || stream == 'C' ) { fprintf(output, "-.-. "); printf("-.-. "); } if (stream == 'd' || stream == 'D') { fprintf(output, "-.. "); printf("-.. "); } if (stream == 'e' || stream == 'E') { fprintf(output, ". "); printf(". "); } if (stream == 'f' || stream == 'F' ) { fprintf(output, "..-. "); printf("..-. "); } if (stream == 'g' || stream == 'G' ) { fprintf(output, "--. "); printf("--. "); } if (stream == 'h' || stream == 'H') { fprintf(output, ".... "); printf(".... "); } if (stream == 'i' || stream == 'I') { fprintf(output, ".. "); printf(".. "); } if (stream == 'j' || stream == 'J') { fprintf(output, ".--- "); printf(".--- "); } if (stream == 'k' || stream == 'K') { fprintf(output, "-.- "); printf("-.- "); } if (stream == 'l' || stream == 'L') { fprintf(output, ".-.. "); printf(".-.. "); } if (stream == 'm' || stream == 'M') { fprintf(output, "-- "); printf("-- "); } if (stream == 'n' || stream == 'N') { fprintf(output, "-. "); printf("-. "); } if (stream == 'o' || stream == 'O') { fprintf(output, "--- "); printf("--- "); } if (stream == 'p' || stream == 'P' ) { fprintf(output, ".--. "); printf(".--. "); } if (stream == 'q' || stream == 'Q') { fprintf(output, "--.- "); printf("--.- "); } if (stream == 'r' || stream == 'R') { fprintf(output, ".-. "); printf(".-. "); } if (stream == 's' || stream == 'S') { fprintf(output, "... "); printf("... "); } if (stream == 't' || stream == 'T') { fprintf(output, "- "); printf("- "); } if (stream == 'u' || stream == 'U') { fprintf(output, "..- "); printf("..- "); } if (stream == 'v' || stream == 'V') { fprintf(output, "...- "); printf("...- "); } if (stream == 'w' || stream == 'W') { fprintf(output, ".-- "); printf(".-- "); } if (stream == 'x' || stream == 'X') { fprintf(output, "-..- "); printf("-..- "); } if (stream == 'y' || stream == 'Y') { fprintf(output, "-.-- "); printf("-.-- "); } if (stream == 'z' || stream == 'Z') { fprintf(output, "--.. "); printf("--.. "); } if (stream == '1') { fprintf(output, ".---- "); printf(".---- "); } if (stream == '2') { fprintf(output, "..--- "); printf("..--- "); } if (stream == '3') { fprintf(output, "...-- "); printf("...-- "); } if (stream == '4') { fprintf(output, "....- "); printf("....- "); } if (stream == '5') { fprintf(output, "..... "); printf("..... "); } if (stream == '6') { fprintf(output, "-.... "); printf("-.... "); } if (stream == '7') { fprintf(output, "--... "); printf("--... "); } if (stream == '8') { fprintf(output, "---.. "); printf("---.. "); } if (stream == '9') { fprintf(output, "----. "); printf("----. "); } if (stream == '0') { fprintf(output, "----- "); printf("----- "); } } 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, " \nNumber 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); fclose(output); //closing the output file printf("\nNumber 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); printf("Morse code and statistics can be found in 'statistics.txt' "); _getch(); return(0); }

Dold text
Visa signatur

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

Permalänk
Medlem

nvm. löste det

FILE *filename; char chosen[100]; printf("write output name of file"); gets_s(chosen); FILE *output = fopen(("%s" ".txt", chosen), "w"); //Created a output file and opened it in write mode. Named it 'statistics.txt'

Nytt problem, skapade filen nämns bara till det användaren anger. ".txt" kommer ej med

Visa signatur

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

Permalänk

Jag förstår vad du vill göra, men tyvärr gör inte komilatorn det. Jag är lite förvånad över att

gets_s(chosen); FILE *output = fopen(("%s" ".txt", chosen), "w");

över huvud taget kompilerar, men... Som första argument till fopen skickar du

("%s" ".txt", chosen)

Detta slinker igenom kompilatorn för att strängarna "%s" och ".txt" slås ihop till en sträng. Sedan blir det ett komma-uttryck av det hela och resultat av komma-uttrycket är det högra uttrycket, dvs chosen. Vilket stämmer med det beteende du ser: filen får det namn användaren angav, utan ".txt".

Om du vill göra en ny sträng baserat på chosen föreslår jag att du kollar på sprintf (lite bökigt), strcat eller kanske C++-klassen string.