Help If Else statement

im trying to make this program where you can choose a language between the ones given. But every time i try to run it, no matter the input it give me only english back.

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
#include <iostream>

int main() {

    char e = 0;
    char f = 0;
    char g = 0;
    char i = 0;
    char r = 0;
    int choice;

    std::cout << "Engl., Fren., Ger., Ital., or Rus.? (e|f|g|i|r): ";
    std::cin >> choice;


    if(choice == e) {
       std::cout << "English";

    } else if (choice == f) {
       std::cout << "French";

    } else if (choice == g) {
       std::cout << "German";

    } else if (choice == i) {
       std::cout << "Italian";

    } else if (choice == r) {
       std::cout << "Russian";

    }  else
       std::cout << "Invalid input";

return 0;

}

Last edited on
Hi, I think there is confusion on the nature of what a variable is.

The actual name of a variable doesn't mean anything to the program itself (only to the human reading it). For example, you could have called your choice variable int blahblahblah; std::cin >> blahblahblah; and your code would have run the same.

Likewise, you have variables "char e", "char f", etc. These are uninitialized variables, regardless of the name you gave them. These variables don't actually have valid values in them because you never set them.

Instead of declaring variables, character literals can be written using single quotes ('a').

change
if (choice == e)
to
if (choice == 'e')
and do the same for the other letters.

and get rid of your e,f,g,i,r char variables.

EDIT: And change "int choice;" to "char choice;"

Your choice variable is where you store what the user entered (whether they entered an 'e', 'f', 'g', 'i', etc.).
Last edited on
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
#include <iostream>

int main() {

    char choice;

    std::cout << "Engl., Fren., Ger., Ital., or Rus.? (e|f|g|i|r): ";
    std::cin >> choice;


    if(choice == 'e') {
       std::cout << "English";

    } else if (choice == 'f') {
       std::cout << "French";

    } else if (choice =='g') {
       std::cout << "German";

    } else if (choice == 'i') {
       std::cout << "Italian";

    } else if (choice == 'r') {
       std::cout << "Russian";

    }  else
       std::cout << "Invalid input";

return 0;

}


but it is better to use switch for this
1
2
3
4
5
6
7
8
9
10
switch (choice){
        case 'e': 
            std::cout << "English";
            break;
        case ...
        .
        .
        .
        default 
            std::cout << "Invalid input";
Last edited on
How can you compair char and int????

In C and C++ a char is a tiny int.
Try for yourself
1
2
3
4
5
int ch = 65;
if (ch == 'A')
  std::cout << "same";
else
  std::cout << "different";
In C and C++ a char is a tiny int.
Try for yourself


True
Last edited on
thank you so much guys! @saeidsj @Ganado @Thomas1965
Topic archived. No new replies allowed.