Currency Converter


General Statement: Convert a given US dollar amount to Pounds, Lira, Francs, Marks, or Yen.

Input: The first line in the data set is an integer that represents the number of data collections that follow. Each collection consists of an integer US dollar amount followed by the name of the currency to be converted to.

Output: Output a dollar sign ($), the US dollar amount and the words CONVERTS TO and then the converted amount followed by the currency unit name.
If the conversion factor is an integer, output an integer. If it is a decimal, round to 2 decimal places.
The output is to be formatted exactly like that for the sample output given below.

Assumptions: The US dollar amount is an integer in the range 1..500.
All letters are upper case.

Discussion: The conversion factors for $1 are:
0.84 POUNDS
2040 LIRA
9.85 FRANCS
3.23 MARKS
260 YEN

Sample Input:
3
17 POUNDS
25 MARKS
228 YEN

Sample Output:
$17 CONVERTS TO 14.28 POUNDS
$25 CONVERTS TO 80.75 MARKS
$228 CONVERTS TO 59280 YEN

Solutions :

#include <iostream>
using namespace std;

void currency_converter (int x, string str)
{
    if ( str == "POUNDS" )
        cout << x * 0.84 << " POUNDS";

    else if ( str == "LIRA" )
        cout << x * 2040 << " LIRA";

    else if ( str == "FRANCS" )
        cout << x * 9.85 << " FRANCS";

    else if ( str == "MARKS" )
        cout << x * 3.23 << " MARKS";

    else
        cout << x * 260 << " YEN";

    cout << endl;
}

int main ()
{
    int dataset;
    scanf ("%d", &dataset);

    while ( dataset-- ) {
        int amount;
        string currency;

        cin >> amount >> currency;

        cout << "$" << amount << " CONVERTS TO ";

        currency_converter (amount, currency);
    }

    return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s