Write a complete C++ program that reads the attached file (values.txt) to find and print the following:
a. The average value of the numbers in the file.
b. The maximum and minimum value
c. The count of negative and positive values (ignore zero)

#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
    int count=0,sum=0,poscount=0,negcount=0;
    double avg,max,min;

    cout<<" Enter any integer number: ";
    cin>>count;

    while (count != 0)
    {
    {
        sum += count;
        avg = sum / static_cast <double> (count);
        count++;
    }

        if (count < 0)
        {
            negcount++;
        }
        if (count > 0)
        {
            poscount++;
        }
        max = count;
        if (count > max)
            max = count;
        min = count;
        if (count < min)
            min = count;
    }
    cout<<" The average value of the numbers is: "<<avg<<endl;
    cout<<" The maximum number is:               "<<max<<endl;
    cout<<" The minimum number is:               "<<min<<endl;
    cout<<" There is          "<<poscount<<" positive numbers "<<endl;
    cout<<" There is          "<<negcount<<" negative numbers "<<endl;
    return 0;
}

when I debug it , it just saying " enter any integer number " and I don't know is my code right? or there is something wrong..!

Recommended Answers

All 5 Replies

you need to replace lines 8 and 9 with code that opens and reads a text file. Do you know how to read files?

Also, delete line 2, there is no reason to include cstdlib

A few obvious things.
You need to have the input statements (lines 9,10) repeated at the end of the loop to get the next input from the user (insert after line 33).

Don't increment count in the loop, if it is to be your input value. Personally, I'd use a different name for the input, count pretty much implies a count of values, not the values themselves.

min and max need to be set to the very first value read in - I'd set them to the input value you read before entering the loop. Some move lines 28 and 32 to line 11.

You need to have the input statements (lines 9,10) repeated at the end of the loop to get the next input from the user (insert after line 33).

The data is supposed to come from a file, not the keyboard.

AD, OK, whatever the input source, there needs to be a read before the loop and at the end of it.

Or better, especially since this is file input that may contain 0, the input should be the while loop condition.

ifstream fin ("something.txt" );
int num;

while ( fin >> num )
{
    //do something with num
}

Yes I know how read files and Thank you all.

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.