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);
}

Recommended Answers

All 3 Replies

>>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
}

Thanks for the replies, they help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.