Answer to Question #168221 in C++ for sara

Question #168221

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.


1
Expert's answer
2021-03-05T01:10:25-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS