我是C的初學者,我正在編寫一個將美元轉換爲歐元的基本程序。由於某種原因程序沒有運行這一行:「scanf(」%c「,& yn);」。如果我刪除while while循環,程序工作正常。在C中循環時遇到麻煩
而不是停止並等待用戶輸入「y」或「n」循環重新啓動並再次要求美元的金額。
#include<stdio.h>
main()
{
float usd = 0.00;
float euro = 0.00;
char yn;
const float conversion = 0.75;
do {
/*get amount to convert*/
printf("Please enter the amount of USD you want to convert to Euros: ");
scanf("%f", &usd);
/*convert amount*/
euro = (usd * conversion);
/*output results and ask to continue*/
printf("\n%.2f USD equals %.2f Euros. Do you want to convert another amount? (y/n): ", usd, euro);
scanf("%c", &yn);
printf("\n");
/*if yes, get new amount to convert. if no, program ends*/
} while (yn = 'y');
return 0;
}
在此先感謝。
是什麼讓你說「該程序沒有運行這條線」?它最cetainly *是*運行該行。也許你應該從'scanf'打印返回值,並將值寫入'yn'中以確定發生了什麼。 (提示:'yn'將等於'\ n''。) –