//On a certain day the British pound was equivalent to $1.487 U.S., the French franc was
//$0.172, the German deutschemark was $0.584, and the Japanese yen was $0.00955.
//Write a program that allows the user to enter an amount in dollars, and then displays this
//value converted to these four other monetary units.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
float pound = 1.478;
float franc = 0.172;
float deutschemark = 0.584;
float yen = 0.00955;
int dollar;
int menu;
pound = dollar/pound;
franc = dollar/franc;
deutschemark = dollar/deutschemark;
yen = dollar/yen;
cout << "Money Changer:" << endl << endl;
cout << "1. Input ammount ($)" << endl << "2. Exit" << endl << "How can I help you: ";
cin >> menu; cout << endl;
if(menu==1)
{
cout << "Insert the ammount: ";
cin >> dollar; cout << endl;
cout << "The money you converted: " << endl;
cout << "Pound= " << setprecision(2) << pound << endl << "Franc= " << setprecision(2) << franc
<< endl <<"Deutschemark= " << setprecision(2) << deutschemark << endl << "Yen= " << setprecision(2) << yen << endl;
} elseif(menu==2)
{
cout << "Thanks for using our service." << endl;
}
else
{
cout << "Please select the menu number provided." << endl;
}
return 0;
}
Money Changer:
1. Input ammount ($)
2. Exit
How can I help you: 1
Insert the ammount: 10000
The money you converted:
Pound= 1.6e+006
Franc= 1.4e+007
Deutschemark= 4e+006
Yen= 2.5e+008
Process returned 0 (0x0) execution time : 3.539 s
Press any key to continue.
My question is:
1. How to display the decimal without the 'e'?
2. And also, how to display only the last 2 digits after the comma?
constfloat pound = 1.478;
constfloat franc = 0.172;
constfloat deutschemark = 0.584;
constfloat yen = 0.00955;
Don't attempt to assign any other value, for example don't do this: pound = dollar/pound;
Instead, define new variables for any subsequent calculations.
For a single calculation it would not matter very much, but if the user needed to enter a series of different dollar amounts, you'd want the original conversion rates to remain unaltered.
As well as that, you can't carry out the actual conversion until after line 32: cin >> dollar;
That is, until the user has entered the value for dollar, it isn't possible to convert that amount into any other currency.
as for the original question, try cout << fixed << setprecision(2);
All standard i/o manipulators which set or reset something, with the exception of setw, are "sticky" (i.e. do not reset after use); so the above could also be written as (with fixed added):
or even as a single cout statement, as you had originally.
1 2 3 4 5 6 7 8
cout << "The money you converted: " << endl
<< setprecision(2) << fixed
<< "Pound= " << p << endl
<< "Franc= " << f << endl
<< "Deutschemark= " << endl
<< "Yen= " << y << endl
<< endl;
// endls look better at the ends of lines to me...
Andy
PS
- the i/o manipulators that don't set/reset anything those which do something: endl, ends, flush and the new C++ manips: get_money, put_money, get_time, put_time.
- the sticky manipulators are all setting a flag
- setw behave the way is does as that's what ios_base method it calls (width) does itself.
1 2 3 4 5
cout << "set width to 8\n\"";
cout.width(8);
cout << f;
size_t w = cout.width();
cout << "\"\n\"width = " << w << "\"" << endl;