I am working on an assignment that revolves around calculating the average boxes a seller sells.
My main question is is that starting in line 22, if the value inputted is a negative (not -1) then it is counted as a seller and becomes part of the final answer. How would i code it so that it ignores a value like -10 with a
'while' statement? If you can include your logic with that too, that'd be most appreciated!
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
int numBoxes; // Number of boxes of cookies sold by one child
double totalBoxes, // Accumulates total boxes sold by the entire troop
numSeller; // Counts the number of children selling cookies
double averageBoxes; // Average number of boxes sold per child
totalBoxes = 0;
numSeller = 1;
cout << " **** Cookie Sales Information **** \n\n";
// Get the first input
cout << "Enter number of boxes of cookies sold by seller " << numSeller
<< " (or -1 to quit): ";
cin >> numBoxes;
while (numBoxes != -1)
{
totalBoxes += numBoxes;
++numSeller;
cout << "\nEnter number of boxes of cookies sold by seller " << numSeller
<< " (or -1 to quit): ";
cin >> numBoxes;
}
numSeller--;
if (numSeller == 0)
cout << "\nNo boxes were sold.\n";
else
{
averageBoxes = totalBoxes / numSeller;
cout << "\nThere were " << numSeller << " sellers and they sold an average of " << fixed << setprecision(1) << averageBoxes << " each.\n";
}
return 0;
}
While that would be correct, my assignment does ask for "appropriate while statements". And although I could be going down with a bad case of Occoms Razer, is there a way to use a 'while' statement for this?