We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,692 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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!

2
Contributors
4
Replies
2 Hours
Discussion Span
1 Year Ago
Last Updated
5
Views
Question
Answered
maynardjk13
Newbie Poster
16 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 53

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
Skill Endorsements: 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 until after 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
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 53
Question Answered as of 1 Year Ago by Narue

That makes things much clearer. Thank you!

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

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0986 seconds using 2.67MB