I'm still stuck on this one question, can someone help me with this?

4.) Write a program that asks the user to type positive integers. When the user types a negative value the program writes ERROR and asks for another value. When the user types -1 that means that the last value has been typed and the program must write the average of the positive integers. If the number of typed values is zero the program writes 'NO AVERAGE'.

This is the code that I came up with, and I do not know how to add the no average statement, also when I try to take the average it comes out as a decimal. Like I typed all 8's and the average came out as 6.3 something with about 5 decimal places. Thank you for the help in advance.

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main(){
    int number, count = 0;
    float sum, average;

    cout<<"Enter a Positive Integer: ";
    cin>>number;
    while(number!=-1){

        if(number>=1){
            cout<<"Enter a Positive Integer: ";
            cin>>number;
            sum+=number;
            count++;
        }
        else{
            cout<<"ERROR!\n"<<"Enter a Positive Integer: ";
            cin>>number;
        }
        if(number==-1){
        average=sum/count;
        cout<<"The Average of the Positive Integers is: "<< average<<endl;
    }
}
}

Dani AI

Generated

— the weird average and missing "NO AVERAGE" come from a few simple bugs. First, sum was never initialized, so it held garbage. Second, your code reads a new number before adding the previous one, which skips inputs and can accidentally add -1. Third, you compute the average inside the loop instead of after finishing input. was right to point out testing count > 0 — do that after the loop.

Use this safe pattern: initialize sum and count to zero; repeatedly read a value; if it is -1 break; if it is nonpositive (or negative-but-not--1) print ERROR and continue; otherwise add to sum and increment count. After the loop, if count == 0 print NO AVERAGE; otherwise compute double avg = (double)sum / count and print it. Use a wider type for sum (e.g., long long) to reduce overflow risk and double for the average if you want fractional results.

#include <iostream>
#include <iomanip>

int main() {
    long long sum = 0;
    int count = 0;
    int n;

    while (true) {
        std::cout << "Enter a Positive Integer: ";
        if (!(std::cin >> n)) return 0;
        if (n == -1) break;
        if (n <= 0) { // zero is not positive
            std::cout << "ERROR\n";
            continue;
        }
        sum += n;
        ++count;
    }

    if (count == 0) {
        std::cout << "NO AVERAGE\n";
    } else {
        double avg = static_cast<double>(sum) / count;
        std::cout << "The Average of the Positive Integers is: "
                  << std::fixed << std::setprecision(2) << avg << '\n';
    }
    return 0;
}

Quick debug tips: add cout of sum and count before computing the average to see what was accumulated. If you want no decimal places when the average is integral use setprecision(0) or cast to int before printing.

Recommended Answers

All 2 Replies

If the number of typed values is zero the program writes 'NO AVERAGE'.

After line 25 you would have a test for what count is. Not using the code style since this is not code:
if count > 0 then (what's on 26 and 27)
else no average.

commented: For some reason my average keeps coming out incorrect. +0

" I do not know how to add the no average statement " is what I addressed.

As to the wrong answer, test your code with a cout of what the values are at the time of count and sum.
Be aware you did not initialize sum so in some compilers that could be a random number. As to average, no need to initialize.

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.