Thread: Currency Conversion Program

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    384

    Currency Conversion Program

    I wrote a Dollar/Euro/Yen Currency Conversion Program for the book and was wondering if I could make it simpler and more readable by splitting it up into separate functions (right now, it's all clogging up main, and I had to use multiple sets of if-else statements (one if statement nesting two conditions each for each currency case, and then an else condition to default to if the user provides "bad" input (such as trying to use Francs or something that isn't even a currency)).

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char currency;
        double money;
        cout << "Please enter amount of money, followed by currency it is in (D or d == Dollars, Y or y == Yen, E or e == Euros): \n";
        cin >> money >> currency;
        cin.ignore();
        double converted_money;
        if (currency == 'd' || currency == 'e' || currency == 'y' || currency == 'D' || currency == 'E' || currency == 'Y')
        {
            cout << "Enter currency to convert to: ";
            char desired_currency;
            cin >> desired_currency;
            cin.ignore();
            if (currency == 'd' || currency == 'D')
            {
                if (desired_currency == 'y' || desired_currency == 'Y')
                {
                    converted_money = money * 120.152;
                    cout << "Your amount converted to Yen is: " << converted_money << " Yen.\n";
                }
                else if (desired_currency == 'e' || desired_currency == 'E')
                {
                    converted_money = money / 0.927884;
                    cout << "Your amount converted to Euro is: " << converted_money << " Euros.\n";
                }
            }
            else if (currency == 'e' || currency == 'E')
            {
                if (desired_currency == 'd' || desired_currency == 'D')
                {
                    converted_money = money * 1.07781;
                    cout << "Your amount converted to Dollars is: " << converted_money << " Dollars.\n";
                }
                else if (desired_currency == 'y' || desired_currency == 'Y')
                {
                    converted_money = money * 129.500;
                    cout << "Your amount converted to Yen is: " << converted_money << " Yen.\n";
                }
            }
            else if (currency == 'y' || currency == 'Y')
            {
                if (desired_currency == 'd' || desired_currency == 'D')
                {
                    converted_money = money / 120.150;
                    cout << "Your amount converted to Dollars is: " << converted_money << " Dollars.\n";
                }
                else if (desired_currency == 'e' || desired_currency == 'E')
                {
                    converted_money = money / 129.521;
                    cout << "Your amount converted to Euro is: " << converted_money << " Euros.\n";
                }
            }
        }
        else
        {
            cout << "That's either invalid input or I don't know such a currency.\n";
            return 0;
        }
        cin.ignore();
    }
    Any help would be much appreciated.
    Last edited by Osman Zakir; 04-08-2015 at 03:59 PM.

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    I think this looks good Osman. The only thing I could think of in terms of restructuring, would be to store the conversion rates in one location. If all currencies maintained the same ratio to one another, you could convert the input to an imaginary currency first (only storing that ratio for each) and from that to the target currency, but afaik that's not how the real market behaves.

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    I think if you switch the order of getting money and currency you'll get a problem? Also you don't need the first large if statement since you catch garbage input with an else anyway, but you don't check if the target currency is garbage input. And yes what adrian said, looks good but declare the hardcoded rates as constants in one place.
    Last edited by jiggunjer; 04-08-2015 at 09:26 PM.

  4. #4
    Registered User
    Join Date
    Mar 2015
    Posts
    384
    But I do check if the user provided bad input for the original currency (the currency to convert from), although I guess I do need to also check if the target currency is bad input. And isn't there something I can do to make sure I don't get bad input for the amount of money? Like, if I want to make sure that I get a number (checking for negative numbers would come after that).

    And I'd rather not use a constant variable for the rates.

  5. #5
    Registered User
    Join Date
    Mar 2015
    Posts
    384
    I don't see an "Edit" link appearing in my actual post, so I decided to make a double post - any mod or admin who sees this, please merge it with the post right before it. Thank you very much.

    I also need to write a program that implements a square function without the use of the multiplication operator (it has to use multiple addition to achieve the desired result - which I honestly don't know how to do).

    And I also have to do the same thing this code is doing, but I have to use for-loops instead of while-loops:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int i = 97;
        char c = 'a';
        while (i <= 122 && c <= 'z')
        {
            cout << c << "\t" << i << "\n";
            i++;
            c++;
        }
        cout << "\n";
        int i2 = 65;
        char c2 = 'A';
        while (i2 <= 90 && c2 <= 'Z')
        {
            cout << c2 << "\t" << i2 << "\n";
            i2++;
            c2++;
        }
        cin.ignore();
    }
    And as for the currency conversion, I'd apparently misinterpreted it; I had to just write a program that converts Yen, Pounds, and Euro into USD. And now I have to add two more currencies and modify the code to switch-case statements instead of if-else statements and think about how much easier it'd be to read and modify it or whatnot.

    It'd probably still be good practice to write a successful program that converts between Dollars, Euros, Yen, and Pounds both ways. I don't really need to do that for the exercises in the book so far, though.

    Edit:

    Program that implements that square() function I mentioned - tell me if there's a way to do this without using the multiplication operator at all:
    Code:
    #include <iostream>
    #include <cmath>
    
    int square(int number);
    
    using namespace std;
    
    int main()
    {
        for (int i = 0; i < 100; i++)
        {
            cout << i << "\t" << square(i) << "\n";
        }
        cin.ignore();
    }
    
    int square(int number)
    {
        int result = 0;
        result += pow(number, 2);
        return result;
    }
    And as for the Currency Converter for converting to Dollars from other currencies using switch-case (modified version from the if-else version):
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char currency;
        double money;
        cout << "Please enter amount of money, followed by currency it is in\n";
        cout << "(P or p == Pounds, Y or y == Yen, E or e == Euros, C or c == CNY (Yuan), N or n == NOK (Kroner)): \n";
        cin >> money >> currency;
        cin.ignore();
        double converted_money;
        switch (currency)
        {
        case 'P':
        case 'p':
            converted_money = money / 0.679872;
            cout << "Your amount converted from Pounds to Dollars is: " << converted_money << " Dollars\n";
            break;
        case 'Y':
        case 'y':
            converted_money = money / 120.572;
            cout << "Your amount converted from Yen to Dollars is: " << converted_money << " Dollars\n";
            break;
        case 'E':
        case 'e':
            converted_money = money / 0.937789;
            cout << "Your amount converted from Euro to Dollars is: " << converted_money << " Dollars\n";
            break;
        case 'C':
        case 'c':
            converted_money = money / 6.20640;
            cout << "Your amount converted from Yuan to Dollars is: " << converted_money << "Dollar\n";
            break;
        case 'N':
        case 'n':
            converted_money = money / 8.10159;
            cout << "Your amount converted from Kroner to Dollars is: " << converted_money << " Dollars\n";
            break;
        default:
            cout << "That is either invalid input, or I don't know such a currency.\n";
            cout << "Please note that this program also doesn't convert Dollars into any currency, it only converts to Dollars\n";
            cout << " from the currencies mentioned above.\n";
            break;
            return 1;
        }
        cin.ignore();
    }
    Last edited by Osman Zakir; 04-09-2015 at 04:14 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Osman Zakir View Post
    Program that implements that square() function I mentioned - tell me if there's a way to do this without using the multiplication operator at all:
    I don't think your square method is right. It's essentially just doing exactly what pow(number, 2) does. As for doing it without multiplication... of course it's possible. Think back to basic algebra.
    5^2 == 5*5 == 5+5+5+5+5
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2015
    Posts
    384
    Yeah, but how do I do it so that every number from 1 through 99 gets correctly squared, but without me having hard-code everything (i.e. can't I do it through a loop?)?

    And how about the letter ASCII table? I tried doing it with a for-loop, but I got some weird output; the letters repeated two or three times and the numbers weren't corresponding with letters correctly. I need to know of a way to do it with a for-loop correctly.

  8. #8
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    Always see if you can split your problems into smaller parts, it's the only way to stay sane.

    If you look at the 5 example by Elysia, which amounts to:
    Code:
    int n = 5;
    n += 5;
    n += 5;
    n += 5;
    n += 5;
    How would you do that in a loop? Think about it.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Break it down into two steps:
    - First, how do you multiply two numbers? Write a routine for it.
    - Second, how do you square a number? Write a routine that uses the multiply routine.

    For the ASCII program, post your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    In:
    Code:
    default:
            cout << "That is either invalid input, or I don't know such a currency.\n";
            cout << "Please note that this program also doesn't convert Dollars into any currency, it only converts to Dollars\n";
            cout << " from the currencies mentioned above.\n";
            break;
            return 1;
        }
    I think the return 1 statement is dead code.

    As for your square program, aside from using pow() it is needlessly verbose.

    Code:
    int square(int number)
    {
      return pow(number,2);
    }
    should also do the job, if I haven't made a typo.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to do this currency conversion
    By rashid in forum C Programming
    Replies: 2
    Last Post: 02-22-2010, 10:41 PM
  2. A simple currency conversion program gone south!!
    By lildroopie in forum C Programming
    Replies: 1
    Last Post: 02-01-2009, 12:45 PM
  3. Currency Conversion
    By roadrunnr70 in forum C Programming
    Replies: 8
    Last Post: 12-16-2004, 03:47 PM
  4. Currency Conversion Program
    By Dangerous_Dave in forum C Programming
    Replies: 2
    Last Post: 11-11-2003, 08:54 PM
  5. Currency Conversion Program
    By kgraw21 in forum C Programming
    Replies: 5
    Last Post: 04-19-2002, 08:39 AM