Beginner Problems. C++ Assignment

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;

}

if (persons >= 10)
totalcharges = charges * 90;
else if (persons >= 4)
totalcharges = charges * 100;
else
totalcharges = charges * 150;


//end if
average = totalcharges / totalpersons;



//display total
cout << " Total Guests Attending: " << totalpersons << endl;
cout << " Total Charges: " << totalcharges << setprecision(2) << endl;
cout << " Average: " << average << endl;

system("pause");
return 0;
} //end
Well, after this loop:

1
2
3
4
5
6
7
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;

}

if (persons >= 10)
totalcharges = charges * 90;
else if (persons >= 4)
totalcharges = charges * 100;
else
totalcharges = charges * 150;


//end if
average = totalcharges / totalpersons;



//display total
cout << " Total Guests Attending: " << totalpersons << endl;
cout << " Total Charges: " << totalcharges << setprecision(2) << endl;
cout << " Average: " << average << endl;

system("pause");
return 0;
} //end

_____________
i know my logic is messed up somewhere...im been working on it a while. I think my brain is just tired.
Please use code tags: [code]Your code here[/code]

If I understand it right it must be like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
while (persons > 0)
{
  if (persons >= 10)
    totalcharges += persons * 90;
  else if (persons >= 4)
    totalcharges += persons * 100;
  else
    totalcharges += persons * 150;

  totalpersons += persons;

  cout << " Next amount(0 to stop): ";
  cin >> persons;
}
Hey that worked ^_^

Ok, sorry, ill use the [code] thing from now on im sorry. But thanks for the help. I appreciate it.
Topic archived. No new replies allowed.