Problem with if else statements using char variable

I am trying to make a program that will display the mode of a Note in the key of C. For those who don't know music theory, this is what I want to happen:

If the user inputs the char 'A', display "Aeolian"
If 'B', display "Locrian"
If 'C', display "Ionain"
else display "INVALID"

The problem I am having is that the compiler will only acknowledge the first if statement ( which is for if the user inputs 'A' ). If I were to input 'B' or 'C', the compiler still goes right to the else statement. If I write a similar kind of program using integers, it works just fine, so I am assuming there is something wrong with my declaration, or I'm not writing the structure of multiple if statements. I guess run the code and you'll see what I mean, unless the answer is right in front of my face. Here is the Code:

#include <iostream>

using namespace std;



int main()
{
char input;

cout << " Type a character (A-C) to see its corresponding mode in C \n\n ";
cin >> input;

if (input == 'A')
{
if (input == 'B')
{cout << "Locrian";
return 0;}

if (input == 'C')
{cout << "Ionian";
return 0;}

cout << "Aeolian";
return 0;
}
else
cout << "INVALID";
return 0;
}



1) Please use code tags when posting code.

2) Look at:

1
2
3
4
if (input == 'A')
{
  if (input == 'B')
  {


How can that possibly make any sense? The outer code block at line 2 will only execute if input is 'A'. If input is 'A', how can it possibly be 'B' as well?
Last edited on
You have the if statements for 'B' and 'C' inside the block for when 'A' is true.
Also, you don't need to put return 0 in each branch, just put it once in the end.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main()
{
    char input;

    cout << " Type a character (A-C) to see its corresponding mode in C \n\n ";
    cin >> input;

    if (input == 'A')
        cout << "Aeolian";
    else if (input == 'B')
        cout << "Locrian";
    else if (input == 'C')
        cout << "Ionian";
    else
        cout << "INVALID";
    return 0;
}
Topic archived. No new replies allowed.