scanf issue when reading double

让人想犯罪 __ 提交于 2019-12-02 05:38:41

In your code, change

  scanf("%lf \n", &radius);

to

  scanf("%lf", &radius);

Otherwise, with a format string having whitespace, scanf() will behave as below (quoted from C11, chapter §7.21.6.2, paragraph 5)

A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read.

So, to provide the "non-white-space character" to end the scanning, you need to input a 0 (basically, a non-whitespace character).

Please check the man page for more details.

Conrad Siahaan

You have the same solution for a little bit different problem (just in type of variable)

Why does scanf ask for input twice, but just in the first loop iteration only?

When you include the whitespace in the scanf

The program will wait until you enter a blank space or any other value in it, so the program will continue as normal, but the blank space or any value that you entered will not be used anyway.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!