0

How is this program written?

How is this coded in a c++program Write a program that prompts the user to type a positive integer. When the user types a negative value the program outputs ERROR and asks for another value. When the user inputs 0 (zero) that means that the last value has been typed and the program must output the average of the positive integers. If the user has not input any values, the program should output 'NO AVERAGE'. [Hint Combine loops and if else statements and research about the break statement]

15th Jun 2018, 6:28 PM
juliet
4 Answers
+ 5
#include <iostream> using namespace std; int main() { int b = 0, average, sum = 0, input = -1; while (input != 0) { cout<<"Input a positive integer"; cin>>input; if (input < 0) cout<<"ERROR\nInput another integer"; else { sum += input; b++; } } average=sum/b; if (average == 0) cout << "NO AVERAGE"; else cout << average; }
15th Jun 2018, 6:36 PM
Hatsy Rei
Hatsy Rei - avatar
0
#include <iostream> using namespace std; int main() { int a,b,average,sum; cout<<"Input a positive integer"; cin>>a; average=sum/b; { if(a<0) cout<<"ERROR\nInput another integer"; cin>>a; else if (a>0) cout<<"Your average is"<<average; else cout<<"NO AVERAGE"; } while (a=0) { sum=sum+a; a++; if(a==0) break; } } this is my trial program
15th Jun 2018, 6:28 PM
juliet
0
look at this: https://code.sololearn.com/cFvnG63csfOo/?ref=app problems with your code: -you calculate the average before reading any values -you last while loop makes no sense -all of the stuff you need for your code seems to be there but in random order -you don‘t initialize your variables
15th Jun 2018, 6:55 PM
Max
Max - avatar
0
thanks 😀
15th Jun 2018, 7:33 PM
juliet