Why is my If Statement coming true when it's false?

I've tried googling but I have had no luck so far.

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
  	cout << "Please enter account number and service code <s or p>" << endl;
		int accountNum;
		char serviceCode;
		int dayMinutes, nightMinutes;
		cin >> accountNum >> serviceCode;
		while (serviceCode != 's' && serviceCode != 'S' && serviceCode != 'p' && serviceCode != 'P') {
			cout << "Please enter account number and VALID service code <s or p>" << endl;
			cin >> accountNum >> serviceCode;
		}
		cout << serviceCode;
		int regMinutes;
		if (serviceCode == 's' || 'S') {
			cout << "Please enter number of minutes" << endl;
			cin >> regMinutes;
			while (regMinutes < 0) {
				cout << "Please enter VALID number of minutes" << endl;
				cin >> regMinutes;
			}
		}
		else if (serviceCode == 'p' || 'P') {
			cout << "Please enter number of daytime minutes and nighttime minutes." << endl;
			cin >> dayMinutes >> nightMinutes;
			while (dayMinutes + nightMinutes < 0) {
				cout << "Please enter VALID number of daytime minutes and nightime minutes." << endl;
				cin >> dayMinutes >> nightMinutes;
			}


So I am having a little trouble figuring this out. If I input the service code as 's' the program works perfectly and it does the math correctly later on. If I input the service code as 'p', then print the service code to show it knows the service code is 'p', it still says the first if statement is true that service code == 's' or 'S'. Is it a syntax error? I've been working on this since yesterday and I am stuck.
if (serviceCode == 's' || 'S') {

On the left, serviceCode == 's'. A comparison that may or may not come out as true.

On the right, 'S'. Which is not false, so it's true. Always. 'S' evaluates to true.

So your code is:
if (serviceCode == 's' || true ) {

I expect you meant
if (serviceCode == 's' || serviceCode == 'S') {
Huh, I was not expecting it to be so simple. Thank you for your help I need to remember to add that in there everytime and not think of it as if this equals to this or this, and think of it as if this equals to this or this equals to this.
or, consider instead
if(tolower(serviceCode) == 's')

Topic archived. No new replies allowed.