help with code

I am programming a code that will find the average number of boxes of cookies
sold by the children in a particular scout troop. I am having trouble creating a while loop that will reject numbers that are negative, unless it is -1. For example, if i were to input the numbers 8, 9, -10, -1, I want the code to find the average of the numbers without counting the negative number -10. Thanks.

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;

int main()
{
  int numBoxes, totalBoxes,numSeller;
    double averageBoxes;
    totalBoxes = 0; 
    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;

  while (numBoxes != -1)
    {
      totalBoxes = totalBoxes + numBoxes; 
      numSeller = numSeller + 1; 
       cout << "Enter number of boxes of cookies sold by the next seller: "; 
       cin >> numBoxes;
    }
      numSeller = numSeller - 1; 

    if (numSeller == 0)          
        cout << "\nNo boxes were sold.\n";
     else
    {  
        averageBoxes = (static_cast<double>(totalBoxes) / (numSeller));
          cout << "The average number of boxes sold by the " << numSeller << " sellers was " << averageBoxes << "." << endl;
    }


        return 0;
}


Last edited on
u can make an if statement that says if number < 0 ,
it will assign the number to 0 so it dont count.

1
2
if (number < 0)
       number = 0;
Last edited on
Hello amps1204,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


See what you can do while I straighten out your code and see what you have.

Andy
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
Thank you everybody :)
Topic archived. No new replies allowed.