0
#include<stdio.h>
int main()
{
float p, r, t;
char ch = 'y';
do
{
printf("Enter principal: ");
scanf("%f", &p);
printf("Enter rate: ");
scanf("%f", &r);
printf("Enter t: ");
scanf("%f", &t);
printf("SI = %f", (p *r * t)/100);
printf("\nCalculate SI one more time ? ('Y' for Yes, 'n' for no) : ");
ch = getchar();
}while(ch == 'y');
return 0;
}
Enter principal: 1334
Enter rate: 4
Enter t: 2
SI = 106.720000
Calculate SI one more time ? ('Y' for Yes, 'n' for no) :
Process returned 0 (0x0) execution time : 5.359 s
Press any key to continue.
正如你可以看到我無法計算SI第二時間?問題是什麼 ?
我甚至試圖通過scanf函數替換的getchar()(),但它仍然沒有工作時數完成(在不能成爲它的一部分的第一個字符)
'ch = getchar();'還有一個'newline'仍在輸入緩衝區中,由前一個'scanf'留下。 –
@WeatherVane你會解釋發生了什麼事嗎? – Cody
帶有大多數格式(但不是'%c')的'scanf'會消耗*領先*空格,但會在輸入緩衝區中留下任何後續內容。基於字符的輸入函數完全取決於那裏。 –