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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
businessesatozAuthor Commented:
thanks .
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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
jkrCommented:
???
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.