Checking if a char variable is equal in an if statement?

Hi,

I'm taking a beginners course in C++. Our latest project is to create this program that sort of outputs a phone company bill based on a few user inputs.

The problem I *think* I am having is that my first if statement follows through regardless if the char character is correct or not.

Early in the program I have the user input a character for "servcode":
1
2
3
4
5
6
7
8
9
cout << "Please enter your account number: ";
	cin  >> acctnum;
	cout << endl
		 << "Service codes: \n"
		 << "R = Regular Service\n"
		 << "P = Premium Service\n"
		 << endl
		 << "Please enter your service code: ";
	cin  >> servcode;


The user inputs either R, r, P, or p.

I then have an If and Else If statement that checks the value of that char:
if (servcode == 'r', 'R') //Regular Service

and

else if (servcode == 'p', 'P') //Premium Service

Unfortunately the program always follows the if branch no matter what the char the user inputs is.

Here is the full code: http://pastebin.com/hHQe26HQ
(sorry, pastebin kind of pushes some things down to a new line making it slightly harder to read).

So am I simply not able to check if a char is equals or am I doing something else wrong?

Thanks,
Jake
Last edited on
if(servcode == 'r' || servcode == 'R')

You cannot use the comma operator like that.
Last edited on

- Use the or operator.

1
2
3
4
5
6
7
8
9
10

if (var== 'r' || 'R')
{

}
else if ( var == 'p' || 'P')
{

}
Last edited on
- Use the or operator.
1
2
3
4
5
6
7
8
if (var== 'r' || 'R')
{

}
else if ( var == 'p' || 'P')
{

}



Do use the logical or operator as illustrated by L B.

@thejman250:

Your code is equivalent to:
1
2
3
4
5
6
if ( 'R' )
{
}
else if ( 'P' )
{
}
Last edited on
EDIT: Thank you! That worked.

Originally I thought it didn't work but that turned out to be an issue with my compiler and it's now resolved. Here is the finished code (although I'm still going to mess with it, but it's working):

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	string acctnum;
	char servcode;
	int minutes;
	int overage;
	int minutesAmToPm;
	int minutesPmToAm;
	int minuteCheck;
	int overageAmToPm;
	int overagePmToAm;
	bool minutesCheckBool = 0;
	double overageCost;
	double overageAmToPmCost;
	double overagePmToAmCost;
	double totalBill;

	cout << "Please enter your account number: ";
	cin  >> acctnum;
	cin.ignore(100, '\n');
	cout << endl
		 << "Service codes: \n"
		 << "R = Regular Service\n"
		 << "P = Premium Service\n"
		 << endl
		 << "Please enter your service code: ";
	cin  >> servcode;
	cout << "How many minutes have you used? " << endl
		 << "Minutes: ";
	cin  >> minutes;

	if (servcode == 'R' || servcode == 'r') //Regular Service
	{
		overage = minutes - 50;
		overageCost = overage * 20;
		totalBill = (overageCost / 100) + 10;

		cout << "\nAccount Number: " << acctnum << endl
				<< "Service Type: Regular \n"
				<< "Access Fee: $10\n"
				<< "Minutes Used: " << minutes << " / 50\n"
				<< "Minutes Over: " << overage << endl
				<< "Overage Cost: $" << setprecision(2) << fixed << overageCost / 100 << " ($0.20 per minute.)\n"
				<< endl
				<< "Total Amount Due: $" << setprecision(2) << fixed << totalBill;
	}

	else //Premium Service
	{
		while (minutesCheckBool == 0) //Allows the user to reinput information instead of terminating the program.
		{
			cout << "You used a total of " << minutes << " minutes.\n"
					<< "How many of those minutes were between 6:00am and 6:00pm?\n";
			cin  >> minutesAmToPm;
			cout << "Now how many of those minutes were between 6:00pm and 6:00am?\n";
			cin >> minutesPmToAm;

			minuteCheck = minutesAmToPm + minutesPmToAm;

			if (minuteCheck > minutes)
				cout << "Sorry, you input more or less than your total minutes. Please try again.\n";
			else if (minuteCheck == minutes)
				minutesCheckBool = 1; //User's information is correct, so the loop ends.
		}

		overageAmToPm = minutesAmToPm - 75;
		overageAmToPmCost = overageAmToPm * 10;
		overagePmToAm = minutesPmToAm - 100;
		overagePmToAmCost = overagePmToAm * 5;

		totalBill = ((overagePmToAmCost + overageAmToPmCost) / 100) + 25;

		cout << "\nAccount Number: " << acctnum << endl
				<< "Service Type: Premium\n"
				<< "Access Fee: $25\n"
				<< "Minutes Used: " << minutes << endl
				<< "Daytime Minutes: " << minutesAmToPm << endl
				<< "Night-Time Minutes: " << minutesPmToAm << endl
				<< "Day Charges: " << setprecision(2) << fixed << overageAmToPmCost / 100 << " for $0.10 a minute over 75 minutes.\n"
				<< "Night Charges: " << setprecision(2) << fixed << overagePmToAmCost / 100 << " for $0.05 a minute over 100 minutes.\n"

				<< "Total Amount Due: $" << setprecision(2) << fixed << totalBill;
	}

		cin.ignore(100, '\n');
		cin.get();
		return 0;
}
Last edited on
Topic archived. No new replies allowed.