我正在使用switch語句一個簡單的貨幣轉換程序,如下圖所示:爲什麼恆定的焦炭打破循環和switch語句
#include "library/std_lib_facilities.h"
int main()
{//This program converts yen, euros, yuan, kroner and pounds to dollars
double amount = 0;
char currency = ' ';
//one dollar equivalent of each currency
const double yen_to_dollar = 113.67;
const double pounds_to_dollar = 0.85;
const double euros_to_dollar = 0.95;
const double yuan_to_dollar = 6.87;
const double kroner_to_dollar = 7.04;
//case labels corresponding to currency
const char y = 'y', p = 'p', e = 'e', u = 'u', k = 'k';
cout << "Please type the amount you want to convert, followed by the currency(y,e,p,u,k) u is for yuan: ";
while(cin >> amount >> currency) {
switch(currency) {
case y:
cout << amount << " yen == " << amount/yen_to_dollar << " dollars." << '\n';
break;
case p:
cout << amount << " pounds == " << amount/pounds_to_dollar << " dollars." <<'\n';
break;
case e:
cout << amount << " euros == " << amount/euros_to_dollar << " dollars." << '\n';
break;
case u:
cout << amount << " yuan == " << amount/yuan_to_dollar << " dollars." << '\n';
break;
case k:
cout << amount << " kroner == " << amount/kroner_to_dollar << " dollars." << '\n';
break;
default:
cout << "Please try supported currencies" << '\n';
break;
}
}
return 0;
}
我可以預定義的貨幣轉換成美元時,該功能是通過輸入運行金額和貨幣,如下所示:5y(將5日元換算成等值美元)。 除了e
常數外,其他數量和不變的工作。只要我想用一定數量和恆定的數字來測試代碼,代碼就會結束。 (24E)。
當我將常量e = 'e'
更改爲s = 's'
之類的東西時,效果很好。
所以我的問題是爲什麼包含e字符的輸入會破壞我的代碼?
沒有'case:e' – user463035818
代碼不會編譯,因爲您仍然有'case's''''''定義,只有'e'。請確保您顯示的代碼重現您描述的行爲。 – user463035818
這是一個錯誤。我測試了它的情況下,它的工作。當我問這個問題時,我忘了回到案例e。已經編輯過現在的案例。它仍然打破了循環和切換語句 –