Question on adding 10 sums

Hey, I nearly completed my code, however there is one problem. I have to add 10 customers' bills and vat. I'm not sure how to do that. I have tried to perform this but failed everytime. Some help could be appreciated.

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
#include <iostream>
#include <cmath>
using namespace std;
int main(){
	int phone_service;
	int count;
	int airtime;
	int package;
	int pre_paid;
	double paygo;//Pay as you go.
	double bill;
	double vat;
	double vat1;
	double total1;
	double phone_charge;
	double phone_rental;
	double total;
	double total10;
	double sum;
	double vat10;
	phone_charge=0.25;
	vat=0.125;
	phone_rental=25;
	paygo=0.50;
	pre_paid=50;
	
	for(count=0;count<10;count=count+1){
		cout<<"Enter phone-service (1 for home phone or 2 for mobile phone): ";
		cin>>phone_service;
		cout<<"Please enter the amount of minutes of airtime: ";
		cin>>airtime;
		
   if(phone_service==1){
   	bill=ceil(phone_charge*airtime)+phone_rental;//Home Phone
   
   }else{
   
   	cout<<"Enter mobile package(3 for pay-as-you-go and 4 for pre-paid): ";
   	cin>>package;
   
   if(package==3){
   	bill=ceil(paygo*airtime);//Mobile Package 3
   }
   else{
   	bill=pre_paid*1.0;
   	//Mobile Package 4
   }
}
   
   
 cout<<"Phone service picked: ";
 if(phone_service==1){
 	cout<<"Home Phone"<<endl;
 
 }else{
 	cout<<"Mobile with package: ";
 
 if(package==3){
 cout<<"Pay-as-you-go"<<endl;	
 }
 else{
 	cout<<"PrePaid Package"<<endl;;	
 }
}

 cout<<"Number of minutes used: "<<airtime<<endl;
 cout<<"Cost of airtime: $"<<bill<<endl;
 vat1=ceil(vat*bill);//The reason for vat1 and total1 is because the code would increase the total and vat each time the loop is performed.
 total1=ceil(vat1+bill);
 cout<<"Vat charged: $"<<vat1<<endl;
 cout<<"Total bill of the customer: $"<<total1<<endl;
 
}
 cout<<"Total bill for ten customers are: $"<<total10<<endl;
 cout<<"Total vat for ten customers are: $"<<vat10<<endl;
}
 
Last edited on
You need to either store the information for all ten customers, OR do the calculations (i.e. keep track of the total) as the data is entered.
I didn't really look too much at your code but I'm guessing you need something like this:

1
2
3
4
5

double result = vat + // whatever else;

cout << result;


~Talemache
Hello CheesyForde1,

I off this revision of your code as an example of what you could have done. There are some comments in the program worth reading.

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
#include <iostream>
#include <cmath>

//using namespace std;  // <--- Best not to use.

