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;
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
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.
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.
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.
#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;
constdouble RATE1 = 150.0;
constdouble RATE2 = 100.0;
constdouble 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
}elseif (numOfRegistrants <=9 ){
chargePerCompany = (totalRegistrants) * RATE2;
}elseif (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
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.