Morse code

i have no idea about y my code cant run successfully. it is just skip the input and print "No conversion was done due to an error". help meeeeee plzzzzzzzzz. :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <string>
#include <cctype> 
#include <algorithm>
#include <functional> 
using namespace std;

int main()
{
	string morse[39]={".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-",".-..", "--",
					"-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
					".----", "..---", "...--", "...._", ".....", "-....", "--...", "---..", "----.", "-----",
					".-.-.-", "--.--", "..--.."};
	string plaintext[39]={"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
						"S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", 
						"Stop", ",", "?"};
	
	string text, morsecode;
	char answer;
	int textlength, choice;
	int count=0, i=0;
	bool again=true;
	
	while (again)
	{	  
		cout<<"1-3 Enter a string to convert to Morse Code.\n"
			"2-3 Enter Morse Codes to convert to Text.\n"
			"3-3 Quit!!!";
		
		cout<<"\n\nEnter your choice: ";
		cin>>choice;
		  
		switch(choice)
		{
			case 1:
				cout<<"Enter a string: ";
				getline(cin, text);
				textlength=text.size();
				cout<<"You Entered: \""<<text<<"\" with "<<textlength<<" characters.\n\nConvert to: ";			
				transform(text.begin(), text.end(), text.begin(), ptr_fun <int, int> (toupper ) );
				for (i=0;i<textlength;i++)
			    {
					int find_string=0;
					while (find_string < 39)
					{
						if (text[i]==plaintext[find_string][0])
						{
							count=1;
							cout << morse[find_string] << " ";
							break;
						}
						find_string+=1;
					}
				}
				if (count==0)
					cout << endl << "No conversion was done due to an error" << "\a" << endl ;
				break;
			case 2:
				cout<<"Convert Morse Code to Text";
				cout<<"\n\nEnter Morse Code(sperate by space): ";
				getline(cin, morsecode);
				for(int i=0; i<morsecode.size(); i++)
				{
					int find_string=0;
					while (find_string < 39)
					{
						if (morsecode[i]==morse[find_string][0])
					    {
							count=1;
							cout << plaintext[find_string] << " ";
							break;
						}
						find_string+=1;
					}
				}
				if (count==0)
					cout << endl << "No conversion was done due to an error" << "\a" << endl;
				break;
		}
		cout<<"\n\nRun Again(y/n)? ";
		cin>>answer;
		answer=toupper(answer);
		if (answer!='y')
			again=false;
	}
	return 0;
}




1-3 Enter a string to convert to Morse Code.
2-3 Enter Morse Codes to convert to Text.
3-3 Quit!!!

Enter your choice: 1
Enter a string: You Entered: "" with 0 characters.

Convert to:
No conversion was done due to an error


Run Again(y/n)? y
1-3 Enter a string to convert to Morse Code.
2-3 Enter Morse Codes to convert to Text.
3-3 Quit!!!

Enter your choice: 2
Convert Morse Code to Text

Enter Morse Code(sperate by space):
No conversion was done due to an error


Run Again(y/n)? n

Press Enter to return to Quincy...
Last edited on
There are many things that are... interesting here :D

But your first problem is actually here: case 1:

you meant to say '1' instead of 1 (notice the single quotation mark)


[edit:] I was wrong, don't listen to me =]
Last edited on
ultifinitus wrote:
But your first problem is actually here: case 1:


choice is an int so that is OK how it is.

Some of the other problems:

Gee, this must be the week for using namespace std; errors.

line 3: don't do this. You have a count variable, which is interpreted as std::count (an algorithm function), which is this:

http://www.cplusplus.com/reference/algorithm/count/


That ought to give a compiler error IMO - calling a function without arguments.

If you have any compiler warnings or errors, then post them here in full.

Instead of line 3, put std:: before each std thing as in std::cout, std::cin std::endl std::string etc . This is much better than worrying about whether your variable name is in the std namespace.

Be careful with your variable names, it makes it harder for others to understand. find_string is not good when it's type is int

find_string+=1; can be written find_string++;

lines 78 to 80, the point of toupper is so you don't have to compare to both upper & lower case.



I'm surprised no one else mentioned this but you use std::getline right after using std::cin and operator >> which leaves a newline in the buffer so it doesn't read anything with std::getline You must ignore everything left in the buffer first.
Whew, I guess I better pay more attention, thanks TheIdeasMan!

Also +1 giblit you'll get past the first step with a cin.ignore()
@ultifinitus

No worries, the OP's variable names don't make it easy. If I had a $ for every time I messed up .....
i do not understand.....plz tell me more
So which parts don't you understand?
the "std" part,
That is why I think people shouldn't learn to use using namespace std; What you did was include the entire namespace(std). So then you didn't have to explicitly type it out each time like std::cout or std::endl;. This is can cause naming conflicts also there is a lot of things in the std namespace.
Also look at some of the posts I have been doing this past week, lots of errors just like this. Ideally one should put stuff in their own namespace, I talked about that too.
i have no idea about "std" things.....however, thx
Topic archived. No new replies allowed.