ifstream not reading all the way towards end of file c++

Hello i'm testing out a basic code that just reads numbers from a file and adds them up but it is not reading beyound the 3rd int and i don't know why..

for example i type in
10
20
30
40
into my nums.txt file and then it only reads 10,20,30 and adds them up but does not read the 40.. not sure what is the issue


#include <iostream>
#include<fstream>
using namespace std;
int SumFileInts(ifstream &read);

int main(void)
{

	ifstream in_stream;

	in_stream.open("nums.txt");

	if(in_stream.fail())
	{
		cout <<"input file opening failed.";
    }
SumFileInts(in_stream);
cout <<SumFileInts(in_stream);

}

int SumFileInts(ifstream &read)
{
	int numbers;
	int total;
	int sum;
	total = numbers;
	
if(read >> numbers)
{
     total =numbers + numbers;
	 sum = total+numbers;
	 return sum;
}
 
 read.close();
}

Open in new window

businessesatozAsked:
Who is Participating?
 
sarabandeCommented:
there is no loop in SumFileInts. so you have only

total =numbers + numbers;
sum = total+numbers;

Open in new window


which is 3 times of the number of first line.

instead, you should replace if (read >> number) by while (read >> number) such that the read was continued while the read does not fail. in the loop simply add the number to sum or total but not multiple times.

Sara
0
 
jkrCommented:
Your reading control flow is a bit flawed - that should be more like
#include <iostream>
#include<fstream>
using namespace std;
int SumFileInts(ifstream &read);

int main(void)
{

	ifstream in_stream;

	in_stream.open("nums.txt");

	if(in_stream.fail())
	{
		cout <<"input file opening failed.";
    }


  cout <<SumFileInts(in_stream);

}

int SumFileInts(ifstream &read)
{
	int number;
	int total = 0;

	
  while(!read.eof())
  {
     read >> number;
     total += number;
     number = 0;
  }
 
  read.close();

  return total;
}

Open in new window

0
 
businessesatozAuthor Commented:
thanks .
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
sarabandeCommented:
the while(!read.eof()) would be an infinite loop when the nums.txt contains some non-numeric value. in that case the read fails but the eof flag never was reached. so better use while(read) or while (read >> number) which would test on both eof and fail.

Sara
0
 
jkrCommented:
???
0
 
sarabandeCommented:
i tested with

while(!read.eof())
{   
    read >> numbers;
    sum += numbers;
}

Open in new window


and input file

Student: Sara
Grade: C
Average: 82

Open in new window


and it runs infinite as expected.

businessesatoz, when doing the test i found that you call SumFileInts twice in your main. look into the code jkr has posted, where the correct code was implemented.

Sara
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.