My second while condition is being ignored and I don't understand why. I would think I have 1 and 0 = 0 so it would break out.

while(fgets(line, sizeof line, stdin) && sscanf_counter != 2)

Edited by COKEDUDE

9 Months
Discussion Span
Last Post by rubberman

Look carefully at your use of left and right parentheses on line 1.

Did you mean to compare gets(line, sizeof line, stdin) && sscanf_counter to not be equal to 2?
Or did you mean gets(line, sizeof line, stdin) then and the next condition.

Be clear about that. Don't rely on "a C compiler works like this."


And I will add the following : in the begining, the first version of your code, it is recommanded to not mix actions; separate reading from checking. Read what you need to read then check the result in the while condition even if that means you need to put the same line twice (before and inside the loop block). The code will be easier to read and you can log (print) the content to check if it's correct.


I want the loop to continue until sscanf_count equals 2 or the user hits ctrl + D.


@C. Then state so in your while condition. Never rely on the compiler to read our minds. Spell it out with a few more parentheses.

I know folk that want to debate the too many parantheses but they are here to debate, not solve it.

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.