If/else problem

hey guys, Im having a problem with the following piece of code for soem reason when i execute the program and test this part, lets say the first if statement is true and its supposed to print out "blah blah blah", it prints that out but then goes on to also print the else part which is "failed", This doesnt happen with the second IF only the first one. So any ideas on how i can make it not print the else for the first IF? Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
case '3':
                cout<<"Please enter student ID: ";
                cin >> ID2;
                if (ID2 == ID[0]){
                cout<<"blah blah blah"<<endl;
                }
                if (ID2 == ID[1]){
                cout<<"ha ha ha"<<endl;
                }
                else {
                     cout<<"failed"<<endl;
                     }
                break;
closed account (z05DSL3A)
try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
case '3':
    cout<<"Please enter student ID: ";
    cin >> ID2;
    if (ID2 == ID[0])
    {
        cout<<"blah blah blah"<<endl;
    }
    else if (ID2 == ID[1])  // use else if
    {
        cout<<"ha ha ha"<<endl;
    }
    else 
    {
        cout<<"failed"<<endl;
    }
    break;
Last edited on
It's because you have to seperate blocks of if, first one being ID2 == ID[0], next block being ID2 == ID[1], else.

Try changing if (ID2 == ID[1]) to else if(ID2 == ID[1])
Thanks alot guys, worked :)
Topic archived. No new replies allowed.