我用我从网上编辑的代码和我输入的 switch 语句组合了一个简单的货币转换器程序。当我在 Visual Studio 中运行该程序时:
printf("Would you like to make another conversion? y/n\n");
fflush(stdin);
scanf("%c",&marker);
将等待输入,然后返回到 while 语句的开头或退出控制台窗口。现在,如果我在 Mac 上运行这是 Xcode “你想进行另一次转换...”,但不等待输入,它只是直接返回到 while 循环。
我是否将 printf("Would you like...etc 部分放在正确的位置?或者有没有更好的方法让循环在接受用户输入后再次运行?我需要用 'bool 做任何事情吗? ' 声明。我们在课堂上还没有走那么远。
完整代码如下所示:
#include <stdio.h>
int main ()
{
int choice;
float money;
float total;
char marker='y';
printf("\n\nCURRENCY CONVERSION\n");
printf("***Rates correct as of 26 NOV 12***\n");
printf("------------------------------------\n");
printf("1. Australian Dollar (AUD) 1.533=1 GBP\n");
printf("2. Euro (EUR) 1.235=1 GBP\n");
printf("3. Indian Rupee (INR) 89.494=1 GBP\n");
printf("4. Japanese Yen (JPY) 131.473=1 GBP\n");
printf("5. US Dollar (USD) 1.602=1 GBP\n");
printf("Enter the number for the currency to convert...");
while(marker!='n')
{
printf("\n\nWhat would you like to convert your money to? (1-5): ");
scanf("%d",&choice);
printf("\n\nHow much money do you want to convert? (GBP): ");
scanf("%f",&money);
switch(choice) {
case 1:
total = money * 1.533;
printf("\n\nYou will have %.2f Australian Dollars \n\n", total);
break;
case 2:
total = money * 1.235;
printf("\n\nYou will have %.2f Euros \n\n", total);
break;
case 3:
total = money * 89.494;
printf("\n\nYou will have %.2f Indian Rupees \n\n",total);
break;
case 4:
total = money * 131.473;
printf("\n\nYou will have %.2f Japanese Yen \n\n", total);
break;
case 5:
total = money * 1.602;
printf("\n\nYou will have %.2f US Dollars \n\n", total);
break;
default:
printf("You did not choose a correct option\n");
}
printf("Would you like to make another conversion? y/n\n");
fflush(stdin);
scanf("%c",&marker);
}
return 0;
}
非常感谢收到的任何意见。