Mar 10, 2016 at 10:52pm UTC
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
Mar 11, 2016 at 12:56am UTC
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 Mar 11, 2016 at 12:57am UTC
Mar 11, 2016 at 10:25am UTC
if (sum%8 ==0)
pizzas = sum / 8;
else
{
pizzas =sum/8;
pizzas=pizzas+1;
}
Mar 11, 2016 at 12:51pm UTC
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 Mar 11, 2016 at 12:52pm UTC
Mar 11, 2016 at 5:03pm UTC
The math you want is pizzas = (sllices+7) / 8;
Mar 11, 2016 at 8:01pm UTC
Thank you guys for the help! I turned it in and got a 100!