ask user to type in morse code and translate

Hi, I have a homework to do. We can't use sstream members.

Here is the pb:
We have to ask the user to type in the morse code (which is provided by the prof), then the prgm translate it.
When I enter the morse code as a variable. It works, but I don't know what to use to get the input (i.e cin, getline, cin.getline....)

I have enclosed my code.
Thanks for the help.
#include <iostream>
#include<cctype>
#include<string>
#include<cstring>

using namespace std;

int main(){
string word=" ";
string code[7];
// the user has to type: ... .- ...- ..-- ..-. ..- -.
//It works when I use that :
//string code[]={"...",".-","...-","..--","..-.","..-","-."};

string morseCode[] = {".-", "-...", "-.-.", "-..", ".", "..-.", 
	"--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", 
".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", 
"-.--", "--.."};

cout<<"Enter the morse code: "<<endl;
getline( cin,code);

for(int i=0; i<7 ;++i)
{string st=code[i];
for(int j=0;j<26;++j)
	if(st == morseCode[j]) {word+=char(j+65);word+=" ";}
}
cout<<"the word is:" <<word;

return 0;
}

Open in new window

pgmerLAAsked:
Who is Participating?
 
phoffricCommented:
If you do not like that limitation where you need a space at the end of the code, then you can consider just using
    getline( cin, oneLine);
where oneLine is a string.

Then to parse the line, you can use oneLine.find(...)
    http://www.cplusplus.com/reference/string/string/find/

and then oneLine.substr(...) to copy out the section into your code string.
     http://www.cplusplus.com/reference/string/string/substr/

If you choose to go this way, then read very carefully what the various integer values mean in these two functions. It can be a little tricky to get the values just right.
   
0
 
phoffricCommented:
getline on code[7] does not match getline prototype:
    http://www.cplusplus.com/reference/string/getline/

You could use:
    for( int i=0; i<7; ++i  ) {
        string oneCode;
        getline( cin, oneCode, ' ' ); // using space as a delimiter
        code[ i ] = oneCode;
    }
but now you have to end what you type in with a space.

This now gives you an array of 7 strings. The output is:

Enter the morse code:
... .- ...- ..-- ..-. ..- -.

the word is: S A V F U
0
 
phoffricCommented:
>> the word is: S A V F U
In this case I didn't add the space after the morse code, so the program hung up. There is really an additional N at the end of this code. One of the codes is not in the list. What did you want to do for a bad morse code?
0
 
pgmerLAAuthor Commented:
Hi phoffric,
can you please check:

http://www.experts-exchange.com/Programming/Languages/CPP/Q_26939285.html

and if you have any suggestion, I am hitting a wall.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.