I'm trying to write a program that outputs a word based on user's input of a character (first letter of the output word). My problem is that I'm trying to validate the user's input with an If-Else statement, but my compiler is screaming at me. For lines 20 through 68 I'm getting the error message: "non-lvalue in assignment." I'm not sure if my logical expressions are written incorrectly on line 16, or if my else-if statements are written incorrectly. I tried to change the operators in line 16 to ASCII integer values, i.e. if (65 <= letter <= 90 || 97 <= letter <= 122) which covers all upper and lowercase letters, but I received the same error. If anyone can help, I'd appreciate it. I don't know where to look for the error.
view source
print?
01 #include <iostream>
02 #include <string>
03
04 using namespace std;
05
06 int main()
07 {
08 // Input variables
09 char letter;
10
11 // Read input data
12 cout << endl << "Please enter a letter of the alphabet: ";
13 cin >> letter;
14
15 // Test input data
16 if ('A' <= letter <= 'Z' || 'a' <= letter <= 'z')
17 {
18 cout << endl << endl << "The corresponding International Civil "
19 << "Aviation Organization alphabet word is: ";
20 if (letter = 'A' || letter = 'a')
21 cout << "Alpha.";
22 else if (letter = 'B' || letter = 'b')
23 cout << "Bravo.";
24 else if (letter = 'C' || letter = 'c')
25 cout << "Charlie.";
26 else if (letter = 'D' || letter = 'd')
27 cout << "Delta.";
28 else if (letter = 'E' || letter = 'e')
29 cout << "Echo.";
30 else if (letter = 'F' || letter = 'f')
31 cout << "Foxtrot.";
32 else if (letter = 'G' || letter = 'g')
33 cout << "Golf.";
34 else if (letter = 'H' || letter = 'h')
35 cout << "Hotel.";
36 else if (letter = 'I' || letter = 'i')
37 cout << "India.";
38 else if (letter = 'J' || letter = 'j')
39 cout << "Juliet.";
40 else if (letter = 'K' || letter = 'k')
41 cout << "Kilo.";
42 else if (letter = 'L' || letter = 'l')
43 cout << "Lima.";
44 else if (letter = 'M' || letter = 'm')
45 cout << "Mike.";
46 else if (letter = 'N' || letter = 'n')
47 cout << "November.";
48 else if (letter = 'O' || letter = 'o')
49 cout << "Oscar.";
50 else if (letter = 'P' || letter = 'p')
51 cout << "Papa.";
52 else if (letter = 'Q' || letter = 'q')
53 cout << "Quebec.";
54 else if (letter = 'R' || letter = 'r')
55 cout << "Romeo.";
56 else if (letter = 'S' || letter = 's')
57 cout << "Sierra.";
58 else if (letter = 'T' || letter = 't')
59 cout << "Tango.";
60 else if (letter = 'U' || letter = 'u')
61 cout << "Uniform.";
62 else if (letter = 'V' || letter = 'v')
63 cout << "Victor.";
64 else if (letter = 'W' || letter = 'w')
65 cout << "Whiskey.";
66 else if (letter = 'X' || letter = 'x')
67 cout << "X-ray.";
68 else if (letter = 'Y' || letter = 'y')
69 cout << "Yankee.";
70 else
71 cout << "Zulu.";
72 }
73 else
74 cout << endl << endl << "Invalid data: you must enter a letter "
75 << "of the alphabet."
76
77 return 0;
78 }
you should not use the "<=" epression like "if ('A' <= letter <= 'Z' || 'a' <= letter <= 'z')"
.it's ok to use like " 'A'<= letter && letter <="Z'.
and there is anohter error in your code .
"else if (letter = 'Y' || letter = 'y')"
notice that you only uesed one "=" but i suggested you want to use "==".
it's a good habit that you always make the constant front of the vriable ,such as " 'Y' == letter.
even more a "=" is lost,the complier will show you a error.