0

I'm trying to get my program to read from a text file, but the only way I'm able to get it accurate at the moment is to set the first loop to the number of lines of data that I have. I'm looking for a way to set it so that I can have it read until the end of the file. Using the !(in.eof()) thing gives me an extra line at the end. Any help would be appreciated, sorry if I'm not overly clear.

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int a,b;
ifstream in("d:\\testin.txt");
ofstream out("d:\\testout.txt");



for(int l=1; l<=14; l++)


{
	b=0;
	
	for(int c=1; c<=2; c++)
	{
		
		in>>a;
		b=b+a;
	}
out<<l<<" "<<b<<endl;

}

	return(0);
}
3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Ejl191
0

>>Using the !(in.eof()) thing gives me an extra line at the end
Yup -- that's why we never use eof() like that. Here is one way to do that loop

while( in>>a )
{
   // your code here
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.