Ignoring a value with a while statement

I am working on an assignment that revolves around calculating the average boxes a seller sells.

My main question is is that starting in line 22, if the value inputted is a negative (not -1) then it is counted as a seller and becomes part of the final answer. How would i code it so that it ignores a value like -10 with a
'while' statement? If you can include your logic with that too, that'd be most appreciated!

Thank you!

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
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{   
	int numBoxes;    // Number of boxes of cookies sold by one child
  double totalBoxes,       // Accumulates total boxes sold by the entire troop
       numSeller;        // Counts the number of children selling cookies
       
   double averageBoxes;  // Average number of boxes sold per child
   
   totalBoxes = 0;
   numSeller = 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 += numBoxes;
	   ++numSeller;

	   cout << "\nEnter number of boxes of cookies sold by seller " << numSeller
		   << " (or -1 to quit): ";
	   cin >> numBoxes;
   }

   numSeller--;
   
   if (numSeller == 0)
      cout << "\nNo boxes were sold.\n";
   else
   { 
		 averageBoxes  = totalBoxes / numSeller;

	   cout << "\nThere were " << numSeller << " sellers and they sold an average of " << fixed << setprecision(1) << averageBoxes << " each.\n";
   }
   return 0;
}




You can do
1
2
3
4
5
6
7
while (numBoxes != -1)
{
	if (numBoxes >= 0)
	{
		// do your normal stuff
	}
}
or
1
2
3
4
5
6
7
8
9
while (numBoxes != -1)
{
	if (numBoxes < 0)
	{
		continue;
	}
	
	// do your normal stuff
}
Last edited on
While that would be correct, my assignment does ask for "appropriate while statements". And although I could be going down with a bad case of Occoms Razer, is there a way to use a 'while' statement for this?
Topic archived. No new replies allowed.