Finding the sum of integers in a file

Greetings!

I've been working on this program for the past couple of hours now, and I cant seem to get it right. I'm supposed to write a program that reads in integers from a text file, and then find the sum of the integers. The problem I'm having is with finding the sum. Here is my program thus far:


//File name: readData.cpp
//Created by: Ricardo Renta
//Created in: 11/1/11

#include <cstdlib> // function exit() is in cstdlib
#include <fstream> // class ofstream() is in fstream
#include <iostream>
#include <string>
using namespace std;

int main()
{
ofstream fout;
ifstream fin; // declare an input file stream
string name;
int x(0);
double avg(0.0);
int sum(0);


cout << "Enter file name: ";
cin >> name;

// open file file_name for input
fin.open(name.c_str(), ios::in);

// check if file is opened for input
if (!fin.is_open())
{
cerr << "Unable to open file " << name << endl;
exit(10);
}

// read text from file
fin >> x;
int ne(0);
int oe(0);
while (!fin.fail())
{
cout << "Read integer: " << x << endl;
fin >> x;

sum += x;




if (x%2 == 0)
{ ne++; }

else
{ oe++;}



}

avg = sum/(ne+oe);

cout << "The sum if the integers is:" << sum << endl;
cout << "The average of the integers is:" << avg << endl;
cout << "The number of even integers is:" << ne << endl;
cout << "The number of odd integers is:" << oe << endl;






// check for error
if (!fin.eof())
{
cerr << "Error reading file " << name << endl;
exit(20);
}

// close file stream fin
fin.close();

return 0;
}

if anyone could point me in the direction that I need to go, that would be very much appreciated! I am not looking for a handout, just a hint :)

Thank you!
@maynardjk13

Try changing
1
2
3
int x(0);
double avg(0.0);
int sum(0);
to
1
2
3
int x = 0;
double avg = 0.0;
int sum = 0;
.
And do the same to
1
2
int ne(0);
int oe(0);
to
1
2
int ne =0;
int oe = 0;
You should have have less problems.
Last edited on
What is your input file contents . please specify the formate of input file
hmmmm....still not giving me the right sum
1
2
3
4
5
6
while (!fin.fail())
{
  cout << "Read integer: " << x << endl;
  fin >> x; //¿what if it fails here?

  sum += x; //this will be garbage 

A test case could help
@maynardjk13

Try this!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Sum of Integers.cpp : Defines the entry point for the console application.
//Created by: Ricardo Renta
//Created in: 11/1/11

#include <cstdlib> // function exit() is in cstdlib
#include <fstream> // class ofstream() is in fstream
#include <iostream>
#include <string>

using namespace std;

int main()
{
	ofstream fout;
	//ifstream fin("numbers.txt"); // declare an input file stream
	ifstream fin; // declare an input file stream
	string name;
	int x = 0;
	double avg = 0.0;
	int sum = 0;

	cout << "Enter file name: ";
	cin >> name;

	// open file file_name for input
	fin.open(name.c_str(), ios::in);

	// check if file is opened for input
	if (!fin.is_open())
	{
		cerr << "Unable to open file.." << endl;
		exit(10);
	}

	// read text from file
	int ne = 0;
	int oe = 0;
	while (!fin.fail())
	{

		fin >> x;
		if (fin)
		{
			cout << "Read integer: " << x << " : ";

			sum += x;

			if (x%2 == 0)
			{
				cout << " even number.." << endl;
				ne++;
			}
			else
			{
				cout << " odd number.." << endl;
				oe++;
			}
		}
	}

	avg = sum/(ne+oe);
	cout << endl << endl;
	cout << "The sum of the integers is:     " << sum << endl;
	cout << "The average of the integers is: " << avg << endl;
	cout << "The number of even integers is: " << ne << endl;
	cout << "The number of odd integers is:  " << oe << endl << endl;

	// check for error
	if (!fin.eof())
	{
		cerr << "Error reading file." <<  endl;
		exit(20);
	}

	// close file stream fin
	fin.close();

	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <fstream> // class ofstream() is in fstream
#include <iostream>
#include <string>
using namespace std;

int main()
{
        ofstream fout;
        ifstream fin; // declare an input file stream
        string name;
        int x = 0 , sum  = 0, m_nceven = 0 , m_ncodd = 0 , count = 0 ;
        double avg = 0.0;

        cout << "Enter file name: ";
        cin >> name;

        // open file file_name for input
        fin.open(name.c_str(), ios::in);

        // check if file is opened for input
        if (!fin.is_open())
        {
                cerr << "Unable to open file " << name << endl;
                exit(10);
        }

        // read text from file
//      fin >> x;

  while (!fin.fail())
        {
                cout << "Read integer: " << x << endl;
                fin >> x;
                sum += x;
                cout<<" The sum of x = "<<x<<"\t"<<sum;

                if (x%2 == 0)
                { m_nceven++; }
                else
                { m_ncodd++;}
                count++;

        };
        sum -=x;

                avg = (sum ) /(count -1);

        cout << "The sum if the integers is:" << sum << endl;
        cout << "The average of the integers is:" << avg << endl;
        cout << "The number of even integers is:" << m_nceven << endl;
        cout << "The number of odd integers is:" << m_ncodd<< endl;

// check for error
        if (!fin.eof())
        {
                cerr << "Error reading file " << name << endl;
                exit(20);
}
  // close file stream fin
        fin.close();

return 0;
}
Topic archived. No new replies allowed.