Infinite loop? No output

I'm not sure what's happening here. I've looked it over and changed a few things trying to pinpoint the problem to no avail.

Here's the code, it's nice and short
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
#include<iostream>
#include<string>

using namespace std;

string morseCon(char tmp)
{
	char alphChar[] = {' ',',','.','?','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','0','1',
						'2','3','4','5','6','7','8','9'};
	string morse[] = {" ","--..--",".-.-.-","..--..",".-","-...","-.-.","-..",".","..-.","--.",
						"....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.",
						"...","-","..-","...-",".--","-..-","-.--","--..","-----",".----",
						"..---","...--","....-",".....","-....","--...","---..","----."};

	int count = 0;
	bool found = false;

	while(found == false || count <= 40)
	{
		if (toupper(tmp) == alphChar[count])
			found = true;
		else
			count++;
	}

	if (found == true)
		return morse[count];
	else
		return "Character not available";
}

int main()
{
	string strInput;

	cout << "Enter something to convert to morse code: " << endl;
	getline(cin, strInput);

	char temp;

	cout << "Your input in morse code is: " << endl;
	for (int count = 0; count < strInput.length(); count++)
		cout << morseCon(strInput[count]) << " | ";

	system("pause");
	return 0;
}


Not the prettiest right now, particularly if a character is not found, but I'm really just interested in the lack of output right now.
This may or may not be the problem, but I would move the toupper() function outside of the while loop. This seems like something that can be calculated only once, but you are calculating it every iteration of the loop.

You also don't need your else statement in your while loop.

Also, when you find the character I would add a

break;

Although your while statement includes checks for when to exit, if the only thing you do is look for a character, then it makes no sense to check for the true condition one more time when you do d what you are looking for.
Last edited on
Thanks for the quick reply. I tried that, but still not showing anything after "Your input in morse code is: "

:/
I hate when this stuff happens

EDIT:
Ok, I didn't see your edit in time. The redundant check in the loop seemed to do the trick. Thanks!
Last edited on
Topic archived. No new replies allowed.