If else statements

I am trying to write a code that reads a message in other language based on what letter is typed in the beginning. I have every thing working but it won't give me any out put other than we don't speak that language. Thanks in advanced.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <iostream>
using namespace std;
int main(){
    char language;
    char e,f,g,i,r;
    cout << "English, French, German, Italian, or Russian? (e|f|g|i|r):  ";
    cin >> language;
    if (language ==e ) {cout << "Welcome to Physics 270!";}
    if (language ==f ) {cout << "Bon jour, Physics 270!";}
    if (language ==g ) {cout << "Guten tag, Physics 270!";}
    if (language ==i) {cout << "Bon giorno, Physics 270!";}
    if (language ==r ) {cout << "Dobre utre, Physics 270!";}
    else{ cout << "Sorry; we don’t speak your language.";}
        return 0;
        }
Last edited on
char e,f,g,i,r;

This creates 5 different variables... each named e, f, etc.

Note that the name of the variable does not matter... it is just for the programmers reference. The name can be anything. For example... you could name them "dog", or "cat" and they would still function the exact same way.




Knowing that... you never actually assign any values to those variables. So your "e" variable contains garbage.

So when you do this:

if (language ==e )

You are NOT checking to see if the user input the letter e, but instead are checking what they input and comparing it to the contents of your e variable (which, again, you never filled -- so it has nonsense in it).




You do not want to compare against a variable's contents here. You want to compare against an actual value. For that, you want a literal:

1
2
3
4
//  char e,f,g,i,r;  // <-  get rid of these.  You don't need/want them.
    cout << "English, French, German, Italian, or Russian? (e|f|g|i|r):  ";
    cin >> language;
    if (language =='e' ) { // <- compare with the literal 'e' instead of a variable named e 
Thank you so much, so the single apostrophe around a letter denotes it as a letter and not a variable?
Yes.
Topic archived. No new replies allowed.