C ++:为什么我必须以这种方式将声明声明为double?

问题描述

所以我的任务之一是创建一个转换货币的程序,下面的这个程序将美元转换为某些南美元。

我不确定为什么不能同时声明美元变量和其他货币变量。我是这样做的,程序搞砸了转换:

double dollars;

double pesos = dollars * 26;

double reais = dollars * 5.9;

double soles = dollars * 3.53;

std::cout << "Enter the number of U.S. dollars you have:";
std::cin >> dollars;

std::cout << "In colombian pesos,that's " << pesos << "\n";
std::cout << "In brazilean reals,that's " << reais << "\n";
std::cout << "In peruvian soles,that's " << soles << "\n";

输入/输出是这样的:

Enter the number of U.S. dollars you have:1
In colombian pesos,that's 0
In brazilean reals,that's 0
In peruvian soles,that's 0

但是,当我以这种方式运行代码(将输入直接放在dollars的声明之后)时,输入将执行正确的转换:

double dollars;

std::cout << "Enter the number of U.S. dollars you have:";
std::cin >> dollars;

double pesos = dollars * 26;

double reais = dollars * 5.9;

double soles = dollars * 3.53;

std::cout << "In colombian pesos,that's " << soles << "\n";

只要我在使用变量之前声明变量,我就认为放置变量的位置并不重要。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)