Sum from a text file

Hello there,

I have an assignment for my computer science class which asks me to do this:

Write a main( ) routine in which the program open a data file with a name: “in.dat”. You can download this file from VLT. Then, your program calls a function that has the following interface:

int sumIntElement( ifstream &y)
{

…..

}

Inside sumElement( ), the code should calculate the sum of all the integers in “in.dat” file and return the sum of integers stored in that file. You may design a while loop with a test condition: x.eof( ), where eof( ) represents the end of a file. It is a member function of fstream class.

After trying, I did 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
#include <iostream>
#include <fstream>
using namespace std;
int sumInElement (ifstream & x) 

{

	int num = 0;

	while (!x.eof()) 

	{

		x >> num;

		cout << num << endl;

	}

	return 0;

}


	int main()
	{

		ifstream x;

		x.open("in.dat"); // open in.dat

		cout << sumInElement(x) << endl;

		x.close(); // close the file

			system ("pause");
			return 0;

	}


but I am missing something which is the sum of all integers.. I want it to be printed out at the end.. Any help?

Best,
Said
Where are you calculating the sum?
Why are you returning 0 at line 20?
1
2
3
4
5
6
7
8
9
10
11
int sumInElement (ifstream & x) 
{  int sum = 0; 
    int num;

    while (!x.eof()) 
    {   x >> num;
         if (x.good())
            sum += num;
    }
    return sum;
}




By the way, despite what you may have been told, this is not a good way to code the loop:
while (!x.eof())
Depending on the content of the file, the above may give incorrect results.

It is much more better and more dependable to do it like this:
1
2
3
4
5
    while (x >> num)
    {
        cout << num << endl;
        total += num;
    }
@AbstractionAnon

This is the skeleton the professor gave to us during lab but it's confusing me I don't understand most of it.

@Chervil

I know since I don't get what it means.


-------


What I am trying to do is, showing the integers first from a text file, then calculate the sum of it.

After using your code,

I came up with:

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
#include <iostream>
#include <fstream>

using namespace std;
int sumInElement (ifstream & x) 

{
	int sum = 0;

	int num = 0;

	while (!x.eof()) 

	{

		x >> num;

		if (x.good())
			sum += num;

		cout << num << endl;

	}

	return sum;

}




	int main()
	{

		ifstream x;

		x.open("in.dat"); // open in.dat

		cout << sumInElement(x) << endl;

		x.close(); // close the file

			system ("pause");
			return 0;

	}


and it worked perfectly. But how can I write something like,

cout << "The sum of the integers in text file is: " << sum << endl; ?

Where can I write that?

Thank you so much.

EDIT: It's not calculating the last integer, I don't know why?
Last edited on
Here you go

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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;

int main(int nNumberofArgs,char* pszArgs[])
{
    char filename[256];
    double sum = 0;
    cout << "Enter the name of file you want to open: ";
    cin.getline(filename,256);

    for(int s = 0;s != '\n';s++)
    {
        if(filename[s] == NULL)
        {
            filename[s] = '.';
            filename[s + 1] = 't';
            filename[s + 2] = 'x';
            filename[s + 3] = 't';
            filename[s + 4] = NULL;
            break;
        }
    }
    cout << "Opening and reading contents of '" << filename << "'" << endl;
    cout << "Then adding sum of integers in file." << endl;

    ifstream File(filename);

    for(int n = 1;;n++)
    {
        double object;
        File >> object;

        if(File.fail())
        {
            break;
        }

        cout << n << " - " << object << endl;
        sum += object;
    }
    cout << "The sum of the integers in the file is: " << sum << endl;

    system("PAUSE");
    return 0;
}
You even get to say what file you want to open
Thank you so much for your code, it's working perfectly as I want it but little thing, I want the code to open the file in.dat which is in my c++ project directory, plus, the file is not .txt, and not all the files are, so normal user probably won't recognize that he/she has to write it only in .txt.. if you know what I mean.

So, I want to get rid of the if() and I want to use the I/O to open the file.. which what my assignment is all about.

Edit: Ok I got what I wanted. Thanks everyone for the help.
Last edited on
Topic archived. No new replies allowed.