problems with while loop and if statement....

hi, im new here. taking some classes in c++ and need some help with a problem. not asking you to do it for me as i read the rules. just need some kind of direction. problem has different rates for different amounts of registrants.
Input= amount of registrants Output= total number of people being registered, total charge, and average charge per person. But it has to calculate a total for each company and then add them all up. ex. 1-3 registrants=$150 4-9=100 and more than 10=90. im trying to use a while loop and an if statement. I know i need the while statement. Not sure if im using the if statement right. Please help....it will calculate the registrants in the loop fine, but i get "0" for the other totals...

#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::fixed;
using std::setprecision;

int main()
{
//declare variables
double chargePerCompany = 0.0;
int numOfRegistrants = 0;
int totalRegistrants = 0;
const double RATE1 = 150;
const double RATE2 = 100;
const double RATE3 = 90;
double totalCharge = 0.0;
double averageCharge = 0.0;


cout << "First Company's Registrants: ";
cin >> numOfRegistrants;

while (numOfRegistrants >= 0)
{
totalRegistrants = totalRegistrants + numOfRegistrants;
totalCharge = totalCharge + chargePerCompany;

cout << "Next Company's Registrants (Neg. Number to Stop): ";
cin >> numOfRegistrants;
} //end while


if (numOfRegistrants <=3 )
chargePerCompany = static_cast<double>(numOfRegistrants) * RATE1;
else if (numOfRegistrants <=9 )
chargePerCompany = static_cast<double>(numOfRegistrants) * RATE2;
else if (numOfRegistrants >=10 )
chargePerCompany = static_cast<double>(numOfRegistrants) * RATE3;

//end ifs

//calculate average price per registrant

averageCharge = totalCharge / static_cast<double>(totalRegistrants);



//display people registered, total charge, and the average charge per registrant
cout << fixed << setprecision (2) << endl;
cout << "Total People Registered: " << totalRegistrants << endl;
cout << "Total Charge of All Registrants: " << totalCharge << endl;
cout << "Average Charge Per Registrant: " << averageCharge << endl;


return 0;
} //end of main function
just to clarify, Im not asking for a solution, just for some guidance. I would really appreciate it...

thanks in advance,
Allan
Hello, I just killed a long post, so I give you the short version.

Try using an array for the numOfRegs

Use the while loop to fill the array numOfRegs

After that, use a for or while loop to
- Add numOfRegs[i] to TotalRegs
- Defnie the rate applicable
- calculate the chargeOfcompany with the above rate
- add the chargeOfCompany to TotalCharge

print it out


int main
In the while loop you ar trying to calculate the totalCharge. But the result is always gonna be 0 because you have the equation totalCharge = totalCharge + chargePerCompany; but the chargePerCompany is set to 0 double chargePerCompany = 0.0; which means that every time you add an amount of 0.
Try to calculate your totalCharge after you assign avalue to chargePerCompany.

[edit]
If you don't want to use an array you can just have a variable that acts as a counter in the while loop so you can know how many inputs the user entered and then calculate the totalCharge with that value.
totalCharge = companysCounter * chargePerCompany
Last edited on
int main, thanks for that. It's just that I'm havent gotten to arrays yet, and not sure how to make them. Only thing I have learned is for, while, if/else, nested selection. I wish i could use your advice though...thanks so much.
sorry, I edited my reply before i see that you posted something.
Last edited on
no problem, i see why its at zero, still having trouble on where to place the percompany charge.
I'm just reposting your code to get the line numbers :
The code at lines 36-41 where you calculate the chargePerCompany should really be inside the 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
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
#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::fixed;
using std::setprecision;

int main()
{
//declare variables
double chargePerCompany = 0.0;
int numOfRegistrants = 0;
int totalRegistrants = 0;
const double RATE1 = 150;
const double RATE2 = 100;
const double RATE3 = 90;
double totalCharge = 0.0;
double averageCharge = 0.0;


cout << "First Company's Registrants: ";
cin >> numOfRegistrants;

while (numOfRegistrants >= 0)
{ 
totalRegistrants = totalRegistrants + numOfRegistrants;
totalCharge = totalCharge + chargePerCompany;

cout << "Next Company's Registrants (Neg. Number to Stop): ";
cin >> numOfRegistrants;
} //end while


if (numOfRegistrants <=3 )
chargePerCompany = static_cast<double>(numOfRegistrants) * RATE1;
else if (numOfRegistrants <=9 )
chargePerCompany = static_cast<double>(numOfRegistrants) * RATE2;
else if (numOfRegistrants >=10 )
chargePerCompany = static_cast<double>(numOfRegistrants) * RATE3;

//end ifs

//calculate average price per registrant

averageCharge = totalCharge / static_cast<double>(totalRegistrants);



//display people registered, total charge, and the average charge per registrant
cout << fixed << setprecision (2) << endl;
cout << "Total People Registered: " << totalRegistrants << endl;
cout << "Total Charge of All Registrants: " << totalCharge << endl;
cout << "Average Charge Per Registrant: " << averageCharge << endl;


return 0;
} //end of main function 
Here is a working code. You have to check to find out that this is how you wanted the results to be.
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
#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::fixed;
using std::setprecision;

int main()
{
	//declare variables
	double chargePerCompany = 0.0;
	int numOfRegistrants = 0;
	int totalRegistrants = 0;
	const double RATE1 = 150.0;
	const double RATE2 = 100.0;
	const double RATE3 = 90.0;
	double totalCharge = 0.0;
	double averageCharge = 0.0;
	int companysCounter = 0;//Keeps the number of companys (user inputs)

	cout << "First Company's Registrants: ";
	cin >> numOfRegistrants;
	while (numOfRegistrants >= 0){ 
		totalRegistrants = totalRegistrants + numOfRegistrants;
		companysCounter++;
		cout << "Next Company's Registrants (Neg. Number to Stop): ";
		cin >> numOfRegistrants;
	} //end while

	if (numOfRegistrants <=3 ){//numOfegistrants is not correct here, you mean totalRegistrats or companysCounter 
		chargePerCompany = (totalRegistrants) * RATE1; //You should have totalRegistrants her instead of numOfRegistrants
	}else if (numOfRegistrants <=9 ){
		chargePerCompany = (totalRegistrants) * RATE2;
	}else if (numOfRegistrants >=10 ){
		chargePerCompany = (totalRegistrants) * RATE3;
	}
	//end ifs
	totalCharge = companysCounter * chargePerCompany; //the total charge is calculated after you assign a value to chargePerCompan
	//calculate average price per registrant

	averageCharge = totalCharge / static_cast<double>(totalRegistrants);

	//display people registered, total charge, and the average charge per registrant
	cout << fixed << setprecision (2) << endl;
	cout << "Total People Registered: " << totalRegistrants << endl;
	cout << "Total Charge of All Registrants: " << totalCharge << endl;
	cout << "Average Charge Per Registrant: " << averageCharge << endl;


	return 0;
} //end of main function 
Last edited on
Finally, i got the code working the way I intended, with the help of you guys. Thanks alot to Mitsakos, int main, and guestgulkan. I really appreciate the help you guys.

Thanks again,
Allan
Topic archived. No new replies allowed.