-2

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;
}
jpw
  • 44,361
  • 6
  • 66
  • 86
  • 5
    Use your debugger to detect your bug. Don't ask on SO first. – πάντα ῥεῖ Jun 21 '15 at 21:15
  • 1
    Please provide a more complete "question" before posting your code. Tell us what steps you've taken to try and solve this. Ask a very clear, concise, and direct question. Don't just post code and say "it doesn't work". – Cloud Jun 21 '15 at 21:20

1 Answers1

1

You are running into two issues:

  1. You aren't backing up the first value stored in numBoxes, so there is data loss.
  2. You are encountering issues due to integer division (ie: truncation errors).

Here are some important items you should read on the subject of integer division.

Division in C++ not working as expected

What is the behavior of integer division?

Dividing two integers to produce a float result

C++ Best way to get integer division and remainder

Here's the fixed code. Only two one-liner fixes were needed.

Code Listing


#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;
    totalBoxes += 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--;
    // 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 = ((double)totalBoxes / (double)numSeller);
        cout << "\n\nThe average number of boxes sold per seller is: " << averageBoxes << endl;

    }

    return 0;
}
Community
  • 1
  • 1
Cloud
  • 18,753
  • 15
  • 79
  • 153