What is wrong with this else if statement?

I have a multiple if, else if, else statement and the problem is that even when a condition is met and the associated code is ran the else statement after is also being executed too.

Can someone please tell me why this is happening as I have read many posts and threads about these statements and I can't see where my logic/syntax is going wrong.

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
cin >> Y;

if (y == 'A' || y == 'a')
{
         option_a = 1;
}
else if (y == 'B' || y == 'b')
{
	option_b = 1;
}
else if (y == 'C' || y == 'c')
{
	option_c = 1;
}
else if (y == 'D' || y == 'd')
{
	option_d = 1;
}
else if (y == 'E' || y == 'e')
{
	option_e = 1;
}
else if (y == 'F' || y == 'f')
{
	option_f = 1;
}
else if (y == 'G')
{
	option_g = 1;
}
else if (y == 'H' || y == 'h')
{
	option_h = 1;
}
else if (y == 'I' || y == 'i')
{
	option_i = 1;
}
else  
{
	cout << "Not an option." << endl;
}


the option of choice WILL BE saved but then "Not an Option" is still flagged, no matter what char is input.

UPDATE: Seems there was a bad else if in my code, functions fine now.
Last edited on
Are you sure that one of the conditions is being met? Have you observed that one of those option_<foo> variables is being set to 1, as well as the else block being executed? Or are you just assuming that's the case?

The variable you're using to store the user input on line 1, is not the same as the variable you're checking in all your if and else if statements. Can you see why?
Last edited on
Topic archived. No new replies allowed.