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
}
答案 0 :(得分:2)
有很多评论但没有解决方案。事实上,我也无法找到一个好的解决方案。我能想到的最好的方法就是安装一个自定义的num_get
方面(这一点几乎肯定会将代码规定为适合作为家庭作业解决方案):这是一些先进的东西,我不认为许多人会想到这一点。
除此之外,我认为您想要对货币进行数据驱动,即不是为每种货币设置代码分支,而是要设置某种描述所有货币的容器(BTW,复数形式)欧元是欧元)。结果程序看起来像这样:
#include <iostream>
#include <locale>
#include <string>
#include <tuple>
#include <map>
#include <ctype.h>
struct currency_get:
std::num_get<char>
{
iter_type do_get(iter_type it, iter_type end, std::ios_base&, std::ios_base::iostate& err, double& v) const
{
std::string input;
for (; it != end && (*it == '.' || *it == '-' || *it == '+'
|| isdigit(static_cast<unsigned char>(*it))); ++it)
{
input.push_back(*it);
}
errno = 0;
if (input.empty())
{
err |= std::ios_base::failbit;
}
else
{
v = strtod(input.c_str(), 0);
}
return it;
}
};
int main()
{
typedef std::tuple<double, std::string, std::string> desc;
std::map<char, desc> currencies;
currencies['y'] = desc(0.013, "yen", "yen");
currencies['p'] = desc(1.55, "pound", "pounds");
currencies['e'] = desc(1.29, "euro", "euro");
double amount(0);
char currency(' ');
std::locale loc(std::locale(), new currency_get);
std::cin.imbue(loc);
if (std::cin >> amount >> currency)
{
std::map<char, desc>::const_iterator it(currencies.find(currency));
if (it != currencies.end())
{
desc const& d(it->second);
std::cout << amount << " " << (amount == 1? std::get<1>(d): std::get<2>(d)) << " is "
<< (std::get<0>(d) * amount) << " dollar"
<< (std::get<0>(d) * amount == 1? "": "s") << "\n";
}
}
else
{
std::cout << "input failed\n";
}
}
答案 1 :(得分:-1)
以下是您的计划的建议:
1.如果使用Visual Studio,请使用system("pause");
之类的内容暂停程序或更改项目设置,以便调试窗口在执行后等待您。
2.对于下一个if
语句,您应该使用else语句并使用大括号括起来。这是一个例子,
else 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";
}
如果以这种方式执行此操作,您的上一个else
语句将为您提供正确的结果。否则你总会得到“抱歉,输入的格式不正确”除了'e'。的确,这就是你的计划中发生的事情。
3.您应该为return 0
返回类型int
功能添加main
声明。
4.每次添加此语句一次,而不是键入std,
using namespace std;
在包含预处理器之后,可能会在此之后省略std namespace
,
cout << "Please enter a amount followed by a unit (p, y, or e): ";
5。 e是与整数/双精度数字类型一起使用的保留字符。 e表示指数。输入货币时,请先使用空格明确告诉cin此e不是数字输入的一部分。否则,您必须使用字符串解析输入。