Search code examples
c++sumaveragemean

Read a .txt file, calculate sum and mean


I have a .txt file which contains numbers and looks like this:

1
2
3

etc

The numbers are unimportant but they all start on a new line.

I want to then find the sum & mean of the numbers in the text file.

Here's what I have so far:

#include<cmath>
#include<cstdlib>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;

int main(int argc, char * argv[])
{
    std::fstream myfile("numbers.txt", std::ios_base::in);

    float a;
    while (myfile >> a)
    {
        printf("%f ", a);
    }

    getchar();

    return 0;

int sum(0);
int sumcount(0);
double average(0);
int even(0);
int odd(0);

ifstream fin;

string file_name;

int x(0);

cout<<"numbers.txt";
cin>> file_name;
fin.open(file_name.c_str(),ios::in);

if (!fin.is_open())
{
    cerr<<"Unable to open file "<<file_name<<endl;
    exit(10);

}

fin>>x;
    while (!fin.fail())
    {
        cout<<"Read integer: "<<x<<endl;
        fin>>x;
        sum=sum+x;
        sumcount++;
        if(x%2==0)
            even++;
        else
            odd++;

    }

    fin.close();
    average=(double)sum/sumcount;
    cout<<"Sum of integers: "<<sum<<endl;
    cout<<"Average: "<<average<<endl;
    cout<<"Number of even integers: "<<even<<endl;
    cout<<"Number of odd integers: "<<odd<<endl;

    return 0;
}

The start loads the numbers but it won't execute the next step.

i have looked at a lot of other code and tried to implement other ideas to solve the problem; however, some people use loops and arrays and other things and I'm not sure which to use.

How can I get my program to find the sum and mean of numbers in the file?

also any other links for help would be appreciated

EDIT: although the numbers are intergers the mean may not be


Solution

  • Here is your working code.

    #include <cmath>
    #include <cstdlib>
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <fstream>
    
    
    using namespace std;
    
    int main(int argc, char * argv[])
    {
        std::fstream myfile("numbers.txt", std::ios_base::in);
    
        float a = 0;
    
        myfile >> a;
    
        while (!myfile.fail())
        {
            printf("%f ", a);
            myfile >> a; // here you dispay your numbers
        }
    
        getchar(); // waiting for input
    
        float sum(0);
        int x(0);
        int sumcount(0);
        double average(0);
        int even(0);
        int odd(0);
    
        ifstream fin;
    
        string file_name;
    
        cout<<"numbers.txt" << endl;
    
        cin>> file_name; // waiting for enter file name
    
        fin.open(file_name.c_str(),ios::in);
    
        if (!fin.is_open())
        {
            cerr<<"Unable to open file "<<file_name<<endl;
            exit(10);
        }
    
        fin >> x;
    
        while (!fin.fail())
        {
            cout<<"Read integer: "<<x<<endl; // display number again
    
            sum=sum+x;
            sumcount++;
            if(x % 2==0)  // compuing your file statistics
                even++;
            else
                odd++;
    
            fin>>x;
        }
    
        fin.close();
        average=(double)sum/sumcount;
    
        cout<<"Sum of integers: "<<sum<<endl; // displaying results
        cout<<"Average: "<<average<<endl;
        cout<<"Number of even integers: "<<even<<endl;
        cout<<"Number of odd integers: "<<odd<<endl;
    
        return 0;
    }
    

    I have added some minimal alteration to make your program work correctly.

    the result

    enter image description here

    and more

    enter image description here