C跳过char的scanf行

时间:2018-11-16 22:27:55

标签: c while-loop scanf do-while

我正在尝试做作业,但遇到一个奇怪的问题,就是在编译代码后,在我的代码的第31行(while()之前的一行)我不明白这是怎么回事。程序只是跳过该行中的scanf函数。 我不知道发生了什么事,想寻求帮助。

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
// x is user input number
int x = 0;
// y is used for calculation
int y = 1;
// i is used for loops
int i = 0;
// c is used for user input char
char c = 'a';

do
{
    printf("Pick a number:\n");
    scanf("%d", &x);
    while(x <= 0)
    {
    printf("Input invalid\nPick another number:\n");
    scanf("%d", &x);
    }

    for(i = 1; i <= x; i++)
    {
        y = y*i;
    }
    printf("The result is: %d\n", y);
    printf("Would you like to make another calculation?\n(y/any key)\n");
    scanf("%c", &c);
} while(c == 'y');

return 0;
}

The screenshot is cut by the edges of the output

0 个答案:

没有答案