Below is my code for an average of boxes sold per seller. I always get 3 regardless of the amount of boxes or amount of sellers I enter.
#include <iostream>
using namespace std;
int main()
{
int numBoxes, // Number of boxes of cookies sold by one child
totalBoxes = 0, // Accumulator - Accumulates total boxes sold by the entire troop
numSeller = 1; // Counter - Counts the number of children selling cookies
double averageBoxes; // Average number of boxes sold per child
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)
{
cout << "Please input the number of boxes sold by the next seller (or -1 to quit): ";
cin >> numBoxes;
totalBoxes += numBoxes; // Accumulates the running total
numSeller++;
}
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.
if (numSeller == 0)
cout << "\nNo boxes were sold.\n\n";
else
{
averageBoxes = (totalBoxes / numSeller);
cout << "\n\nThe average number of boxes sold per seller is: " << averageBoxes << endl;
}
return 0;
}