未使用某些文字读取输入
Input not being read using certain literals
非常新的程序员有一个看似晦涩的问题:
string currency = "???";
double amount = 0.0;
double amount_final = 0.0;
cin >> amount >> currency;
if (currency == "GBP"){
amount_final = amount*1.47;
}
else if (currency == "Yen"){
amount_final = amount*0.0083;
}
else if(currency == "Euro"){
amount_final = amount*1.07;
一切正常,除非您输入 "Euro",在这种情况下,它就像您没有输入任何内容和 returns 初始值一样。
一些测试告诉我,我 运行 遇到问题的唯一一次是如果字符串的第一个字母是 E 或 e 并且前面没有空格,我尝试的任何其他值都可以正常工作。
TL;DR:如果除了 "Euro" 之外,我什么都不做更改,以 "Fish" 程序运行,怎么了?
#include <iostream>
#include <string>
int main() {
std::string currency = "???";
double amount = 0.0;
double amount_final = 0.0;
std::cin >> amount >> currency;
if (currency == "GBP"){
amount_final = amount*1.47;
}
else if (currency == "Yen"){
amount_final = amount*0.0083;
}
else if(currency == "Euro"){
amount_final = amount*1.07;
}
std::cout << amount_final << std::endl;
}
[localhost functionTest]$ ./a.out
11 GBP
16.17
[localhost functionTest]$ ./a.out
11 Yen
0.0913
[localhost functionTest]$ ./a.out
11 Euro
11.77
抱歉,我的情况有效,您是否一直使用 Amount Currency?
float 解析器是贪婪的,它消耗 "E" 代表输入流中的指数,留下 "uro" 作为输入流的剩余部分。
基本上贪婪解析在这里失败了,因为它是一个需要先行 1 的语法。("E" 后跟数字)。
非常新的程序员有一个看似晦涩的问题:
string currency = "???";
double amount = 0.0;
double amount_final = 0.0;
cin >> amount >> currency;
if (currency == "GBP"){
amount_final = amount*1.47;
}
else if (currency == "Yen"){
amount_final = amount*0.0083;
}
else if(currency == "Euro"){
amount_final = amount*1.07;
一切正常,除非您输入 "Euro",在这种情况下,它就像您没有输入任何内容和 returns 初始值一样。 一些测试告诉我,我 运行 遇到问题的唯一一次是如果字符串的第一个字母是 E 或 e 并且前面没有空格,我尝试的任何其他值都可以正常工作。
TL;DR:如果除了 "Euro" 之外,我什么都不做更改,以 "Fish" 程序运行,怎么了?
#include <iostream>
#include <string>
int main() {
std::string currency = "???";
double amount = 0.0;
double amount_final = 0.0;
std::cin >> amount >> currency;
if (currency == "GBP"){
amount_final = amount*1.47;
}
else if (currency == "Yen"){
amount_final = amount*0.0083;
}
else if(currency == "Euro"){
amount_final = amount*1.07;
}
std::cout << amount_final << std::endl;
}
[localhost functionTest]$ ./a.out
11 GBP
16.17
[localhost functionTest]$ ./a.out
11 Yen
0.0913
[localhost functionTest]$ ./a.out
11 Euro
11.77
抱歉,我的情况有效,您是否一直使用 Amount Currency?
float 解析器是贪婪的,它消耗 "E" 代表输入流中的指数,留下 "uro" 作为输入流的剩余部分。
基本上贪婪解析在这里失败了,因为它是一个需要先行 1 的语法。("E" 后跟数字)。