Morse Code  | | |
March 13th, 2002, 11:17 PM
|
#1 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Orange,CA
Posts: 782
|
ok this is for a school assignment, we are suppose to create a program where the user inputs english and the program outputs the morse code for it and vice versa.
so far i've done only option 1: english to morse code but i'm running into a problem
say if the user enters :"hello"
the program prints out the morse code.
but if the user enters: "hello world"
the program goes haywire.. i dont know why either,since i have a case for spaces....any help would be appreciated...
btw yes i know the way im doing it is the long way but that's how the teacher sort of explained how to do it. Code: #include <iostream>
using namespace std;
void engconvert(char[]);
//void morseconvert(char[]);
int main()
{
int menuChoice;
char englstring[20];
char morsestring[20];
//allows user to make option and continue to use it until they are finished
while(menuChoice !=3)
{
cout <<"Menu" <<endl;
cout <<"1 - English to Morse Code \n";
cout <<"2 - Morse Code to English\n";
cout <<"3 - End Program\n";
cout << "Enter your choice now: ";
cin >> menuChoice;
if (menuChoice == 1) //option 1
{
cout <<"Enter the phrase which you would like to be converted to Morse Code: \n";
cin >> englstring;
engconvert(englstring);
}
if (menuChoice == 2){ //option 2
cout <<"Enter the Morse Code for which you would like to be converted to English:\n";
cin >> morsestring;
//morseconvert(morsestring);
}
if (menuChoice == 3) //option 3
{
cout << "Thanks for using the program. Good Bye!\n";
}
}
return 0;
}
void engconvert(char english[])
{
cout << "Here is the morse code translation:\n";
for (int i= 0;i<20;i++)
{
switch (english[i])
{
case 'a':
case 'A':
cout<<".- ";
break;
case 'b':
case 'B':
cout<<"-... ";
break;
case 'c':
case 'C':
cout <<"-.-. ";
break;
case 'd':
case 'D':
cout <<"-.. ";
break;
case 'e':
case 'E':
cout<<". ";
break;
case 'f':
case 'F':
cout<<"..-. ";
break;
case 'g':
case 'G':
cout<<"--. ";
break;
case 'h':
case 'H':
cout <<".... ";
break;
case 'i':
case 'I':
cout<<".. ";
break;
case 'j':
case 'J':
cout <<".--- ";
break;
case 'k':
case 'K':
cout<<"-.- ";
break;
case 'l':
case 'L':
cout<<".-.. ";
break;
case 'm':
case 'M':
cout<<"-- ";
break;
case 'n':
case 'N':
cout<<"-. ";
break;
case 'o':
case 'O':
cout << "--- ";
break;
case 'p':
case 'P':
cout<<".--. ";
break;
case 'q':
case 'Q':
cout <<"--.- ";
break;
case 'r':
case 'R':
cout << ".-. ";
break;
case 's':
case 'S':
cout <<"... ";
break;
case 'T':
case 't':
cout<<"- ";
break;
case 'u':
case 'U':
cout <<"..- ";
break;
case 'V':
case 'v':
cout<< "...- ";
break;
case 'W':
case 'w':
cout <<".-- ";
break;
case 'X':
case 'x':
cout << "-..- ";
break;
case 'Y':
case 'y':
cout << "-.-- ";
break;
case 'z':
case 'Z':
cout << "--.. ";
break;
case ' ':
cout<<" ";
break;
case '1':
cout << ".----";
break;
case '2':
cout <<"..---";
break;
case '3':
cout << "...--";
break;
case '4':
cout <<"....-";
break;
case '5':
cout << ".....";
break;
case '6':
cout << "-....";
break;
case '7':
cout << "--...";
break;
case '8':
cout << "---..";
break;
case '9':
cout << "----.";
break;
case '0':
cout << "-----";
break;
case '\0': // if the string is less than 20 characters it ends the loop
i=20;
break;
}
}
cout <<endl;
return;
}
|
| |
March 13th, 2002, 11:23 PM
|
#2 (permalink)
| | |
This isn't my thing, but is ' ' a null character or a space?? maybe there is another char for space , like a hex code or something?
Just a thought. Ignore me if I'm no where near.
Actually, another thought: maybe text strings with spaces need to be enclosed in quotation marks, like in good ol basic ie :""hello world"" instead of "world" with no spaces.
I have no idea of C or C++ I will admit, this is just a stab in the dark....  | |
| |
March 13th, 2002, 11:26 PM
|
#3 (permalink)
| | Member
Join Date: Oct 2001 Location: Duncanville,Texas
Posts: 223
|
Have you tried to do the entire alphabet? Does it have a problem with any other letter possibly 'n'?
cstierhoff
__________________
Out Playing in Traffic
|
| |
March 13th, 2002, 11:31 PM
|
#4 (permalink)
| | |
Just noticed something else in the code - dunno if it's significant or not.
Your cout commands don't all have the same syntax - some include a space after <<, some don't. Is this a problem?
eg
case ' ':
cout<<" ";
and
case 'z':
case 'Z':
cout << "--.. "; | |
| |
March 13th, 2002, 11:37 PM
|
#5 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Orange,CA
Posts: 782
|
i figured out the problem it's not reading anything after the "hello"
because i tried "1 2"
and what it did was it printed out the morse code for 1
then went back to the main menu and used 2 for that..
so the question now is how do i get it to read the whole string... |
| |
March 13th, 2002, 11:37 PM
|
#6 (permalink)
| | Not Really a Member
Join Date: Oct 2001
Posts: 25,398
|
well its definately puking after that space....
I ran it in VC++ and I'm stepping through it one step at a time. I look at the array and my input was 'stupid a$$'
'stupid' worked just fine, but as you said soon as I put 'stupid a$$' it died.
if you look at the array its
0=s
1=t
2=u
3=p
4=i
5=d
6= (space)
7= the rest is crap :/
ascii char -52 LOL whatever that is... |
| |
March 13th, 2002, 11:38 PM
|
#7 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Orange,CA
Posts: 782
|
mickwish the space doesnt effect the program,just make it neater...so i will go back and do that.... grrr this is only one step of the problem, still have to do morse code to english...joy joy joy |
| |
March 13th, 2002, 11:40 PM
|
#8 (permalink)
| | Not Really a Member
Join Date: Oct 2001
Posts: 25,398
|
btw, should of just upper cased the input string  LOL
that way you don't have to test for both cases
stupid teachers 
__________________
Helicopters don't fly; they vibrate so much and make so much noise that the earth rejects them.
|
| |
March 13th, 2002, 11:44 PM
|
#9 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Orange,CA
Posts: 782
|
ok i have to use a cin.get line to get it to read the whole thing (did some reading)
but how would i run the string into the function? |
| |
March 13th, 2002, 11:51 PM
|
#10 (permalink)
| | Not Really a Member
Join Date: Oct 2001
Posts: 25,398
|
its a function so you should be able to do a variable = cin.getline etc etc. |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |