-4

needing some help on how to find the average number of boxes of cookies sold by children using a counter, accumulator, and an end sentinel (basic programming hwk, yay!). I'm having an issue with figuring out why the averageBoxes prints out the wrong number.

For example:

Program result prints out -5 averageBoxes when that's clearly not right...

{
    int numBoxes,
        // Number of boxes of cookies sold by one child
        totalBoxes = 0,   // Accumulates total boxes sold by the entire troop
        numSeller = 1;    // Counts the number of children selling cookies

    double averageBoxes; // Average number of boxes sold per child

                         // Write code to initialize the totalBoxes accumulator to O and
                         // The numSeller counter to 1.

    cout << "**** Cookie Sales Information ****\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)
    {
        // Write code to add numBoxes to the totalBoxes accumlator.
        // Write code to add 1 to the numSeller counter.

        totalBoxes += numBoxes;
        numSeller++;

        // Write code to prompt for and input the number of boxes
        // sold by the next seller.

        cout << "Enter the number of boxes sold by girl scout # " << numSeller << ": ";
        cin >> numBoxes;
    }

    // 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.

    numSeller = numSeller - 1;

    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.
       // Write code to print out the number of sellers and average number
       // of boxes sold per seller.

        averageBoxes += numSeller / numBoxes;

        cout << "The average number of boxes sold by the " << numSeller << " sellers was " << averageBoxes << endl;
    }

    return 0;
}

It's probably an easy fix but any help would be appreciated, thanks!

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
Frankie
  • 1
  • 1
  • 3
  • 1
    Can you share an example input, the output you're getting and the output you're expecting? – Mureinik Nov 15 '15 at 07:14
  • Hi, the output I'm getting should be in the link above, under "For Example:" The output I'm expecting should say that "The average number of boxes sold by the 5 sellers was 39.8" – Frankie Nov 15 '15 at 07:16
  • Your final math equation is wrong. Change `averageBoxes += numSeller / numBoxes;` to `averageBoxes = totalBoxes / numSellers;` – Tot Zam Nov 15 '15 at 07:22

1 Answers1

1
  1. You left double averageBoxes; uninitialized (it has an indeterminate value and doing += to it will produce nothing more than another indeterminate value).

  2. You should cast either of the operands (or both) to a floating-point type, so the result of numSeller / numBoxes is a floating-point type:

    averageBoxes += numSeller / static_cast<double>(numBoxes);
    

Edit:

Ok, you used the wrong variables for the above expression (it's easy to do so thanks to their names). It should be:

averageBoxes += totalBoxes / static_cast<double>(numSeller);

And maybe just don't declare your variables on the top of your program (without giving them a value). Declare them, where you first use them:

// += wasn't needed in any case
double averageBoxes = totalBoxes / static_cast<double>(numSeller);
LogicStuff
  • 19,397
  • 6
  • 54
  • 74