我是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''。) –