Write a Program that asks the user to input a positive integer. If the user inputs a negative number, program should output "ERROR" and ask for /the user's input again. If the user inputs the value of 0, the program says "NO AVERAGE". If the user inputs a -1, the program takes the sum and average of only the positive integers, then outputs "The average is _____ "

I dont understand what I'm doing wrong

//THIS IS WHAT I HAVE SO FAR
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

int main()
{
int number;
double sum, average;
cout<<"Type Positive Integers: ";
cin>> number;
sum += number;
average = sum/=number;

while(number>=1){
    cout<<"Type Positive Integers: ";
    cin>>number;
    if(number==0){
    cout<<"NO AVERAGE: ";
    }
    if(number<=-2){
        cout<<"ERROR!"<< "Enter Another Value: ";
        cin>> number;
    }
    if(number== -1){
        cout<<"The Average of the Positive Integers is :"<< average << endl;
    }
}

Recommended Answers

All 2 Replies

How about changing line 19 to test for not equal -1 (your exit condition)?

And drop line 27 since you will loop around to line 21 for another input.

While this current code omits counting how many valid numbers were input, you can start by fixing up a few basic things then add your input counter later.

Your specs don't make sense. A negative number results in ERROR but -1 performs an action. Last I checked, -1 was a negative number. Do we take the spec from your text or from your code? They can't both be correct.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.