Im learning C++. And I am doing a problem in the book im reading to learn it ( introduction to Programming with C++)
Im currently learning to use while loops and if statements.
In the program I am trying to calculate how many people are attending , and depending how big each group is, each group pays a different amount. and also I need the average. My problem is that everyone is paying 150 even if there group is higher than 4 (which pays 100) or 10 ( which only pays 90). I am truly confused.
Any help is very much appreciated.
_________________________________________________________________________
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//declare
int totalpersons = 0;
int persons = 0;
int charges = 0;
double totalcharges = 0.0;
double average = 0.0;
//enter
cout << " Enter the amount of persons attending: ";
cin >> persons;
while (persons > 0)
{
totalpersons += persons;
charges += persons;
cout << " Next amount(0 to stop): ";
cin >> persons;
while (persons > 0)
{
totalpersons += persons;
charges += persons;
cout << " Next amount(0 to stop): ";
cin >> persons;
}
"persons" must be less than or equal to zero (because the loop terminated). Thus, the else statement will always be executed. Maybe you should ask the user if they want to continue (y/n) that way the "persons" variable won't be overwritten.
I also don't know if your logic is quite right. You ask for the amount of persons in a loop, so if the user selects 5, you add 5 people, if they select 7, then you add 7 people, until they want to stop. Shouldn't the user just select the total amount of people and be done with it (so no use for the while loop)? On top of this, your check afterwards is on "persons", not "totalpersons" so it theoretically only operates on the last number the user entered, not the total amount of people (which I think is what you wanted).
The loop exists because the people are coming in groups. Each group will pay a certain amount depending on how many show up for there group, if that makes sense. I can now get the totalpersons to calculate but for some reason no matter how many people show up in a group they all pay the 150, instead of the discounted price for larger groups.
Your code is not calculating it per group. If that's the case, you would need to do the calculation for the current group within the while loop. After the user enters 0, you should only output summary statistics.
yeah thats where im confused =(. Im only supposed to use the while and if statements. I have everything working except for the price part. How would I go about making it so it calculates it differently depending on how many people? Im only supposed to use the if and while statements.. and this is for a unlimited amount of groups . so 1 to whatever. its not preassigned. this is what i have now.
_____________________
// Chapter 7
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//declare
int totalpersons = 0;
int persons = 0;
int charges = 0;
double totalcharges = 0.0;
double average = 0.0;
//enter
cout << " Enter the amount of persons attending: ";
cin >> persons;
while (persons > 0)
{
totalpersons += persons;
charges += persons;
cout << " Next amount(0 to stop): ";
cin >> persons;