With this simple program I am trying to make -- I need to use a while loop to take input from the user until they don't want to input anymore and then calculate the average of what they are entering(they are entering numbers).
So far - I'm having trouble getting my loop to work properly... If I'm not mistaken I think my while loop is crappily set up, but I'm not sure how to improve it... So what I'm asking you all - is if there is a better way to set up this loop - or if my seemingly easy way out works well enough!
The code works fine - I'm just thinking that my loop is badly set up, is it?
Code:
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float number = 0;
int numbers = 0;
float sum = 0;
float average = 0;
char answer = 'y';
while(answer == 'y'){
cout <<"Enter a number: ";
cin >> number;
numbers++;
sum += number;
cout <<" Do you want to input another number? (y/n) ";
cin >> answer;
answer = tolower(answer);
}
average = sum / numbers;
cout <<"\nThe average of all the numbers you entered is: ";
cout << setprecision(3) << average;
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}