learnedfor loop,In practice when it came to getchar()) != ‘n’ The problem。I.e.while loopSo that in thefor loopEnter the void (to be treated as a character。)
All to known,getchar functionUsed to get the character,And if the direct use in a loop,It found that the situation will not be able to press enter characters into the system。The reason is the carriage return character is so ~,Enter the same time we want to join it is " \n "Discrimination,That has getchar()!='n' ,With this approach while Circulation work well,Available at for Under cycling there are many novice shouted ineffective。
= = I was one of them。
What we do know is for The format of this cycle is:
1 |
for(赋值或某语句(在循环的开始);执行条件,若值为真则循环;循环完一次执行这里的语句) |
so,According to my understanding,Use a for loop to get the input string should be like a child
1 |
for(c = getchar();c != '\n';) |
Enter decisive executable found still useless,Resolute character is treated as a system to deal with。After Google found the problem hehe
original,exist for(c = getchar();c != 'n';) in c = getchar() Before execution cycle,That is circulating in a separate statement executed,Natural Enter "invalid",which is !='n' Needs and c = getchar() Write together,As a condition for loop execution,So each for loop,As long as the establishment of a second conditional statement,Will be executed。
Original article written by LogStudio:R0uter's Blog » c = getchar()) != ' N' and for loop
Reproduced Please keep the source and description link:https://www.logcg.com/archives/131.html