Program Is Outputting Data as an int not double?

I made a program that converts money from yen, kroner, and/or pounds to USD. However, for some reason it outputs the result as an int rather than double for kroner and pounds. I know I could use setprecision() but I shouldn't need to use it for this.

Could you please tell me what I'm doing wrong or give me a hint? Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
Program Name: MoneyConversion.cpp
Program Purpose:
- This is an exercise for the programming textbook for C++ I'm learning from.

Program Goal:
- Write a program that converts yen ('y'), kroner ('k'), and pounds ('p')
  into dollars.
*/
#include <iostream>

int main() {
    double money = 0.0;
    char symbol = ' ';
    std::cout << "Input the balance of money you would like to convert followed by the symbol sign (y, k, or p): ";
    std::cin >> money >> symbol;

    double USD = 0.0;

    // Yen to USD
    if (symbol == 'y') {
        USD = money * 0.0094;
    }
    // Kroner to USD
    else if (symbol == 'k') {
        USD = money * 0.11;
    }
    // Pounds to USD
    else if (symbol == 'p') {
        USD = money * 1.22;
    }
    // Anything Else:
    else {
        std::cout << "Error. Incorrect money type input.\n";
        exit(EXIT_FAILURE);
    }

    std::cout << "You will receive " << USD << " USD in return for your " << money << symbol << '\n';
    // It should output a double not int. But does int for k and p.
    // BUG ^
    
    return 0;
}
I get decimal outputs when I run it. There's nothing wrong with the code, perhaps an old compiler?
Wait what exactly is the issue?
Input the balance of money you would like to convert followed by the symbol sign (y, k, or p): 11.44y
You will receive 0.107536 USD in return for your 11.44y


Edit: Are you saying you want it to show a .0 when the output happens to be a whole number?
Last edited on
I tried your code in VS 2019 and got the following output:
Input the balance of money you would like to convert followed by the symbol sign (y, k, or p): 18 y
You will receive 0.1692 USD in return for your 18y

Could you post a sample output? I'd bet you are entering a number that after calculation ends up with a decimal portion that is so small it gets trimmed off on output.

If you are going to use exit() and EXIT_FAILURE you should include <cstdlib>. A future version of your compiler may remove the implicit inclusion.
Obligatory warning: Money amounts should never be stored as floating point (double)! The only things here that can be stored as a double is the exchange rate itself, and the transient multiplication result before it is rounded and converted back into a currency.

Currency exchange rate rounding rules seem to say "apply the multiplication, and then round to 2 decimal places".
https://help-2018r1.acumatica.com/Wiki/ShowWiki.aspx?pageid=749a8ca0-64b6-4e9c-b0dd-d0b9fccf5578
57.785 EUR becomes 57.79
57.354 EUR becomes 57.35 EUR.)


Other countries/zones might have different rules, but I'd say the above is probably good enough for most purposes.


For example, here is a better way to store a currency. Note that this is still far from perfect, because you'll need something else to handle overflow, and it's not very user-input friendly, but it's a start.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>

using currency_type = long long int;
struct Currency {
    currency_type amount;  
};

std::ostream& print_usd(std::ostream& os, const Currency& currency)
{
    return os << currency.amount / 100 << '.'
        << std::setfill('0') << std::setw(2)
        << currency.amount % 100;
}

int main() {

    Currency c { 123407 };   
    print_usd(std::cout, c) << '\n';
    
    Currency c2 { 409 };
    print_usd(std::cout, c2) << '\n';
}

1234.07
4.09
Last edited on
Here are some examples of output:
Input the balance of money you would like to convert followed by the symbol sign (y, k, or p): 100k
You will receive 11 USD in return for your 100k
zapshe wrote:
I get decimal outputs when I run it. There's nothing wrong with the code, perhaps an old compiler?
Ganado wrote:
Edit: Are you saying you want it to show a .0 when the output happens to be a whole number?
Furry Guy wrote:
Could you post a sample output? I'd bet you are entering a number that after calculation ends up with a decimal portion that is so small it gets trimmed off on output.

Oh yeah it does work... I guess I just didn't realize the numbers I input were whole numbers. I thought they were being rounded to the nearest whole number yesterday. Whoops. Sorry everyone. Post solved. Thanks :P

Furry Guy wrote:
If you are going to use exit() and EXIT_FAILURE you should include <cstdlib>. A future version of your compiler may remove the implicit inclusion.

Oh okay, thank you for letting me know!

Ganado wrote:
Obligatory warning: Money amounts should never be stored as floating point (double)! The only things here that can be stored as a double is the exchange rate itself, and the transient multiplication result before it is rounded and converted back into a currency.

Currency exchange rate rounding rules seem to say "apply the multiplication, and then round to 2 decimal places".
https://help-2018r1.acumatica.com/Wiki/ShowWiki.aspx?pageid=749a8ca0-64b6-4e9c-b0dd-d0b9fccf5578
57.785 EUR becomes 57.79
57.354 EUR becomes 57.35 EUR.)


Other countries/zones might have different rules, but I'd say the above is probably good enough for most purposes.


For example, here is a better way to store a currency. Note that this is still far from perfect, because you'll need something else to handle overflow, and it's not very user-input friendly, but it's a start.
<code>
Ah okay, thanks for letting me know.
Last edited on
Topic archived. No new replies allowed.