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!
/*
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
elseif (symbol == 'k') {
USD = money * 0.11;
}
// Pounds to USD
elseif (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;
}
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?
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.
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.
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.
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>