C++ program help (not entirely running)

Hi all, i have problem running this code that i built based on this C++ question and I am having trouble running. Please helppp

The question is as follows

An Internet service provider has three different subscription packages for its customers:
Package A: For $9.95 per month 10 hours of access are provided. Additional hours are $2.00 per hour.
Package B: For $14.95 per month 20 hours of access are provided. Additional hours are $1.00 per hour.
Package C: For $19.95 per month unlimited access is provided.
Write a program that calculates a customer s monthly bill. It should ask which package
the customer has purchased and how many hours were used. It should then display the total amount due.
Input Validation: Be sure the user only selects package A, B, or C. Also, the number of hours used in a month cannot exceed 744.

This is my code

#include <iostream>
using namespace std;

int main()
{
char package;
double total_amount=0;
int hours =0;

cout << "Select a package: " << endl;
cout << "Package\t\tCost\t\tHours Provided\t\tExtra Hours" << endl;
cout << "A\t\t$9.95\t\t10\t\t\t$2.00 per hour" << endl;
cout << "B\t\t$14.95\t\t20\t\t\t$1.00 per hour" << endl;
cout << "C\t\t$19.95\t\tUnlimited\t\tUnlimited" << endl;

cout << "Enter the package purchased:";
cin >> package;
while(package!= 'A' && package!='a' && package!='B' && package!='b'&&
package!='C' && package!='c')
{
cout <<"Error! You must select package A, B, or C. ";
cout << "Enter the package purchased: ";
cin >> package;
}

cout << "Enter the number of hours used:";
cin>>hours;
while(hours < 0 || hours > 744)
{
cout << "Error! Hours cannot be negative or exceed 744. You must enter appopriate hours. ";
cout << "Enter the number of hours used. ";
cin >> hours;
}

if (package == 'A' || package == 'a')
{if (hours <= 10)
total_amount = 9.95;
else
total_amount = 9.95+((hours-10)*2);
}
if(package == 'B' || package == 'b')
{if (hours <= 20)
total_amount = 14.95;
else
total_amount= 14.95+ ((hours-20)*1);
}
if(package == 'C' && package == 'c')
{total_amount=19.95;
}

system("pause");
return 0;
}
What is the exact problem ?

I did some changes in your program to make it more readable and easy to understand. Also, there was a '&&' in the last conditions which should actually be an '||' .

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

int main()
{
	char package;
	double total_amount=0;
	int hours =0;

	cout << "Select a package: " << endl;
	cout << "Package\t\tCost\t\tHours Provided\t\tExtra Hours" << endl;
	cout << "A\t\t$9.95\t\t10\t\t\t$2.00 per hour" << endl;
	cout << "B\t\t$14.95\t\t20\t\t\t$1.00 per hour" << endl;
	cout << "C\t\t$19.95\t\tUnlimited\t\tUnlimited" << endl;

	cout << "Enter the package purchased:";
	cin >> package;
	package = tolower(package);

	while(package!='a' && package!='b' && package!='c')
	{
	cout <<"Error! You must select package A, B, or C. ";
	cout << "Enter the package purchased: ";
	cin >> package;
	}

	cout << "Enter the number of hours used:";
	cin>>hours;
	while(hours < 0 || hours > 744)
	{
	cout << "Error! Hours cannot be negative or exceed 744. You must enter appopriate hours. ";
	cout << "Enter the number of hours used. ";
	cin >> hours;
	}

	if (package == 'a')
	{
		if (hours <= 10)
			total_amount = 9.95;
		else
			total_amount = 9.95+((hours-10)*2);
	}
	if(package == 'b')
	{
		if (hours <= 20)
			total_amount = 14.95;
		else
			total_amount= 14.95+ ((hours-20)*1);
	}
	if(package == 'c')
	{
		total_amount=19.95;
	}

	std::cout << total_amount << std::endl;


	system("pause");
	return 0;
} 


Edit: use code tags.
Last edited on
1
2
while(package!= 'A' && package!='a' && package!='B' && package!='b'&&
package!='C' && package!='c')


I personally really dislike constructs like this with the use of the && and !=. They are just ugly and not really scalable

I much prefer to use a switch with a quit case and a default clause to handle bad input, with the whole thing enclosed in a while loop:

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
bool Quit = false;
char option;

while (!Quit){
//get input for option var
//tolower function

    switch (option){
           case 'a':
           //call function
           break;

           case 'b':
           //call function
           break;

           case 'q': //user wants to quit
                  Quit = true;
                   //could use exit function here
                   break;
      
           default: 
               //deal with bad input
    }

}


Hope all goes well
@writetonsharma - Thank You soo much. This program runs great !

Also, I see you have used package = tolower(package);

What is the tolower function. I believe it has something to do with the inclusion of both upper and lower cases ? However, when I run the program and enter an invalid input. Say for instance i run p, instead of upper case package A. it says "Error! You must select package A, B, or C. " which is right but if I enter an upper case A right after. It goes in a loop frenzy. But if i enter lower case a instead. it works fine and goes on with saying enter the number of hours. What should I do ? Any way to fix this ? Can u help me ? Thanks man :)

@nelson007

Look more carefully at what I was saying in my last post - it explains how to deal with your problems.

The tolower function converts a char to lower case, toupper does the opposite. They are handy because it saves one from having to test a variable twice. There are several versions of them - google to find out how they work, or just look in the reference section on this page - top left.

HTH
Thank you soo much @TheIdeasMan. I have read your post. Your code helped me a lot ! You guys are a lifesaver :))
yes, as above said. they convert a upper case to lower case letter.

which is right but if I enter an upper case A right after. It goes in a loop frenzy


because, input is not converted to lower case inside the loop. So you need to do that the way its done out of the loop.

1
2
3
4
cout <<"Error! You must select package A, B, or C. ";
cout << "Enter the package purchased: ";
cin >> package;
package = tolower(package);


I would also suggest, debugging your code if you stuck in issues like these.
Last edited on
Topic archived. No new replies allowed.