954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

reading integers from 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
Newbie Poster
16 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

The first problem is you read a number into x and print it out, but don't add it to the sum. The second problem is using fin.fail() as the loop condition, it's basically the same as using feof() , which is wrong.

Try this instead:

// read text from file
int ne(0);
int oe(0);

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

    sum += x;

    if (x % 2 == 0)
        ++ne;
    else
        ++oe;
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

thank you! I would like a better understanding of this, however. Why is using feof() (which is the equivalent of !fin.fail()) as the loop condition wrong?

maynardjk13
Newbie Poster
16 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 
Why is using feof() (which is the equivalent of !fin.fail()) as the loop condition wrong?


I posted a link. Basically it's a timing issue. The error flags aren't set untilafter you try and fail to read from the stream. Unless the loop contains a further sanity check, you'll be working with data from the previous iteration. It's a "the last record is processed twice" situation.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

That makes things much clearer. Thank you!

maynardjk13
Newbie Poster
16 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: