I'm in kind of a jam and I would really really appreciate all the help I can get. Mind that I am a rookie in the world of c++ and have only taken a crash course for about 4 weeks now.
The problem I am having is that I'm supposed to write a program which translates text into morse and vice versa...
I've managed to make the translation from text to morse work using this code:
#include <iostream>
#include <string>
using namespace std;
string texttomorse(char c)
{
string text = "abcdefghijklmnopqrstuvwqyz"; //osv
string morse[] = {".-","-...","-.-.","-..", ".", "..-.", "--.",
"....", "..", ".---", "-.-", ".-..", "--",
"-.", "---", ".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--", "--.."}; //osv
int index = text.find(c);
if(index!=-1)
return morse[index];
else
return " ";
}
int main()
{
string ord;
getline(cin, ord);
string morse="";
for(int i=0; i<ord.length(); i++)
{
morse += texttomorse(ord[i]);
}
cout << morse << endl;
return 0;
}
Now to the problem at hand :P
To make the program translate morse to text I hoped it would be as simple as just reversing the process
accordingly:
#include <iostream>
#include <string>
using namespace std;
string morsetotext(char c)
{
string text = "abcdefghijklmnopqrstuvwqyz"; //osv
string morse[] = {".-","-...","-.-.","-..", ".", "..-.", "--.",
"....", "..", ".---", "-.-", ".-..", "--",
"-.", "---", ".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--", "--.."}; //osv
int index = morse.find(c);
if(index!=-1)
return text[index];
else
return " ";
}
int main()
{
string kod;
getline(cin, kod);
string text="";
for(int i=0; i<kod.length(); i++)
{
text += morsetotext(kod[i]);
}
cout << text << endl;
return 0;
}
It made me painfully aware that it wasn't as simple as that :P
Could anyone pleeeeeeaase help me with what it is I'm doing wrong?
Thx
This post has been edited by NickDMax: 10 February 2010 - 08:53 AM
Reason for edit:: Added/Fixed code tags [code]...your program here...[/code]

New Topic/Question
Reply



MultiQuote



|