好的,我正在做一本书中的练习,将不同类型的货币转换为美元。但是当我在 if 语句中将 'e' 作为 char 变量的输入并进行比较时,比较不起作用,但如果我用其他字母替换它,则可以正常工作。怎么回事?以下是代码:
int main()
{
const double yen_per_dollar = .013;
const double pound_per_dollar = 1.55;
const double euro_per_dollar = 1.29;
double amount = 1;
char unit = ' ';
std::cout << "Please enter a amount followed by a unit (p, y, or e): ";
std::cin >> amount >> unit;
if (unit == 'y')
std::cout << amount << " yen is $" << amount * yen_per_dollar << " dollars.\n";
if (unit == 'p')
if (amount == 1)
std::cout << amount << " pound is $" << amount * pound_per_dollar << " dollars.\n";
else
std::cout << amount << " pounds is $" << amount * pound_per_dollar << " dollars.\n";
if (unit == 'e')
if (amount == 1)
std::cout << amount << " euro is $" << amount * euro_per_dollar << " dollars.\n";
else
std::cout << amount << " euros is $" << amount * euro_per_dollar << " dollars.\n";
else
std::cout << "Sorry, that input isn't in the correct format." << std::endl;
std::cin >> amount; // Keeps window open
}
3.14e20
是一个有效的表示double
类型数值的规范。祝好! - Cheers and hth. - Alfsystem("pause");
来人为地在程序末尾暂停。如果问题是“窗口突然消失”,那么可以以不同的方式运行程序,使窗口的生命周期与程序的生命周期无关。你的程序不会创建命令窗口,因此逻辑上不负责保持它的存在。 - Karl Knechtelstd::cin.get()
。可以复制粘贴您的消息给未来的违规者吗? - Potatoswatteroperator<<
等顺序连接的方式(例如某种位置格式化工具,如boost::format
)。 - Karl Knechtel