My assignment is this: Create a C++ console application that uses a while loop to count, total, and average a series of positive integers entered by a user. The user enters a –1 to signal the end of data input and to display the count, total, and average of the numbers entered.
Im almost there! here is my code:
//Specification: Find total, sum, and average of user input using While Loop
#include <iostream>
using namespace std;
int main(){
int userInput = 0;
int const endData = -1;
cout << "This program will count, total, and average your numbers\n";
cout << "Please enter a number, enter -1 to display results and exit\n";
cin >> userInput;
//while loop
int totalNums = 0;
int sum = 0;
double average = 0;
int sentinalValue = endData;
int loopControlVariable = userInput; //prime the loop
while (loopControlVariable != sentinalValue){
cin >> loopControlVariable;
totalNums = totalNums + 1;
sum = sum + loopControlVariable;
average = sum / totalNums;
}
cout << "Youve entered " << totalNums << " numbers\n";
cout << "The sum of these numbers is " << sum << endl;
cout << "The average of these numbers is " << average << endl;
return 0;
}
My problem is the sum of the numbers...It isnt adding up right and i dont know why..i think it might be skipping the number just above -1 (which ends the loop)
can anyone push me in the right direction here?

New Topic/Question
Reply



MultiQuote








|