every 3rd integer

For a class assignment I have to get input from a text file using a while loop. I also have to computing the sum of every 3rd integer that is read from this file. My question is how do I not read the last integer if it is not the 3rd one? In the file I'm reading the last one is the 2nd in the loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
if (inData) // Did input file open?
	{
		sum = 0;
		/// read values
		while (inData)
		{
			count = 1;
			while (count < 3)
			{
				inData >> inInt;
				count ++;
			}
			if (inData && count == 3)
			{
				inData >> inInt;
				sum = sum + inInt;
				cout << inInt << endl;
			}
		}// end while
		outData << sum <<fixed << setprecision(2) << endl;
		outData << "\n";// newline
		inData.close ();
		inData.clear ();
	}// end if
	else
	{
		cout << "Can't open file." << endl;
	}
for example,
1
2
3
4
5
6
7
8
9
int count = 0;
while(inData){
   count++;
   inData >> inInt;
   if(count % 3 == 0){
      sum += inInt;
      cout << inInt;
   }
}
Hamsterman, Thanks for your reply. That example returns the same result as mine (but a little cleaner).

edit:

I got it working! Thank you
Last edited on
Topic archived. No new replies allowed.