关于'char'的奇怪程序行为

时间:2018-03-19 18:41:31

标签: c++ string if-statement char

我正在编写这段代码,只是为了学习if-else语句。当我数字10e时,有一个奇怪的行为,程序返回错误,但我不明白为什么!我的意思是与其他单位如'd'或'y'一起工作得很完美,但是当我用数字'e'时,它就变得坚固了!

基本上,程序会转换用户输入的数字,以该数字后面的单位表示。例如,如果您将数字10 d ,它将返回日元的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(空格)e它有效。但是对于其他单位,你不需要数字和单位之间的空间,它无论如何都会起作用。我是编程新手,所以这可能是一个愚蠢的错误。

1 个答案:

答案 0 :(得分:7)

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