我有这段代码
int n;
char c;
while ((scanf("%d%c", &n, &c) != 2)|| c != '\n' && n >= 0)
{
while( getchar() != '\n' );
}
我不明白为什么不检测输入是否是负数,我只是想读正数
有人看到了这个问题吗?
答案 0 :(得分:2)
以下列方式更改条件
while ( (scanf("%d%c", &n, &c) != 2) || c != '\n' || n < 0)