C++ Homework Question

I have to find the number of pizzas, given the number of slices.

Here is my code:

#include<iostream>
#include<cstdlib>
#include<iomanip>

using namespace std;

int main()
{
int slices, pizzas, people = 0, sum = 0;
double average;
string name;

cout<<"Enter your name, or press Q or q to quit: ";
cin>>name;


while(name != "q" && name != "Q")
{

cout<<"Slices?: ";
cin>>slices;
people++;
sum = sum + slices;
cout<<"Enter another name or Q/q to quit: ";
cin>>name;

}

cout<<endl<<endl;
cout<<"Total Number of slices: "<<sum<<endl;
cout<<"Number of people: "<<people<<endl;

if(sum % 8 != 0)
pizzas = ;// THIS IS THE LINE I NEED HELP WITH
else if(sum % 8 == 0)
pizzas = sum / 8;

cout<<"You must purchase: "<<pizzas<<" pizzas."<<endl;

average = (double)sum / people;
cout<<fixed<<showpoint<<setprecision(2)<<"The average slices per person is: "<<average<<endl;


system("PAUSE");
return 0;
}


The line in the if statement is the one I need help with. Thanks
Please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/

I would change the order of your tests.

The first question is, how many whole pizzas will be eaten?
The additional question is, do you need one more pizza?
To answer question 1, it depends on how many slices are entered. For example, if person "A" eats 4, person "B" eats 3, and person "C" eats 5, thats 12 slices, but you need to buy whole pizzas(8 slices each), so you would need 2 pizzas. I need help on getting that number to round up.
Last edited on

if (sum%8 ==0)
pizzas = sum / 8;
else
{
pizzas =sum/8;
pizzas=pizzas+1;
}
Less (code) would be enough.
The first question is, how many whole pizzas will be eaten?
pizzas = sum / 8;
The additional question is, do you need one more pizza?
1
2
3
if ( /*there is remainder*/ ) {
  ++pizzas;
}
Last edited on
The math you want is pizzas = (sllices+7) / 8;
Thank you guys for the help! I turned it in and got a 100!
Topic archived. No new replies allowed.