kazuya8375 发表于 2022-3-25 21:31:53

循環和getchar問題

#include <stdio.h>
int main()
{
int a,temp,c;
temp = 0;
printf("請輸入合法數字");
while(48<=(c=getchar())&&c<=57)
{
printf("請輸入合法數字");
a = c-48 ;
a = a + temp;

temp = a ;






}

printf("%d",temp);
}
為什麼我輸入0~9其中一個數字後一次之後,他就不給我輸入,自動跳出

ba21 发表于 2022-3-25 23:35:53


#include <stdio.h>
int main()
{
int a,temp,c;
temp = 0;
printf("請輸入合法數字");
while(48<=(c=getchar())&&c<=57)
{
printf("請輸入合法數字");
a = c-48 ;
a = a + temp;

temp = a ;



while(getchar()!='\n');


}

printf("%d",temp);
}
页: [1]
查看完整版本: 循環和getchar問題