Why doesn't getchar() wait for me to press enter after scanf()?

前端 未结 10 872
悲哀的现实
悲哀的现实 2020-11-22 16:22

I am learning C and I\'m using \"getchar()\" to stop the command windows so I can see the exercises am doing but it just doesn\'t work. heres a sample:

#incl         


        
10条回答
  •  孤独总比滥情好
    2020-11-22 17:26

    Your scanf only ate the number but not the trailing newline. Putting a newline or white space after the %d will then give you the opposite problem, reading too far.

    This is why people don't like scanf.

    I would suggest reading an actual line (use fgets(3)) and then using sscanf() to scan the string.

提交回复
热议问题