Home > Back-end >  C language, a great god
C language, a great god

Time:02-19

This program is to stay in a hotel, input the correct number of days, only captures the input number function part,
There are two don't understand.
(1) the conditions in the while loop, I understood as when days are 1, not into the circulation, enter not into the loop when 1 (but is not in conformity with the simply enter again the purpose of the correct number of days),
Can run, no matter what I input Numbers (including 1) will not enter the circulation, (that is, the application meets the requirements)
That this program is right, is to meet the requirements, but my understanding of them is wrong, ask god to solve!

2 cycles of the scanf (' * '% s')
What is the role of (1) *
(2) why do you want to join this statement here? (after I get rid of this statement, the program may enter the infinite loop, I can't understand why)

CodePudding user response:

The scanf return values ranging from 1 (note that only one of the scanf % d, therefore is equal to 1 is normal input), shows that the input does not match the % d, then the scanf (" % s "); In the cache is used to receive input does not match the contents of % d, so that ensure the input cache does not contain the content, the next time the scanf will receive new input, without it, so the content of the input buffer will and % d match, do not match, continue to return zero, thus became an infinite loop

CodePudding user response:

reference 1/f, confident boy reply:
the scanf return values ranging from 1 (note the scanf is a % d, and thus is equal to 1 is normal input), shows that the input does not match the % d, then the scanf (" % s "); In the cache is used to receive input does not match the contents of % d, so that ensure the input cache does not contain the content, the next time the scanf will receive new input, without it, so the content of the input buffer will and % d match, do not match, continue to return zero, thus became an infinite loop ~

As if with the getchar () receiving the enter key, understand some, but while the conditions inside, if I start input to 5, namely for 5, 5 days!=1 is true (or nonzero, why don't you into the loop?

CodePudding user response:

reference 1/f, confident boy reply:
the scanf return values ranging from 1 (note the scanf is a % d, and thus is equal to 1 is normal input), shows that the input does not match the % d, then the scanf (" % s "); In the cache is used to receive input does not match the contents of % d, so that ensure the input cache does not contain the content, the next time the scanf will receive new input, without it, so the content of the input buffer will and % d match, do not match, continue to return zero, thus became an infinite loop ~

Well, as with the getchar () receive the return,
But while there still don't understand, if I enter days is 5, 5 then!=1 is true (logical value of the nonzero), why don't you into the loop?

CodePudding user response:

reference weixin_45544561 reply: 3/f
Quote: reference 1/f, confident boy reply:
the scanf return values ranging from 1 (note the scanf is a % d, and thus is equal to 1 is normal input), input does not match the % d, then the scanf (" % s "); In the cache is used to receive input does not match the contents of % d, so that ensure the input cache does not contain the content, the next time the scanf will receive new input, without it, so the content of the input buffer will and % d match, do not match, continue to return zero, thus became an infinite loop ~

Well, as with the getchar () receive the return,
But while there still don't understand, if I enter days is 5, 5 then!=1 is true (logical value of the nonzero), why don't you into the loop?

Is the return value, the return value is either 0 or EOF, either enter the number of matches

CodePudding user response:

references 4 building self-confidence boy reply:
Quote: refer to the third floor weixin_45544561 response:
Quote: reference 1/f, confident boy reply:
the scanf return values ranging from 1 (note the scanf is a % d, and thus is equal to 1 is normal input), shows that the input does not match the % d, then the scanf (" % s "); In the cache is used to receive input does not match the contents of % d, so that ensure the input cache does not contain the content, the next time the scanf will receive new input, without it, so the content of the input buffer will and % d match, do not match, continue to return zero, thus became an infinite loop ~

Well, as with the getchar () receive the return,
But while there still don't understand, if I enter days is 5, 5 then!=1 is true (logical value of the nonzero), why don't you into the loop?

Is the return value, the return value is either 0 or EOF, either enter the number of matching

See learned that much
  • Related