Why is my program looping too many times?

老子叫甜甜 提交于 2019-12-05 21:14:11

Replace scanf("%d\n", &userInts[i]); with scanf("%d", &userInts[i]);

See this about entering a nonwhitespace character in format specifier in scanf.

It says:

Any character that is not either a whitespace character (blank, newline or tab) or part of a format specifier (which begin with a % character) causes the function to read the next character from the stream, compare it to this non-whitespace character and if it matches, it is discarded and the function continues with the next character of format. If the character does not match, the function fails, returning and leaving subsequent characters of the stream unread.

Problem 1:

Just replace :

scanf("%d\n", &userInts[i]);

by scanf("%d", &userInts[i]);

And add fflush(stdout) after all printf statements where the format string doesn't end with \n. Otherwise the output will be displayed only after the next \n is output.

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