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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phoffric\Commented:
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
phoffric\Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
phoffric\Commented:
>> 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.