Please help me, about decimal in C++..

So, i wrote this code:

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
44
45
46
47
48
49
50
51
 //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>

using namespace 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;
    } else

    if(menu==2)
    {
        cout << "Thanks for using our service." << endl;
    }

    else
    {
        cout << "Please select the menu number provided." << endl;
    }

    return 0;
}


and the output:

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?

Thanks,
God bless you
that shouldn't be your main concern.

You have not initialised dollar, which is why your maths is cack.
Last edited on
Any other else want to help me?
READ MY POST.
Or do you seriously think 10 thousand dollars is the equivalent of 1.6 million english pounds?
I would make these values into constants:
1
2
3
4
    const float pound = 1.478;
    const float franc = 0.172;
    const float deutschemark = 0.584;
    const float 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);
Last edited on
he's doing the calculations before he's asked the user for input :s
Oh my god mutexe was right.. Im sorry, im really sorry, hahahaha thx mutexe
Wait a minute, question number two still a problem. So i change the code like this:

1
2
3
4
5
6
cout << "The money you converted: " << endl;
cout << "Pound= " << p << setprecision(2); 
cout << endl << "Franc= " << f << setprecision(2);
cout << endl <<"Deutschemark= " << d << setprecision(2);
cout << endl << "Yen= " << y << setprecision(2);
cout << endl;


and still not showing only the last 2 digits :S
Last edited on
You missed part of my previous comment:
cout << fixed;
Also, this must go before the actual values you wish to output, not afterwards.
Last edited on
Thanks Chervil, God bless you :)
BTW...

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):

1
2
3
4
5
6
7
cout << "The money you converted: " << endl;
cout << setprecision(2) << fixed;
cout << "Pound= " << p; 
cout << endl << "Franc= " << f;
cout << endl <<"Deutschemark= " << d;
cout << endl << "Yen= " << y;
cout << endl;


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;


set width to 8
"     2.3"
"width = 0"

Last edited on
Topic archived. No new replies allowed.