[try Beta version]
Not logged in

 
What is this for loop??

Nov 2, 2011 at 3:37am
Can someone explain to me what this for loop is doing?

1
2
for(ifstream fin("integers1.txt"); fin >> left[lpos++] >> right[rpos++]; );
	lpos--; rpos--;


Step by step if you could. Thank you so much!!!
Nov 2, 2011 at 6:46am
It does
1
2
3
4
5
ifstream fin("integers1.txt");//open a file
if (fin >> left[lpos++] >> right[rpos++]) //try reading things from it
  if (fin >> left[lpos++] >> right[rpos++]) //if succeeded, try reading more
     if (fin >> left[lpos++] >> right[rpos++])
        ... //repeat until failure 

operator >> sort of returns 0 when an error (such as end of file) occurs.
Nov 2, 2011 at 9:17pm
What does the "lpos--" and "rpos-- mean? I know that it decrements it, but why does it do it?
Nov 2, 2011 at 10:51pm
its depending on the rest of the code;
lpos : left position;
rpos : right position;
what's wrong ??
Nov 2, 2011 at 10:52pm
The final fin will have incremented rpos and lpos to now be indices one past the end of right and left respectively. By decrementing them they now index the last integer added to the arrays.
Nov 2, 2011 at 11:51pm
Thank you steve.
Topic archived. No new replies allowed.