Hello amps1204,
Some little things to help get you started.
It is ALWAYS a good idea to initialize your variables when you define them.
1 2 3 4 5 6
|
int main()
{
int numBoxes{}, totalBoxes{}, numSeller{ 1 };
double averageBoxes{};
//totalBoxes = 0;
//numSeller = 1; // THE numSeller COUNTER TO 1.
|
The uniform initializer, the {}s available from C++11 on, empty will initialize the variable to zero or like "numSeller" to 1. This will eliminate the need for the last two lines.
Also any variable that is a "total" or "sum" must be initialized before it is used, so that you are adding to zero and not a garbage value.
Your while loop:
1 2 3 4 5 6 7 8 9 10 11 12
|
while (numBoxes != -1)
{
if (numBoxes > 0)
{
totalBoxes += numBoxes;
numSeller++; // numSeller = numSeller + 1;
}
cout << "Enter number of boxes of cookies sold by the next seller " << numSeller << " : ";
cin >> numBoxes;
}
|
To use an uninitialized variable your code
totalBoxes = totalBoxes + numBoxes;
might look something like this to start with:
-858993460 = -858993460 + 10;
. Not what you want when the code is finished because "totalBoxes would then be
-858993450
. Only 10 numbers closer to zero.
Line 5 is the shorter way of adding to a total. And line 7 is the shorthand for adding 1 to a variable. And the opposite is "--".
I am not sure if the if statement work the way that you would like, but it does pass over negative numbers.
The program could be done in several different ways depending on what you want to average and how you go about it.
In this part:
1 2 3 4 5 6 7 8 9 10
|
if (!numSeller) //(numSeller == 0)
{
cout << "\nNo boxes were sold.\n";
}
else
{
averageBoxes = (static_cast<double>(totalBoxes) / numSeller);
cout << "\nThe average number of boxes sold by the " << numSeller << " sellers was " << averageBoxes << "." << endl;
}
|
Line 1 is a shorter way of writing what is commented out.
In line 7 you do not need the () around "numSeller".
Andy