C++ 取char';e';作为输入和比较

C++ 取char';e';作为输入和比较,c++,C++,好的,我在书中做了一个练习,把不同类型的钱转换成美元。出于某种原因,当我输入“e”作为char变量的输入,并将其与if语句中的“e”进行比较时,比较不起作用,但是如果我用另一个字母替换它,则效果很好。怎么回事?代码如下: int main() { const double yen_per_dollar = .013; const double pound_per_dollar = 1.55; const double euro_per_dollar = 1.29;

好的,我在书中做了一个练习,把不同类型的钱转换成美元。出于某种原因,当我输入“e”作为char变量的输入,并将其与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
}
intmain()
{
恒生双日元/美元=0.013;
const双磅/美元=1.55;
成本双倍欧元/美元=1.29;
双倍金额=1;
字符单位=“”;
std::cout>金额>>单位;
如果(单位='y')

std::cout以下是对您的节目的建议:

1.如果您正在使用Visual Studio,请使用类似于
系统(“暂停”);
的方法暂停程序或更改项目设置,以便调试窗口在执行后等待您。

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";
}
在包含预处理器之后,可以像这样省略include
std namespace

cout << "Please enter a amount followed by a unit (p, y, or e): ";

cout有很多评论,但没有解决方案。事实上,我也找不到一个好的解决方案。我能想到的最好办法是安装一个自定义的
num_get
facet(仅此一点几乎可以肯定地排除代码适合作为家庭作业解决方案提交)当前位置这是一些先进的东西,我想很多人都不会想到这一点

除此之外,我认为您需要数据驱动货币,即,您不需要为每种货币设置代码分支,而是需要设置某种容器来描述所有货币(顺便说一句,Euro的复数形式是Euro)。生成的程序如下所示:

#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";
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
结构货币\u获取:
std::num_get
{
iter_类型do_get(iter_类型it,iter_类型end,std::ios_base&,std::ios_base::iostate&err,double&v)常量
{
std::字符串输入;
对于(;it!=end&&(*it='.| |*it='-'.| |*it='+'
||isdigit(静态_cast(*it));++it)
{
输入。推回(*it);
}
errno=0;
if(input.empty())
{
err |=std::ios_base::failbit;
}
其他的
{
v=strtod(input.c_str(),0);
}
归还它;
}
};
int main()
{
typedef std::元组描述;
地图货币;
货币['y']=desc(0.013,“日元”,“日元”);
货币['p']=desc(1.55英镑);
货币['e']=desc(1.29,“欧元”、“欧元”);
双倍金额(0);
字符货币(“”);
std::locale loc(std::locale(),新货币);
标准:cin.imbue(loc);
如果(标准::cin>>金额>>货币)
{
std::map::const_迭代器it(currency.find(currency));
如果(it!=currences.end())
{
描述施工与设计(it->第二);

Std::Cuth.认为,例如,代码> 3.14E20是一个有效的规范:<代码>双值。欢呼和Hth.AHHH,我想它可能与此有关,只是记住从小学的符号,不知道双倍可以这样表达。谢谢。请不要在最后用Palt人为地暂停程序。与表单相关的技术,如
系统(“暂停”);
。如果问题是“窗口刚刚消失”,然后以不同的方式运行程序,这样窗口的生存期就不会与程序的生存期相关联。您的程序不会创建命令窗口,因此,在逻辑上也不负责保持命令窗口。@KarlKnechtel:说得好!我认为应该扩展到与平台无关的技术,如
std::cin.get()
。可以复制粘贴您的消息给未来的罪犯吗?@AntonGolov有时我们必须为正确的i18n/l10n做这些事情,但是我们应该使用更复杂的方法来构建字符串,而不是使用
运算符进行顺序串联。请注意,如果省略
main()末尾的显式
返回
,那么效果就好像您编写了
return 0;
。只有
main()
可以获得此豁免。我不太喜欢它,但它是标准行为(C99也支持此操作,因此C11也支持此操作)。使用“return 0”将是好的。在过去的日子里,我们在编程竞赛中遇到编译时间错误,因为忽略了这一点,因为大多数评判编译器都不是这样。如果程序消耗大量内存,包括不必要的命名空间成员,显式限定将是好的。对于小程序,显式限定不会有多大影响。我也不同意mo第二点完全错误:
if(a)if(b)stmt;else stmt;
else
解析为内部
if
,如图所示:我明白你的意思。你是对的,但你误解了我。我没有冒犯c的基本语法。我建议它使他的程序工作且全面。我想如果你看他的程序,你会理解的。我想k最简单的解决方案是要求用户在数字和货币说明符之间键入空格。:)
#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";
    }
}