Question on this program (Please help!)

closed account (yR9wb7Xj)
How can I add an if and else statement in my code, what I want is when the user inputs a letter instead of number when its asking to enter the account number, it should automatically tell them that the account is invalid, because it was not numbers they input. Any suggestions? I've tried so many times but it didn't work. This is a problem that I just solved today from the book it's just for fun.

This is what I was trying to do in my code

string accountNum;
string letters;

if(accountNum != letters)
cout << "Please select one of the service Plans you have: Regular or Premium" << endl;
cin >> serviceCode;
else
cout << "You have enter a letter into your account, make sure it's a number. " << endl;


Problem: Write a program that calculates and prints the bill for a cellular telephone company. The company offers two types of service: regular and premium. It's rates vary, depending on the type of service. The rates are computed as follows.

Regular Service: $10.00 plus first 50 minutes are free. Charges for over 50 minutes are $0.20 per min.

Premium Service:$25.00 plus:
a. For calls made from 6am to 6pm the first 75mins are free; charges for more than 75min are $.10 per min.
b.For calls made from 6pm to 6am the first 100mins are free;charges for more than 100mins are $.05 per min.

Your program should prompt the user to enter an account number, a service code (type char), and the number of minutes the service was used. A service code of r or R means regular service; a service code of p or P means premium service. Treat any other character as an error. Your program should output the account number, type of service, number of mins the telephone service was used, and the
amount due from the user. For premium service, the customer may be using the service during the day and the night. Therefore to calculate the bill, you must ask the user to input the number of mins the service was used during the day and the number of mins the service was used during the night.

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <cmath>
#include <string>
using namespace std;

const double regService = 10.00;
const int freeMin = 50;
const double additionalFee = 0.20;

const int dayFreeMin = 75;
const double additionalDayFee = 0.10;
const int nightFreeMin = 100;
const double additionalNightFee = 0.05;

const double preService = 25.00;

int main() {

	double totalAmount = 0;
	string accountNum;
	char serviceCode;
	int totalMin;
	int dayMinTotal;
	int nightMinTotal;
	string letters;

	cout << "Welcome to Verizon Wireless! \n ";
	cout << "Please enter your account number." << endl;
	cin >> accountNum;
	cout << "Please select one of the service Plans you have: Regular or Premium"<< endl;
	cin >> serviceCode;

	switch (serviceCode) {
	case 'R':

		cout << "You have selected the regular service." << endl;
		cout << "\nEnter the total of minutes you have used." << endl;
		cin >> totalMin;
		if (totalMin > freeMin)
			totalAmount = regService + (totalMin - freeMin) * additionalFee;
		else
			totalAmount = regService;
		cout << "Your account number: " << "\n" << accountNum << endl;
		cout << "The service plan you have enter: " << "\n " << serviceCode
				<< "egular" << endl;
		cout << "The total minutes that have been used:" << "\n " << totalMin
				<< " " << "minutes" << endl;
		cout << "Your total cost will be for this month: " << "\n " << "$"
				<< totalAmount << endl;
		break;
	case 'r':
		cout << "You have selected the regular service." << endl;
		cout << "\nEnter the total of minutes you have used." << endl;
		cin >> totalMin;
		if (totalMin > freeMin)
			totalAmount = regService + (totalMin - freeMin) * additionalFee;
		else
			totalAmount = regService;
		cout << "Your account number: " << "\n" << accountNum << endl;
		cout << "The service plan you have enter is: " << "\n " << serviceCode
				<< "egular" << endl;
		cout << "The total minutes that have been used:" << "\n " << totalMin
				<< " " << " minutes" << endl;
		cout << "Your total cost will be for this month: " << "\n " << "$"<< totalAmount << endl;
		break;
	case 'P':
		cout << "You have selected the Premium service." << endl;
		cout
				<< "Enter the minutes you have used between the time of 6:00 A.M. to 6:00 P.M. . ";
		cin >> dayMinTotal;

		if (dayMinTotal > dayFreeMin)
			totalAmount = preService
					+ (dayMinTotal - dayFreeMin) * additionalDayFee;
		cout
				<< "Enter the minutes you have used between the time of 6:00 P.M. to 6:00 A.M.. ";
		cin >> nightMinTotal;
		if (nightMinTotal > nightFreeMin)
			totalAmount = preService + (nightMinTotal - nightFreeMin) * additionalNightFee;
		totalMin = dayMinTotal + nightMinTotal;
		if (totalMin > freeMin)
			totalAmount = preService + (totalMin - freeMin) * additionalFee;
		else
			totalAmount += preService;
		cout << "Your account number: " << "\n" << accountNum << endl;
		cout << "The service plan you have enter: " << "\n " << serviceCode<< "remium" << endl;
		cout << "The total minutes that have been used:" << "\n " << totalMin<< " " << "minutes " << endl;
		cout << "Your total cost will be for this month: " << "\n " << "$"<< totalAmount << endl;
		break;
	case 'p':
		cout << "You have selected the Premium service." << endl;
		cout<< "Enter the minutes you have used between the time of 6:00 A.M. to 6:00 P.M. . ";
		cin >> dayMinTotal;

		if (dayMinTotal > dayFreeMin)
			totalAmount = preService
					+ (dayMinTotal - dayFreeMin) * additionalDayFee;
		cout<< "Enter the minutes you have used between the time of 6:00 P.M. to 6:00 A.M.. ";
		cin >> nightMinTotal;
		if (nightMinTotal > nightFreeMin)
			totalAmount = preService + (nightMinTotal - nightFreeMin) * additionalNightFee;
		totalMin = dayMinTotal + nightMinTotal;
		if (totalMin > freeMin)
			totalAmount = preService + (totalMin - freeMin) * additionalFee;
		else
			cout << "Your account number: " << "\n" << accountNum << endl;
		cout << "The service plan you have enter: " << "\n " << serviceCode<< "remium" << endl;
		cout << "The total minutes that have been used:" << "\n " << totalMin<< " " << "minutes" << endl;
		cout << "Your total cost will be for this month: " << "\n " << "$"<< totalAmount << endl;
		break;

	default:
		cout<< "The service you have selected does not exist from the choices we list it."<< endl;

	}

	return 0;
}
Last edited on
The function is isdigit(char)
I would check a string like this:
1
2
3
4
5
string text = "12a";
bool digit = true;
for (unsigned int i = 0; i < text.size(); i++)
   if (!isdigit(text[i]))
      digit = false;

No need to post unnecessary code next time. All I wanted to see were the first 15 lines of your post.
Topic archived. No new replies allowed.