Friday, July 31, 2009

C++ Have problem when im executing my code (Morse Code)?

Can anybody help me solve a little problem in my code. The problem is when i execute my code i enter a word to translate to morse code and it was translate, but when i try to enter 2 words it seems that it giving this wierd thing. plz tell me what the problem or show me how to fixed it.





Here is my Code i made:


#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


using namespace std;


void engconvert (char[50]);





int main()


{


int menuChoice;


char englstring[50];





while(menuChoice != 2)


{


cout %26lt;%26lt;"Menu" %26lt;%26lt;endl;


cout %26lt;%26lt;"1 - English to Morse Code\n";


cout %26lt;%26lt;"2 - End Program\n";


cout %26lt;%26lt; "Enter your choice now: ";


cin %26gt;%26gt; menuChoice;


if (menuChoice == 1) //option 1


{


cout %26lt;%26lt;"Enter the English text which you would like to be converted to Morse Code: \n";





cin %26gt;%26gt; englstring;





engconvert(englstring);


}





if (menuChoice == 2) //option 2


{


cout %26lt;%26lt; "Thanks for using the program. Good Bye!\n";


}


}





return 0;


}





void engconvert (char english[])


{


cout %26lt;%26lt; "Here is the morse code translation:\n";


for (int i = 0; i%26lt;50; i++)





{


switch (english[i])


{


case 'a':


case 'A':


cout %26lt;%26lt;".-";


break;





case 'b':


case 'B':


cout%26lt;%26lt;"-... ";


break;





case 'c':


case 'C':


cout%26lt;%26lt;"-.-. ";


break;





case 'd':


case 'D':


cout%26lt;%26lt;"-.. ";


break;





case 'e':


case 'E':


cout %26lt;%26lt;". ";


break;





case 'f':


case 'F':


cout%26lt;%26lt;"..-. ";


break;





case 'g':


case 'G':


cout%26lt;%26lt;"--. ";


break;





case 'h':


case 'H':


cout%26lt;%26lt;".... ";


break;





case 'i':


case 'I':


cout%26lt;%26lt;".. ";


break;





case 'j':


case 'J':


cout%26lt;%26lt;".--- ";


break;





case 'k':


case 'K':


cout%26lt;%26lt;"-.- ";


break;





case 'l':


case 'L':


cout%26lt;%26lt;".-.. ";


break;





case 'm':


case 'M':


cout%26lt;%26lt;"-- ";


break;





case 'n':


case 'N':


cout%26lt;%26lt;"-. ";


break;





case 'o':


case 'O':


cout%26lt;%26lt;"--- ";


break;





case 'p':


case 'P':


cout%26lt;%26lt;".--. ";


break;





case 'q':


case 'Q':


cout%26lt;%26lt;"--.- ";


break;





case 'r':


case 'R':


cout%26lt;%26lt;".-. ";


break;





case 's':


case 'S':


cout%26lt;%26lt;"... ";


break;





case 't':


case 'T':


cout%26lt;%26lt;"- ";


break;





case 'u':


case 'U':


cout%26lt;%26lt;"..- ";


break;





case 'v':


case 'V':


cout%26lt;%26lt;"...-";


break;





case 'w':


case 'W':


cout%26lt;%26lt;".-- ";


break;





case 'x':


case 'X':


cout%26lt;%26lt;".-- ";


break;





case 'y':


case 'Y':


cout%26lt;%26lt;"-.-- ";


break;





case 'z':


case 'Z':


cout%26lt;%26lt;"--.. ";


break;





case '':


cout%26lt;%26lt;"";


break;





case '1':


cout%26lt;%26lt;".----";


break;





case '2':


cout%26lt;%26lt;"..---";


break;





case '3':


cout%26lt;%26lt;"...--";


break;





case '4':


cout%26lt;%26lt;"....-";


break;





case '5':


cout%26lt;%26lt;".....";


break;





case '6':


cout%26lt;%26lt;"-....";


break;





case '7':


cout%26lt;%26lt;"--...";


break;





case '8':


cout%26lt;%26lt;"---..";


break;





case '9':


cout%26lt;%26lt;"----.";


break;





case '0':


cout%26lt;%26lt;"-----";


break;





case '\0': // if the string is less than 50 characters it ends the loop


i=50;


break;





}





}


cout %26lt;%26lt;endl;


}

C++ Have problem when im executing my code (Morse Code)?
Well, the one thing that I see is that you do not have a case for a blank which would (should) be the token separator for two words. You have a case for '' which is an empty string. You will probably never get to that so you could just try to insert a space in there and see if it works. You should also have a "default" case in there to catch anything that was not taken care of anywhere else. You didn't give any of your sample out put but I'd bet that one of those two things might clear you up.





Good Luck
Reply:cin only gets one word. Try using cin.get(variable);





You can also use getline(cin,variable);





You also need to throw in a case for the space character, or just parse the string of words into an array and run your code for each word.


No comments:

Post a Comment