Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bizarre program behavior about 'char'

I'm writing this code just for fun while learning the if-else statements. There is a weird behavior when I digit 10e, the program returns the error, but I can't understand why! I mean with the other unit such as 'd' or 'y' it works perfectly, but when I digit 'e' it goes nut!

Basically, the program converts the number the user input, in the unit that follows that number. Example, if you digit 10d it will return how much is 10£ in Japanese Yen.

Have a look:

// 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;
}

When you start the program, try to digit 10e and you can see the program returning the error. If you write 10 (space) e it works. But with the other units you don't need the space between the digit and the unit, it'll work anyway. I'm new to programming, so it is probably a stupid error.

like image 411
Ernesto Campese Avatar asked Mar 06 '23 15:03

Ernesto Campese


1 Answers

This is happening because your money variable is a double. In C++, doubles can be referenced using scientific notation (e), so your cin statement is reading 10e as 10*10^(?). It's not reading any exponent and also not reading anything into unit.

like image 140
Nico Mitchell Avatar answered Mar 17 '23 01:03

Nico Mitchell