将字符'e'作为输入并进行比较
Taking char 'e' as input and comparing
好的,我正在做一本书中的练习,将不同类型的货币转换成美元。出于某种原因,当我输入'e'作为字符变量的输入并将其与if语句中的'e'进行比较时,比较不起作用,但是如果我将其替换为另一个字母,它将工作得很好。这是怎么回事?下面是代码:
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
}
有很多评论,但没有解决方案。事实上,我也想不出一个好的解决办法。我能想到的最好的办法是安装一个自定义的num_get
facet(仅这一点几乎可以肯定地排除了代码适合作为家庭作业解决方案提交):这是一些高级的东西,我认为很多人都不会想到这一点。
除此之外,我认为你想要数据驱动货币,也就是说,你想要建立某种描述所有货币的容器,而不是为每种货币设置一个代码分支(顺便说一句,欧元的复数形式是欧元)。生成的程序看起来像这样:
#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 failedn";
}
}
以下是对您的程序的建议:
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.您应该为main
函数的int
返回类型添加return 0
语句。
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不是数字输入的一部分。否则,必须使用字符串解析输入。
- 我的 c++ 程序似乎没有发现字符串和我拥有但输入使用 getline 的变量之间的比较
- 我正在尝试从输入文件中读取,然后使用它们的子字符串比较字符串的特定部分
- 使用-L和-L标志与将库文件作为输入进行比较
- 通过与输入进行比较来安排输出
- 将私有对象变量与用户输入的变量进行比较
- 输入迭代器的相等性比较
- 将用户输入的术语与 txt 文件中的列表进行比较
- 将 ProcessEntry32.szExeFile 与用户输入的数据进行比较时,C++ _wcsicmp代码未编译
- 如何使用文件填充数组并将其与用户输入C 进行比较
- 从类中查看 rand() 并将用户输入与计算机选择进行比较.石头,纸,剪刀游戏
- (C++) 学习向量 - 编码/输入流终止/比较向量的问题
- 在比较特定输入中的程序的数组中出错
- 将最大最小值的数据与可变数量的输入进行比较 C++
- c++ 输入一个数字来确定其他数字,然后比较它们
- 比较输入的C弦与文件读取c-string
- 将用户输入与矢量中存储的值进行比较
- C++ - 读取和比较用户输入
- 将结果与用户输入-SQLITE3 C 进行比较
- 正在检查用户的有效输入并与文本文件进行比较
- 比较输入值和从文件中读取值的最佳方式