1

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

Jack
  • 321
  • 2
  • 5
  • 16

4 Answers4

1

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

Mykola
  • 3,343
  • 6
  • 23
  • 39
0
#include <iostream>
#include <fstream>
using namespace std;


int main() {

int n=0;
int sum=0,total=0;

fstream file("numbers.txt");
while(file >> n) // or while(cin >> n) to read from stdin, commandline
{
    sum += n;
    total++;
}

int average = (float) sum/total;

cout<<"sum: " << sum << endl;
cout << "average: " << average << endl;
return 0;
}
ArchLinuxTux
  • 840
  • 1
  • 11
  • 28
0

As soon as your main function encounters the statement return 0 it will exit the program and fail to execute the remaining code. This should generally appear only once in your main function, at the end of your code block

David Loughnane
  • 143
  • 1
  • 3
  • 11
0
#include <iostream>
#include<fstream>

int main()
{
    std::ifstream txtFile;
    txtFile.open("data.txt");
    //TODO: check if the file fails to open.

    double tempNum, mean(0.0), sum(0.0), count(0.0);
    while (txtFile >> tempNum)
    {
        sum += tempNum;
        ++count;
    }
    mean = sum/count;
    std::cout << "mean: " << mean << "  sum: " << sum << std::endl;

    return 0;
}
CroCo
  • 5,531
  • 9
  • 56
  • 88