tags:

views:

79

answers:

3

I have a loop similar to this.

int total1, total2;
for (total1 = fsize(myfile);;) {
    total2 = fsize(myfile);
    ...
    ...
    total1 = total2;
}

What I would like to do is to convert this to a while loop and check for an extra condition before ending the loop.

I would like to do something like this:

while((total1 = fsize(myfile)) && input = getch() != 'Q') {
    total2 = fsize(myfile);
    ...
    total1 = total2;
}

Thanks

A: 

You can use a for:

for(total1 = fsize(myfile); (input = getch()) != 'Q';) {
    ...
}
Vinko Vrsalovic
You can *always* use a "for" in place of a while. Just change "while" to "for" and put semicolons just before and after the condition.
T.E.D.
@T.E.D.: Of course. I was trying to guess the answer the OP was looking for
Vinko Vrsalovic
A: 

Perhaps you meant

while((total1 == fsize(myfile)) && (input = getch() != 'Q')) {
    total2 = fsize(myfile);
    ...
    total1 = total2;
}

Mind those operators = is assignment == is comparison

JohnFx
And perhaps you meant not to change the second `=` to `==`.
IVlad
just noticed that, fixed.
JohnFx
Its almost impossible to tell exactly what he meant without a lot more context.
T.E.D.
A: 

The 'initialization' part of the for loop total1=fsize(myfile) has become part of the condition tested in your while loop. Is that what you intended??

Are you sure you didn't want this...

total1 = fsize(myfile);

while((input = getch()) != 'Q') {
  total2 = fsize(myfile);
  ...
  total1 = total2;
}
Roddy