0

The program is supposed to pull in data from an input file and sum numbers and not exceed 1000 and then stop, but it seems that my code is making it not calculate properly, it is less than it is supposed to be, but doesn't add to what is present. The numbers in red are the ones that should be summed, it is getting some weird number from somewhere.

INPUT:
55 67 458 23 81 33
782 375 528
405 324 950 46
14 864 551 38 167 518 630

OUTPUT:
702(But should be 717)

//Part H
	FileIn.open ("M:\\DataFile2.txt");

	int sum4 = 0;
	int NUM4;
	
	   
	bool done = true;

	while (done)
	{
		FileIn >> NUM4;
		sum4 = sum4 + NUM4;
		if ( sum4 + NUM4 > 1000 )
			done = false;
		else if ( sum4 + NUM4 < 1000 )
			sum4 = sum4 + NUM4;


	}
	FileOut << sum4 << " ";
4
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by AODfan
0

May want to check the file stream for errors also.

Could re-design loop to be:

while( inFile >> tempInt )//<-- exits loop on error, possibly what you want.
{
  if( (tempInt + sum) > 1000 )
    break;
  //...
}
0

Thanks for the help guys, got it figured out a bit after posting this, removed line 13 and it worked fine, just what mike was hinting.

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.