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 << " ";

Recommended Answers

All 4 Replies

Check how many times you're updating sum4 per each iteration.

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;
  //...
}

Line 13 and line 17 look awfully similar... wink wink...

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.

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.