Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

The program doesn't stop on scanf("%c", &ch) line, why?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Let's say you input 2 when you're reading for num. The actual input stream will be 2 ( is the newline character). 2 goes into the num, and there remains , which goes into ch. To avoid this, add a whitespace in format specifier.

scanf(" %c", &ch); 

This will ignore any whitespaces, newlines or tabs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...