Input Letter, Output Word

Homework problem.

Have To: Use functional decomposition to make a program that inputs a letter and outputs the corresponding International Civil Aviation alphabet word. A is Alpha, B is Brave, etc, etc.

Hints please? I apparently can't figure it out myself. >_<

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*International Civil Aviation Organization alaphabet.
Inputs a letter and outputs corresponding word.
*/

#include <iostream>
using namespace std;

int main ()
{

char neededLetter; //Declaration of letter.

cin >> "Input letter." >> neededLetter; //User inputs needed letter.

//Runs from letter to letter to see which one needs to be outputted.

if (neededLetter == "A";)
{
	cout << "Alpha" << endl;
}
		else
if (neededLetter == "B")
{
	cout << "Bravo" << endl;
}
		else
if (neededLetter == "C")
{
	cout << "Charlie" << endl;
}
		else
if (neededLetter =="D")
{
	cout << "Delta" << endl;
}
		else
if (neededLetter =="E")
{
	cout << "Echo" << endl;
}		
		else
if (neededLetter =="F")
{
	cout << "Foxtrot" << endl;
}
		else
if (neededLetter =="G")
{
	cout << "Golf" << endl;
}		
		else
if (neededLetter =="H")
{
	cout << "Hotel" << endl;
}
		else
if (neededLetter =="I")
{
	cout << "India" << endl;
}
		else
if (neededLetter =="J")
{
	cout << "Juliet" << endl;
}
		else
if (neededLetter =="K")
{
	cout << "Kilo" << endl;
}
		else
if (neededLetter =="L")
{
	cout << "Lima" << endl;
}
		else
if (neededLetter =="M")
{
	cout << "Mike" << endl;
}
		else
if (neededLetter =="O")
{
	cout << "Oscar" << endl;
}
		else
if (neededLetter =="P")
{
	cout << "Papa" << endl;
}
		else
if (neededLetter =="Q")
{
	cout << "Quebec" << endl;
}
		else
if (neededLetter =="R")
{
	cout << "Romeo" << endl;
}
		else
if (neededLetter =="S")
{
	cout << "Sierra" << endl;
}
		else
if (neededLetter =="T")
{
	cout << "Tango" << endl;
}
		else
if (neededLetter =="U")
{
	cout << "Uniform" << endl;
		else
if (neededLetter =="V")
{
	cout << "Victor" << endl;
}
		else
if (neededLetter =="W")
{
	cout << "Whiskey" << endl;
}
		else
if (neededLetter =="X")
{
	cout << "X-ray" << endl;
}
		else
if (neededLetter =="Y")
{
	cout << "Yankee" << endl;
}
		else
{
	cout << "Zulu" << endl;
}

return 0;
}


There isn't enough room for all the error messages, but these keep popping up.
error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'
error C2446: '==' : no conversion from 'const char *' to 'int'
Basically, you cant compare to chars. You can compare strings by #including <string> and using std::string.

You also needed to sort out line 13, cin >> gets input from the user, and cout << outputs to the console.

It should look like this:


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

#include <iostream>
#include <string>

using namespace std;

int main ()

{
	using std::string;

string neededLetter; //Declaration of letter.

cout << "Please enter letter: ";

	cin >> neededLetter;

//Runs from letter to letter to see which one needs to be outputted.

if (neededLetter == "A")
{
	cout << "Alpha" << endl;
}
		
if (neededLetter == "B")
{
	cout << "Bravo" << endl;
}


You also dont need to use the else statement as if the if statement doesn't find what its looking for, it will just move on to the other.
Yes, but if you use the else statement, it will stop looking for other possibilities once the correct one is found.

Since this has to do with functional decomposition, I suggest you use a function somewhere in there. Try one with a prototype something like:
string ICAO_code( char letter );

You might want to consider the possibility that the input letter will not be majuscule; be sure to #include <cctype> and use the toupper() function.

Another thought is that you can avoid all those if..else if.. statements with a simple lookup table.
1
2
3
4
5
6
7
const char* ICAO_CODES[ 26 ] =
  {
  "Alpha",
  "Bravo",
  "Charlie",
  ...
  };
You can convert the letter itself into an index into the array via index = letter - 'A';

Your initial problem is that you were trying to compare a char ('A') with a char* ("A")-- which are two different things. Either compare chars to chars, or (as MYST suggested) use the STL string class to compare strings (a string to char* comparison is defined by the STL.)

Hope this helps.
Alright, thanks alot, both of you. : ]

The whole char/string thing definitely helped.

I'll play with the whole functional bit; I don't remember him showing us that part.

*marks as solved*
Topic archived. No new replies allowed.