What did i do wrong about nested if?

Nested if not working and it's not showing the message. If i type 'W' there's nothing and the message is not showing.

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
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>

using namespace std;

int main()

{
    char s;
    cout << "Please choose the skill you want to use" << endl;
    cout << "F for fire" << endl;
    cout << "W for water" << endl;
    cout << "A for air" << endl;
    cout << "E for earth" << endl;
    cout << "Please type the skill you want to use here: ";
    cin >> s;


    if( s == 'F' || s == 'f' ) {

        cout << "You used fire element" << endl;

        if ( s == 'W' || s == 'w' )
        {
            cout << "You used water element" << endl;
        }
        if ( s == 'A' || s == 'a' )
        {
            cout << "You used air element" << endl;
        }
        if ( s == 'E' || s == 'e' )
        {
            cout << "You used earth element" << endl;
        }
        else
        {
            cout << "You didn't choose anything!" << endl;
        }
    }




    return 0;


}
The ifs are unconnected as
if (a){
}else if (b){
}else if (c){
}else{
}
I don't think you quite understand how this works, then again if you did I guess you wouldn't be posting here looking for help. I'll try to talk you through just whats going on and why as will as I can.
line 19 you have "if( s == 'F' || s == 'f' ) {" this makes it so that var s must equal 'F' or 'f' for any code between this line and it closing bracket on line 39 to run, since var s is equal to 'F' or 'f' and it doesn't change at all from line 16 when the user sets it. the only out put you will see is "You used fire element".

so lets say you the user types in 'w' line 19 the if statement is false and the program starts running the code from line 39 onward, type in 'F' line 19 the if statement is true and will display "You used fire element" but line 23 the if statement is false, line 27 the if statement is false, and line 31 the if statement is false and the else of the if statement of line 31 is ran then displaying "You didn't choose anything!".

I think what you may have been after is this.
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
#include <iostream>
#include <string>
using namespace std;

int main() {
	char s;
	cout << "Please choose the skill you want to use" << endl;
	cout << "F for fire" << endl;
	cout << "W for water" << endl;
	cout << "A for air" << endl;
	cout << "E for earth" << endl;
	cout << "Please type the skill you want to use here: ";
	cin >> s;
	
	if(s == 'F' || s == 'f')
		cout << "You used fire element" << endl;
	if(s == 'W' || s == 'w')
		cout << "You used water element" << endl;
	if(s == 'A' || s == 'a')
		cout << "You used air element" << endl;
	if(s == 'E' || s == 'e')
		cout << "You used earth element" << endl;
	if(!(s == 'F' || s == 'f' || s == 'W' || s == 'w' || s == 'A' || s == 'a' || s == 'E' || s == 'e'))
		cout << "You didn't choose anything!" << endl;
	return 0;
}


if this is what you after i would use a switch like this:
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
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main() {
	char s;
	cout << "Please choose the skill you want to use" << endl;
	cout << "F for fire" << endl;
	cout << "W for water" << endl;
	cout << "A for air" << endl;
	cout << "E for earth" << endl;
	cout << "Please type the skill you want to use here: ";
	cin >> s;
	
	switch(tolower(s)){
		case 'f':
			cout << "You used fire element" << endl;
			break;
		case 'w':
			cout << "You used water element" << endl;
			break;
		case 'a':
			cout << "You used air element" << endl;
			break;
		case 'e':
			cout << "You used earth element" << endl;
			break;
		default:
			cout << "You didn't choose anything!" << endl;
			break;
	}
	return 0;
}
Last edited on
move the brace "}" on line 39 to line 22

or lose all the braces - not good style :-)
Topic archived. No new replies allowed.