0

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

4
Contributors
5
Replies
30
Views
3 Weeks
Discussion Span
Last Post by rubberman
1

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

0

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.

0

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

0

@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.

1
while(fgets(line, sizeof line, stdin)
{
    if (sscanf_counter != 2)
    #breakout here#
}
Votes + Comments
From Fear Factor to Re Factor.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.