【问题标题】:Bizarre program behavior about 'char'关于'char'的奇怪程序行为
【发布时间】:2018-08-28 10:36:31
【问题描述】:

我在学习 if-else 语句时编写此代码只是为了好玩。当我输入 10e 时有一个奇怪的行为,程序返回错误,但我不明白为什么!我的意思是使用其他单位,例如 'd' 或 'y' 它可以完美地工作,但是当我输入 'e' 时它会变得疯狂!

基本上,程序将用户输入的数字转换为该数字后面的单位。例如,如果您输入 10d,它将返回 10£ 的日元汇率。

看看:

// money_exchange.cpp : A money exchange simulator that converts any rate to pound.
// The standard rates can be updated daily.


#include "stdafx.h"
#include "../../Library/std_lib_facilities.h"

// 19/03/18 - Exchange Rates
const double euro = 1.13706;
const double dollar = 1.40369;
const double krone = 1.83428;
const double yen = 148.816;

double user_pocket = 0.0;
double user_account = 100.0;
string user_name = "ernest";
string user_pass = "pass";

int main()
{
    cout << "Good Morning. Exchange rates loading...\n";
    cout << "Please login into your account by writing username and password separated by a whitspace: ";
    string login_name = " ";
    string login_pass = " ";

    cin >> login_name >> login_pass;

    if (login_name == user_name && login_pass == user_pass) {
        // User information resume:
        cout << "\nWelcome " << user_name << ". I'm loading your information...\n";
        cout << "Name: " << user_name << "\nPocket: " << user_pocket << " GBP" << "\nAccount: " << user_account << " GBP\n";

        // Options available
        cout << "\nI'm sorry but the only options available is: Exchange Rates\n";
        cout << "Please digit the amount of money you would like to convert followed by a unit ( E,D,Y,K ): ";
        double money = 0.0;
        char unit = ' ';
        cin >> money >> unit;

        // Convertion selection
        if (unit == 'e') {
            cout << "\nResult: " << money << "GBP = " << euro * money << "EUR\n";
            cout << "At the moment I can execute just one computation at time, please restart the application.\n";
            cout << "Thank you " << user_name << '\n';
        }
        else if (unit == 'd')
        {
            cout << "\nResult: " << money << "GBP = " << dollar * money << "USD\n";
            cout << "At the moment I can execute just one computation at time, please restart the application.\n";
            cout << "Thank you " << user_name << '\n';
        }
        else if (unit == 'k')
        {
            cout << "\nResult: " << money << "GBP = " << krone * money << "NOK\n";
            cout << "At the moment I can execute just one computation at time, please restart the application.\n";
            cout << "Thank you " << user_name << '\n';
        }
        else if (unit == 'y')
        {
            cout << "\nResult: " << money << "GBP = " << yen * money << "JPY\n";
            cout << "At the moment I can execute just one computation at time, please restart the application.\n";
            cout << "Thank you " << user_name << '\n';
        }
        else
        {
            cout << "\nSomething went wrong! Make sure to follow the instruction ( 10y will produce 10 pound in Yen )";
            cout << "\nAt the moment I can execute just one computation at time, please restart the application.\n";
            cout << "Thank you " << user_name << '\n';
        }
    }
    else
    {
        // Login failed
        cout << "\nUsername or Password not in the database, please try again!";
    }

    keep_window_open();

    return 0;
}

当您启动程序时,尝试输入 10e,您可以看到程序返回错误。如果你写 10 (space) e 它可以工作。但是对于其他单位,您不需要数字和单位之间的空间,无论如何它都会起作用。我是编程新手,所以这可能是一个愚蠢的错误。

【问题讨论】:

  • e 是浮点值的正常部分。喜欢 10e2 的值 100
  • 所以基本上,正因为如此,你不能在 C++ 中使用 10e?我该如何解决这个问题?例如,我必须使用 10e。
  • 使用另一种方式读取和解析输入。
  • 一般来说,避免在数值变量上使用cin &gt;&gt;。相反,读取一个字符串,验证它,然后解析它。
  • 那么在这种情况下我应该如何读取用户输入(money)?你能给我一个小例子吗?哦,嘿,谢谢你们的帮助! =)

标签: c++ string if-statement char


【解决方案1】:

发生这种情况是因为您的 money 变量是双精度变量。在 C++ 中,可以使用科学记数法 (e) 引用双精度数,因此您的 cin 语句将 10e 读取为 10*10^(?)。它没有读取任何指数,也没有将任何内容读入单位。

【讨论】:

  • 哦,明白了!但我需要该变量作为双精度,这样我的用户也可以键入双精度格式。像 10.5 美元...
  • 欧元使用 'u' 而不是 'e'
  • 是的,我也想过改变它,但我想找到一种使用'e'的方法,它会像锻炼一样好!我应该以不同的方式准备单位的价值吗?转换它?我不知道。嗯。
  • 我还没有到关于parse语句的章节,可能我应该暂时停止这段代码,等我学会了parse方法后再编辑它!
  • 您需要将输入读取为字符串,然后使用for 循环读取字符串,直到找到用于表示货币的字符之一或空格字符。然后您可以使用atof 函数将数字部分转换为浮点数。
猜你喜欢
  • 2017-01-02
  • 2012-10-20
  • 1970-01-01
  • 2016-05-13
  • 2012-06-01
  • 2017-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多