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)
{
}

Recommended Answers

All 5 Replies

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.

while(fgets(line, sizeof line, stdin)
{
    if (sscanf_counter != 2)
    #breakout here#
}
commented: From Fear Factor to Re Factor. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.