Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 关于';char';_C++_String_If Statement_Char - Fatal编程技术网

C++ 关于';char';

C++ 关于';char';,c++,string,if-statement,char,C++,String,If Statement,Char,我写这段代码只是为了好玩,同时学习if-else语句。当我输入数字10e时,程序会返回错误,但我不明白为什么!我的意思是,对于其他单位,如“d”或“y”,它工作得很好,但当我用数字“e”时,它就发疯了 基本上,程序将用户输入的数字转换为该数字后面的单位。例如,如果您将数字10d返回10英镑的日元金额 看看: // money_exchange.cpp : A money exchange simulator that converts any rate to pound. // The stan

我写这段代码只是为了好玩,同时学习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;
}
//money\u exchange.cpp:将任何汇率转换为英镑的货币兑换模拟器。
//标准费率可以每天更新。
#包括“stdafx.h”
#包括“./../Library/std_lib_facilities.h”
//2018年3月19日-汇率
康斯特双倍欧元=1.13706;
常数双美元=1.40369;
常数双克朗=1.83428;
恒生双日元=148.816;
双用户_口袋=0.0;
双用户_账户=100.0;
字符串user_name=“ernest”;
字符串user\u pass=“pass”;
int main()
{
无法登录\u name>>登录\u pass;
if(login\u name==user\u name&&login\u pass==user\u pass){
//用户信息简历:

CUT< P>这是因为你的<代码>钱<代码>变量是双的。在C++中,双倍可以用科学符号(E)来引用,所以你的<代码> CIN < /C>语句正在读取<代码> 10E < /代码> 10×10 ^(?)它不是读取任何指数,也不是读取任何单元。<代码> e <代码>是浮点值的正常部分。如<代码> 10E2<代码>值>代码> 100代码>。因此,基本上,在C++中不能使用10E。我如何修复这个?如果例如,我必须使用10E。使用另一种读取和解析InP的方式。一般来说,避免在数值变量上使用
cin>
。相反,应该读取字符串,验证它,然后解析它。那么我应该如何读取用户输入(钱)在这种情况下?你能给我一个小例子吗?哦,嘿,谢谢大家的帮助!=)哦,明白了!但我需要一个双精度的变量,这样我的用户也可以输入双精度格式。比如10.5美元…用“u”代替“e”是的,我也想过改变它,但我想找到一种使用“e”的方法,这将是一种很好的锻炼!Shall我用另一种方式准备好了单位值?转换它?我不知道。Mh。我还没有读到关于parse语句的章节,也许我现在应该停止这段代码,在我学会了parse方法后编辑它!您将希望将输入作为字符串读取,然后使用
for
循环读取字符串,直到找到其中一个字符为止用于表示货币或空格字符的字符。然后可以使用
atof
函数将数字部分转换为浮点。