Write a loop that will accept numbers from a user as long as he wishes to enter numbers. Calculate the sum of all numbers that were entered and when the user exits the loop, display the sum and average of the numbers entered.
Missing a semi-colon. On a note on this code, the operation you used for sum and xNum creates an incorrect result for sum at the end. Might want to take a look at that.
As for taking the final -1 into account, you could throw in an if statement right under cin >> xNum; to break;if (xNum == -1);. The program still runs after you enter an input for xNum, there must be a way to stop the program from executing:
1 2 3
n= n + 1;
Or you could try this, while (cin >> xNum), which runs the while loop as long as the input matches the type of xNum (conversion applies).
The loop stops when you hit end-of file (ctrl+z enter), or when a condition within the loop explicitly stops the loop (an if statement perhaps).
1. Program prompts for a number.
2. User enters -1.
3. Program reads -1 into xNum.
4. The sum is incremented by xNum (so sum = sum - 1).
5. N is incremented by 1.
6. Loop condition fails, exit the loop.