Write a program that asks the user to input arbitrary amount of numbers. Use a while loop to display the average of those numbers each iteration. Your while loop should break when the user inputs -1.
#include <iostream>
using namespace std;
int main(){
float n = 0, count = 0, sum = 0;
cout<<"Input number and press enter to continue\n";
while(cin>>n){
if(n != -1){
count++;
sum += n;
cout<<"Average so far: "<<sum/count<<endl;
}
else {
cout<<"You entered -1.";
break;
}
}
return 0;
}
Comments
Leave a comment