I am almost done solving this problem but i need some help. Currently the input for numBoxes accepts negative numbers ie. -10 boxes of cookies. How do I add a while loop to validate that the input for number of boxes sold is -1 or greater.
ie. 10, -10, 24, -1 (to quit) returns:
The average number of boxes sold by the 3 sellers was 8.
but should really be returning: The average number of boxes sold by the 2 sellers was 17.
I tried added a nested while loop but its not working. Would appreciate it if someone could guide me to the right direction.
#include <iostream>
usingnamespace std;
int main()
{
int numBoxes, // Number of boxes of cookies sold by one child
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; // WRITE CODE TO INITIALIZE THE totalBoxes ACCUMLATOR TO 0 AND
numSeller = 1; // THE numSeller COUNTER TO 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;
// WRITE CODE TO START A while LOOP THAT LOOPS WHILE numBoxes IS NOT EQUAL TO -1, THE SENTINEL VALUE.
while (numBoxes != -1)
{
totalBoxes = totalBoxes + numBoxes; // WRITE CODE TO ADD numBoxes TO THE totalBoxes ACCUMULATOR.
numSeller = numSeller + 1; // WRITE CODE TO ADD 1 TO THE numSeller COUNTER.
cout << "Enter number of boxes of cookies sold by the next seller: "; // WRITE CODE TO PROMPT FOR AND INPUT THE NUMBER OF BOXES SOLD BY THE NEXT SELLER.
cin >> numBoxes;
}
numSeller = numSeller - 1; // WHEN THE LOOP IS EXITED, THE VALUE STORED IN THE numSeller COUNTER WILL BE ONE MORE THAN THE ACTUAL NUMBER OF SELLERS.SO WRITE CODE TO ADJUST IT TO THE ACTUAL NUMBER OF SELLERS.
/* testing code to make sure loop + counter work */
// cout << "Total Boxes of Cookies Sold: " << totalBoxes << endl;
// cout << "Number of Sellers: " << numSeller << endl;
if (numSeller == 0) // If true, -1 was the very first entry
cout << "\nNo boxes were sold.\n";
else
{ // WRITE CODE TO ASSIGN averageBoxes THE COMPUTED AVERAGE NUMBER OF BOXES SOLD PER SELLER.
averageBoxes = (static_cast<double>(totalBoxes) / (numSeller));
// WRITE CODE TO PRINT OUT THE NUMBER OF SELLERS AND AVERAGE NUMBER OF BOXES SOLD PER SELLER.
cout << "The average number of boxes sold by the " << numSeller << " sellers was " << averageBoxes << "." << endl;
}
return 0;
}
while (numBoxes != -1)
{
if( numBoxes > 0 ) // ok, this value is fine
{
totalBoxes = totalBoxes + numBoxes;
numSeller = numSeller + 1;
cout << "Enter number of boxes of cookies sold by the next seller: "; // WRITE CODE TO PROMPT FOR AND INPUT THE NUMBER OF BOXES SOLD BY THE NEXT SELLER.
cin >> numBoxes;
}
else // entered some non-positive value other than -1
{
cout << "number of boxes must be at least one\n. please renter a positive value\n."
<< "(or enter -1 to signal end of input)\n" ;
}
}
ie. 10, 10, 24, -1 (to quit) returns:
The average number of boxes sold by the 3 sellers was 8.
but should really be returning: The average number of boxes sold by the 2 sellers was 17.
Shouldn't the average be 14.67?
1 2 3 4 5 6 7 8
while (numBoxes != -1)
{
totalBoxes = totalBoxes + numBoxes; // WRITE CODE TO ADD numBoxes TO THE totalBoxes ACCUMULATOR.
numSeller = numSeller + 1; // WRITE CODE TO ADD 1 TO THE numSeller COUNTER.
cout << "Enter number of boxes of cookies sold by the next seller: "; // WRITE CODE TO PROMPT FOR AND INPUT THE NUMBER OF BOXES SOLD BY THE NEXT SELLER.
cin >> numBoxes;
}
The problem is that you're accumulating and incrementing regardless of the value of numBoxes. A simple fix would be to add an if statement like so:
1 2 3 4 5 6 7 8 9 10 11 12
while (numBoxes != -1)
{
if (numBoxes >= 0) {
//totalBoxes = totalBoxes + numBoxes;
totalBoxes += numBoxes; // prefer to write in shorthand form
//numSeller = numSeller + 1;
++numSeller;
}
cout << "Enter number of boxes of cookies sold by the next seller: ";
cin >> numBoxes;
}
You also seem to repeat code (line 17, 18 and line 26, 27), which is something you should try to avoid. Consider using a do-while loop to fix this.
1 2 3 4 5 6 7 8
int numBoxes, // Number of boxes of cookies sold by one child
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; // WRITE CODE TO INITIALIZE THE totalBoxes ACCUMLATOR TO 0 AND
numSeller = 1; // THE numSeller COUNTER TO 1.
Also, I'd recommend that you declare and initialise variables simultaneously.