What's wrong with my program?

Here is part of the code for a program I copied out of a textbook:

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
int main()
	{
	//Local Declaration
	char *encDec[27][2] =
		{
			{ "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", "--..#" },
			{ " ", "$$#" }
		};
	char inStr [STR_LEN];
	char outStr [STR_LEN];
	char option;
	bool done = false;
	
	//Statements
	while (!done)
		{
		option = menu();
		switch (option)
			{
			case 'E':
				getInput (inStr);
				if (!encode (encDec, inStr, outStr))
					{
					cerr << "Error! Try again";
					break;
					} //if
				printOutput (inStr, outStr);
				break;
			case 'D':
				getInput (inStr);
				if (!decode (encDec, inStr, outStr))
					{
					cerr << "Error! Try again";
					break;
					} //if
				printOutput (inStr, outStr);
				break;
			default:
				done = true;
				cout << "\nEnd of Morse Code.\n";
			} //switch
		} //while
	return 0;
	} //main

bool encode (char *(*encDec)[2], char *inStr, char *outStr)
	{
	//Local Declarations
	char s1[2];
	char s2[6];
	bool error = false;
	
	//Statements
	outStr[0] = '\0';
	while (*inStr != '\0' && !error)
		{
		s1[0] = toupper(*inStr);
		s1[1] = '\0';
		error = convert (encDec, s1, 0, s2);
		strcat (outStr, s2);
		inStr++;
		}
	return (!error);
	} //encode

int convert (char *(*encDec)[2], char *s1, int col, char *s2)
	{
	//Statements
	int found = 0;
	int i;
	
	for(i=0; i<27 && !found; i++)
		found = !strcmp(s1, encDec[i][col]);
		
	if(found)
		strcpy (s2, encDec [i-1][(col + 1) % 2]);
	else
		*s2 = '\0';
		
	return found;
	} //convert 


I know there is no problem with the getInput routine or the menu routine so I have omitted them from the code to simplify things a bit. The idea of the program is to convert a string to Morse Code or convert Morse Code to a string. I was just testing the encoding function (to convert a string to Morse Code), but whatever word I type in, I get the error message "Error: Try again!" from the code in main, meaning that somewhere along the line from calling encode to calling convert, the program isn't functioning as intended. But I can't figure out where the problem is, so I have included the encode and convert methods in the hope that maybe a sharp eye can figure it out.
It took a bit of finding but....

Line 98; found = !strcmp(s1, encDec[i][col]);

sets found to a one if the two strings match, i.e. its done an encde and found is returned at the end of the function, everthing is OK so far.

On line 84 error = convert (encDec, s1, 0, s2);

picks up this return value and so error is now a one or true.

Line 88 return (!error)

inverts this to zero or false and returns it to

line 47 if (!encode (encDec, inStr, outStr))

which takes it as a failure to encode.

To fix it, I suggest getting rid of the invert on line 88
Top work bnbertha :) I hadn't had a chance to compile it up and have a play yet.
Thanks for your help, bnbertha. I discovered today, after your suggestion, that I actually mistyped the program. The correction is on line 84, which should read:
 
error = !convert (encDec, s1, 0, s2);

Topic archived. No new replies allowed.