我在代码中添加了注释,我有编译器问题吗?我无法弄明白,我试着看谷歌和书,但我无法弄清楚为什么前半部分代码只接受数字和单位之间的空格输入,第二个代码接受数字和单位。
我正在使用代码块。到目前为止,我试图关闭它并再次打开它。
int main(){
constexpr double dollar_to_euro = 0.91;
constexpr double dollar_to_yen = 117.07;
constexpr double dollar_to_pounds = 0.70;
double sum = 1;
char curr = '\0'; // tried replacing '\0' with '0' and ' '
cout << "Please enter sum, followed by currency for conversion.\n"
<< "U for dollar, E for euro, Y for yen and P for pounds.\n";
cin >> sum >> curr; // This is my issue, it does not want to accept "sumcurr" together, it only accepts it if theres space in between
// yet on the second code for inches or centimeters it does accept them being together. Look down.
// For example entering "5 E" works, yet "5E" does not work.
if(curr=='E')
cout << "The amount " << sum << " euro is " << sum/dollar_to_euro << " dollars\n";
else
cout << "GOD DAMMIT !!!!\n";
constexpr double cm_per_inch = 2.54;
double len = 1;
char unit = '\0';
cout << "Please enter length followed by unit.\n";
cin >> len >> unit; // Over here it works, this is an example from a book. Entering "5i" works.
if(unit=='i')
cout << len << " in == " << cm_per_inch*len << "cm.\n";
else
cout << "Wrong input !\n";
}
答案 0 :(得分:7)
此处的问题是E
/ e
在浮点数中有效,但5E
/ 5e
不是有效的浮点数,因为您需要一个值在E
/ e
之后。因此,当您输入5e
时sum
的输入失败,因为5e0
将起作用的语法无效。如果您使用E
/ e
以外的任何内容,那么它将像您的第二个示例一样工作。
有关浮点数格式的详细信息,请参阅:Cppreference floating point literal