int main()
{
	constexpr int PRE_PAID{ 50 };
	constexpr double PAYGO{ 0.50 };//Pay as you go.
	constexpr double PHONE_CHARGR{ 0.25 };
	constexpr double PHONE_RENTAL{ 25.0 };
	constexpr double VAT{ 0.125 };
	constexpr int MAXLOOP{ 3 };  // <---3 used for testing. Change to 10 for normal run.
	int phone_service{};
	//int count{};
	int airtime{};
	int package{};
	double bill{};
	double vat1{};
	double total1{};
	double total{};
	double total10{};
	double sum{};
	double vat10{};

	// <--- These variables changed to constants.
	//PHONE_CHARGR = 0.25;
	//vat = 0.125;
	//PHONE_RENTAL = 25;
	//PAYGO = 0.50;
	//PRE_PAID = 50;

	//for (count = 0; count < 10; count = count + 1)
	for (int count = 0; count < MAXLOOP; count++)  // <--- Best to give "count" a type here.
	{
		std::cout << "\nEnter phone-service (1 for home phone or 2 for mobile phone) for customer " << count + 1 << ": ";
		std::cin >> phone_service;
		std::cout << "Please enter the amount of minutes of airtime: ";
		std::cin >> airtime;

		if (phone_service == 1)
			bill = ceil(PHONE_CHARGR * airtime) + PHONE_RENTAL;//Home Phone
		else
		{
			std::cout << "Enter mobile package(3 for pay-as-you-go and 4 for pre-paid): ";
			std::cin >> package;

			if (package == 3)
				bill = ceil(PAYGO * airtime);//Mobile Package 3
			else
				bill = PRE_PAID * 1.0;
				//Mobile Package 4
		}

		std::cout << "\nPhone service picked: ";
		if (phone_service == 1)
			std::cout << "Home Phone" << std::endl;
		else
		{
			std::cout << "Mobile with package: ";

			if (package == 3)
				std::cout << "Pay-as-you-go" << std::endl;
			else
				std::cout << "PrePaid Package" << std::endl;;
		}

		std::cout << "Number of minutes used: " << airtime << std::endl;
		std::cout << "Cost of airtime: $" << bill << std::endl;

		vat1 = ceil(VAT * bill);//The reason for vat1 and total1 is because the code would increase the total and vat each time the loop is performed.
		total1 = ceil(vat1 + bill);
		
		total10 += total1;  // <--- Added.
		vat10 += vat1;      // <--- Added.

		std::cout << "vat charged: $" << vat1 << std::endl;
		std::cout << "Total bill of the customer: $" << total1 << std::endl;
	}

	std::cout << "\n\nTotal bill for ten customers are: $" << total10 << std::endl;  // <--- Using uninitialized variable.
	std::cout << "Total vat for ten customers are: $" << vat10 << std::endl;  // <--- Using uninitialized variable.

	return 0;
}

A few things to mention:

int main() { is an acceptable way of writing the {}s, but
1
2
3
4
int main()
{

}

Lines the {}s up in the same column and makes the code easier to follow. It is your choice, but the easier you make the code to read and follow the better it is.

You will notice I rearranged some variables and made the constants. These are variables that should never be changed in a program. The fact that they come first make them easier to find when a change is needed. Notice that the names are all caps. This is to help let you know that they are defined as constants and can not be changed.

This will shorten the code by five lines and along with removing some extra blank lines and some {}s from if/else statements with only one line.

Your for loop for (int count = 0; count < MAXLOOP; count++). By giving "count" a type here you can eliminate the need for defining "count" at the beginning of the program. Here you can see how "MAXLOOP" is used. By setting the size to 3 when it was defined I can test all three cases and not have to go through ten iterations of the for loop just to see the final output.

This one for loop is not the best example of using "MAXLOOP", but consider ten for loops. Having to go through to change what is referred to as a magic number it would be easy to miss one for loop and then your whole program is off and it could take some time to find the problem and correct it.

For the third part of the for loop it is more common to use "count++" instead of what you have. The "++" and "--" on a variable can be very useful in a program and save you some typing over "count = count + 1".

Except for some "\n"s in some of the "cout' statements the rest of the code in the for loop is OK.

One part you need to add is to give a value to "total10" and "vat10" before the for loop ends, as I did in the above code, and you try to print an uninitialized variable, which gave me a compiler error.

Not all variables need to be initialized, but some do. In this case, when I first started, "total10" and "vat10" are being used before they are given a value, so if the program would compile and run you would find a strange "garbage number" in your output. Adding the two lines near the bottom of the for loop also takes care of this problem.

The "return 0;" is not necessary for the program, but it is good form to put it in. It also helps as a break point so the console will stay open giving you a chance to read what is there before the program ends and the console window closes, as it does with my IDE.

Hope that helps,

Andy
Thanks guys. I got it to add the sum of vat and total in the loops. Really appreciate the help.
Topic archived. No new replies allowed.