Am I missing something in my while loop?

I'll give the instructions first:

Write a C++ program that has a while-loop to loop indefinitely until an input of a value of -999 (a sentinel value). Hence, prompt user to enter an integer value and repeat as long as it is not -999.

In the body of the loop, keep a total of the values entered and a count of how many values entered. When exit the loop, calculate the average. Display the average (to 2 decimal places) and also the count of how many scores entered.

Set up your while loop to have the initial read outside the loop and then subsequent reads in the loop body.


This is what I have in my while loop so far. This is the loop that allows the user to enter a number until -999 is entered. That's the easy part.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
    double total = 0;
    int number;

    cout << "Enter a number: ";
    cin >> number;

    while (number != -999)
    {
        total += number;
        count++;
        cout << "Enter a number: ";
        cin >> number;
    }



What I think I need to do next is add a for loop, but then I was thinking about what the test expression would be. The directions don't say to have a set amount of values, so I'm not sure what I would put for the test expression, if a for loop is even the correct answer.

If I do add a for loop, would the accumulator and increment operator go within that loop, instead of the while loop?

To display the average would be total / number, correct?

And where it says to also display the count of how many scores entered, is that talking about all the times "Enter a number: " is displayed, or a separate cout statement?
Last edited on
What I think I need to do next is add a for loop

What makes you think so?


Why is 'total' a 'double'?
What is the type of 'count'?
What is the value of 'count' before the while-loop?


Input can fail. For example, the user might type text and text is not a number. all following inputs would fail too and the loop would never end. Therefore, it is possible to do more with less:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// your version
cout << "Enter a number: ";
cin >> number;
while (number != -999)
{
  total += number;
  count++;
  cout << "Enter a number: ";
  cin >> number;
}

// different version
cout << "Enter a number: ";
while ( cin >> number && number != -999 )
{
  total += number;
  ++count;
  cout << "Enter a number: ";
}

What does happen there?

The condition of the loop is more complex. It has two substatements and a logical AND (&&).
In order to evaluate that, the left side (cin >> number) is evaluated first. The formatted input is carried out, and the statement returns the cin.
The cin evaluates to true or false depending on its state.
http://www.cplusplus.com/reference/ios/ios/operator_bool/
If the formatted input did succeed, the left substatement is true.
If the formatted input did fail, the left substatement is false and the number is not valid.

If the left side is false, the && does not even evaluate the right side and simply returns false, and the loop ends.
If the left side is true, then the right side is evaluated, and looping depends on it just like in your version.
For example, the user might type text and text is not a number.


I'm simply doing what we've been taught so far. Yes, text can definitely be input, but with these practice programs, I don't think my professor is thinking that far ahead. Nonetheless, I should be thinking that far ahead, so thank you.

Why did I think a for loop was necessary? I don't know. Really, I'm still trying to get the hang of what loops to use at which time.

I had made 'total' a 'double' because he wants the average displayed with two places after the decimal. Are you saying I could keep it an 'int' and use '<iomanip>' to format it?

I had added 'int' to 'count' right after I posted this, so it's already there. I had initialized it to 1.
Re-reading the instructions, it says to have the initial read outside of the loop, so I will keep it as it is.
I see that my main problem is not knowing when to use, or not use, the for loop. It always seems like I'm trying to use the for loop. I should use it when I know the number of iterations that needs to be performed.
A problem I'm running into is when the average is displayed, it's not showing correctly.

For example, I enter the number 2 five times for a value of 10, then I enter -999 to quit the program. I correctly displays the value, but the average is showing -.01.
@LullaBelle

To display the average would be total / number, correct?


No, that's not correct. You would divide by your variable, count. Remember, you're asking for inputs with variable number, and to finish the inputs, you enter -999. Even if you're not adding it to total, you still would be using it to divide the total.

1
2
3
4
5
cout << "Enter a number: ";
// You type 2
// We do it 5 times
// Total = 10
// Total / count (5) == 2 .. average 
Yes, I figured that out right after I posted that. I took another look at the program and realized it was count, thank you.
I had initialized it to 1.

What if the user types -999 right of the bat? That is, 0 values. What does the count show then? 1
Ok, next time the user gives one good number and then -999. The body of the loop is executed once, isn't it? Your count++; occurs and thus the count==2 after the loop.
Do you notice a problem.


I had made 'total' a 'double' because he wants the average displayed with two places after the decimal. Are you saying I could keep it an 'int' and use '<iomanip>' to format it?

The 'total' is not the 'average', is it? The <iomanip> won't make decimals out of integer.

There is a problem though. If you calculate total / count and both are int, then the outcome is int too. That would discard the remainder. Thus, total as double is ok for your level.
What if the user types -999 right of the bat? That is, 0 values. What does the count show then? 1
Ok, next time the user gives one good number and then -999. The body of the loop is executed once, isn't it? Your count++; occurs and thus the count==2 after the loop.
Do you notice a problem.


I see, it should be initialized to 0. So if the user inputs -999 first, it won't count.
Thank you both for the help. I have two more programs to go through (kill me now). If I have any questions, should I continue here, or make a new post?
@LullaBelle

You always start a new thread IF you are referring to a different program, and continue the thread IF the new questions pertain to that original program. Otherwise, it's hard to keep track of what answer would pertain to which problem
Topic archived. No new replies allowed.