Nested if-else

Hi,

When I key in N or n, it will state as "Congratulations! You are qualified to apply", when it should actually be "Sorry, you are not qualified to apply because you failed your English".

How do I get the N || n to work?

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
#include <iostream>
using namespace std;

int main()
{
	int Age = 0;
	char passEnglish; 
	
	cout << "Enter your age: ";
	cin >> Age;

	if (Age >= 18)
	{
		cout << "Did you passed your GCE 'O' Level English \n(Please enter Y for Yes and N for No): ";
		cin >> passEnglish;
		
		if (passEnglish == 'Y' || 'y')
			cout << "\nCongratulations! You are qualified to apply";
		else if (passEnglish == 'N' || 'n')
			cout << "\nSorry, you are not qualified to apply because you failed your English";
	}
	else
		cout << "\nSorry, you are not qualified to apply because you are below 18";

	cin.ignore();
	cin.ignore();

	return 0;
}
You need to replace passEnglish == 'Y' || 'y' with passEnglish == 'Y' || passEnglish =='y'. Do the same with the one about n.
The || operator checks its left and right sides. If either one of them is nonzero, it resolves as true.

Since 'y' is nonzero, it means passEnglish == 'Y' || 'y' will always be true because the right side of || is nonzero.

You need to have boolean expressions on each side of the ||. So in your case, you could do this:

 
if(passEnglish == 'Y' || passEnglish == 'y')


But really... you're better off getting rid of || all together and making 'passEnglish' always be uppercase or lowercase:

1
2
3
4
cin >> passEnglish;
passEnglish = toupper(passEnglish);  //<- make it uppercase

if(passEnglish == 'Y') // <- now there's no need for || 
Last edited on
Thank you! =)
Topic archived. No new replies allowed.