Thread: How do I make a Currency Converter.(any help greatly appreciated)

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    2

    How do I make a Currency Converter.(any help greatly appreciated)

    here is my code so far i dont know if im going in the right direct or not any help is much appreciated
    Code:
     
    #include<stdio.h>
    #include<conio.h>
    int main()
    {      
          const float USD_CAD = 0.99; //Declare constants for USD to currencies.
          const float USD_EUR = 0.75;
          const float USD_HKD = 7.75;
          const float USD_BPS = 0.63;
          const float USD_MEP = 12.59;
          const float USD_INR = 54.35;
                float inputUSD;
                
          const float CAD_USD = 1.02; //Declare constants for CAD to currencies.
          const float CAD_EUR = 0.76;
          const float CAD_HKD = 7.87;
          const float CAD_BPS = 0.63;
          const float CAD_MEP = 12.77;
          const float CAD_INR = 55.17;
                float inputCAD;
                
          const float EUR_CAD = 1.32; //Declare constants for EUR to currencies.
          const float EUR_USD = 1.34;
          const float EUR_HKD = 10.38;
          const float EUR_BPS = 0.84;
          const float EUR_MEP = 16.82;
          const float EUR_INR = 72.75;
                float inputEUR;
                
          const float HKD_CAD = 0.13; //Declare constants for HKD to currencies.
          const float HKD_EUR = 0.10;
          const float HKD_USD = 0.13;
          const float HKD_BPS = 0.08;
          const float HKD_MEP = 1.62;
          const float HKD_INR = 7.01;
                float inputHKD;
          
          const float BPS_CAD = 1.58; //Declare constants for BPS to currencies.
          const float BPS_EUR = 1.20;
          const float BPS_HKD = 12.41;
          const float BPS_USD = 1.60;
          const float BPS_MEP = 20.12;
          const float BPS_INR = 87.00;
                float inputBPS;
                
          const float MEP_CAD = 0.08; //Declare constants for MEP to currencies.
          const float MEP_EUR = 0.06;
          const float MEP_HKD = 0.62;
          const float MEP_BPS = 0.05;
          const float MEP_USD = 0.08;
          const float MEP_INR = 4.33;
                float inputMEP;
                
          const float INR_CAD = 0.02; //Declare constants for INR to currencies.
          const float INR_EUR = 0.01;
          const float INR_HKD = 0.14;
          const float INR_BPS = 0.01;
          const float INR_MEP = 0.23;
          const float INR_USD = 0.02;
                float inputINR;
          
          float inputUSD_CAD = inputUSD * USD_CAD;
          float inputUSD_EUR = inputUSD * USD_EUR;
          float inputUSD_HKD = inputUSD * USD_HKD;
          float inputUSD_BPS = inputUSD * USD_BPS;
          float inputUSD_MEP = inputUSD * USD_MEP;
          float inputUSD_INR = inputUSD * USD_INR;
    printf("      USD  CAD  EUR  HKD  BPS  MEP  INR\n");
    printf("\nUSD  %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",inputUSD,inputUSD_CAD,inputUSD_EUR,inputUSD_HKD,inputUSD_BPS,inputUSD_MEP,inputUSD_INR);
    printf("\nCAD  %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n");
    printf("\nEUR  %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n");
    printf("\nHKD  %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n");
    printf("\nBPS  %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n");
    printf("\nMEP  %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n");
    printf("\nINR  %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n");
        
    
    getch();
    return 0;
    }
    btw new to c and c++ again thank you for any help

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Given the variation of exchange rates, I would suggest those cannot be fixed constants - they also need to be inputs to the program (although possibly not user inputs - they might be retrieved from another source, such as a database). Bear in mind, also, that converting from currency A to currency B, the rates are reciprocal (eg if an A is 1.5 B then a B is 2/3 A (1/1.5)).

    There is a slight inconvenience that your program is not reading in any values, and is doing calculations with uninitialised variables.

    At a slightly more advanced level, floating point variables should not be used to represent currency. A floating point type cannot represent a value of 0.10 exactly. That gives all sorts of problems when adding currencies, multiplying by exchange rates, etc since errors will come into the calculation. Possibly better to represent an exchange rate as a rational number (ratio of two integers) and a currency itself as a data structure (say, with fields like dollars and cents or - more generally - main currency unit and fraction parts, such as pound and pence).

    When doing currency exchanges, there also tend to be fees and commissions. These may be fixed amounts, or they may be percentages. Or a combination (eg a sliding scale). Change US$1 to another currency and then change it immediately back at the same rate, the amount you will have in your hand is less than US$1.

    Lastly, the function getch() and the header file <conio.h> is not standard C. It is a vendor-specific extension.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, luckyRob!

    It is however, YOUR job to know and tell us what is wrong with your program. Just "I don't know", leaves us to spend time doing things that you already know. Which is why we won't do it (generally). If you don't care enough to lay out the specifics, we don't care enough to spend our time helping.

    I will mention that doubles are a lot more accurate than floats. If you're going to use a floating point data type, those are the one's you want for accuracy.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    The first thing I'd do if I were you is make a menu titled, "What currency do you have?" and a list of available currencies.


    After that, a second menu titled, "What currency do you want to convert to?" and a list of available currencies.
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    2
    Okay guys sorry for everything here is the intended goal, and being new to the whole aspect of programming not really sure how to get there so any tips would be greatly appreciated looking at book at the moment and have been for past 2 hours really isnt getting me anywhere so trying this thanks for the welcome btw.

    Write a computer program in C which will request the user to enter a number representing the amount of foreign currency he/she would like to exchange. Your program should output a foreign currency conversion table for at least 6 different currencies, showing how much of each type of foreign currency can be obtained for the amount the user enters.


    A foreign currency exchange table for one unit of foreign currency. Your program is interactive and so should work for any input amount from the user not just 1.00. In the example table, the number entered by the user is 1, and it shows that 1 USD is equal to 0.547525 GBP, while 1 GBP is equal to 2.11928 AUD and so forth. If the user enters 2, your table should show how much 2 USD is in GBP, CAD, EUR, etc. along with how much 2 CAD would be in EUR, GBP, USD, etc, for each of the 6 currencies. ASCII Art must be used.

    oh and
    All exchange rates in your program must be current and coded as constants

    The program its self nothing is wrong with it in sense its my coding that is wrong because i honestly dont know how to really go about the problem I thought that if I code the currencies I could do it that way ,but when I did i just got 0's and nothing worked out which is why I was being so broad just looking for anything. Sorry again for the Trouble and thank you for any time spent and help gave.
    Last edited by luckyrob; 01-17-2013 at 07:40 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Start at the beginning. Ask the user the amount they would like to exchange, and what the type of currency it will be. Then print out your table of six currencies, based on that amount. That should be a double, at least. Preferably a 100th of a dollar would be used, in any currency, so there will be no rounding errors.

    The ascii art you need will probably be found in 156-159 in the extended character set you use. printf("%c",156), etc., will show you a few, but it's best to print all of them for your own character set, because there ARE differences from one system to the next.

    It would greatly help you if you did this exercise on paper first, THEN see the logic you used to do it. Maybe repeat it a few times, while looking at the assignment carefully. Use that logic for your code.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Notice your printf statements in your first post. They form a nice little table, or in programming speak, a 2-D array. Consider declaring a 2-d array to store all the conversion values. An example:
    Code:
    enum {
        USD,  // 0
        CAD,  // 1
        EUR,  // ...
        HKD,
        BPS,
        MEP,
        INR,
        NUM_CURRENCIES  // 7
    };
    double exchange_rates[NUM_CURRENCIES][NUM_CURRENCIES] = {
        // values taken from your first code sample
        {1, 0.99, 0.75, 7.75, 0.63, 12.59, 54.35},  // from USD to USD, CAD, EUR, HKD, BPS, MEP, INR
        {1.02, 1, 0.76, 7.87, 0.63, 12.77, 55.17},  // from CAD to USD, CAD, EUR, HKD, BPS, MEP, INR
        ...
        {0.02, 0.01, 0.14, 0.01, 0.23, 0.02, 1}   // from INR to USD, CAD, EUR, HKD, BPS, MEP, INR
    };
    Each row is the "from" currency, corresponding to the enum values, and each column is the "to" currency. Thus, to find the exchange rate from USD (0) to HKD (3), you simply do exchange_rates[0][3]. That's the number you multiply your starting currency (in USD) by to get the equivalent in HKD. Also notice that I introduced a 1 along the diagonal, everywhere the row and column number is the same (exchange_rates[0][0], exchange_rates[1][1], etc). That is basically saying that USD to USD, CAD to CAD, etc is an exchange rate of 1:1. That keeps everything lined up, and also gives sensible information, in case anybody really is curious about the USD to USD conversion rate.

    Then, you can take your starting value and currency, say $42.17 USD, and show all the conversions with a simple for loop that runs through each column in the "from" currency row. You might also not want to output a conversion when the from and to are the same. I'm sure you can figure that out without too much trouble.

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    Quote Originally Posted by anduril462 View Post
    Consider declaring a 2-d array to store all the conversion values.
    he is already confused on asking for the inputs, and then the "formula" to convert it....i think the array might be a bit much for him for now!!!

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    It's not necessary to have a NxN array of conversion factors. Just pick one currency as a baseline and reference all others to that. When calculating a from-to cell for the output just calculate the factor on-the-spot.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by nonoob View Post
    It's not necessary to have a NxN array of conversion factors. Just pick one currency as a baseline and reference all others to that. When calculating a from-to cell for the output just calculate the factor on-the-spot.
    Actually, it is. That is a big part of how foreign currency traders make their money, shifting it around taking advantage of exchange rates. There is no guarantee that going from EUR to USD to INR is equivalent to going from EUR directly to INR. Those differences are usually very minor, but they often make a difference in the end result. Checking this on Google, for example, gave:

    10.00 EUR -> 13.32 USD
    13.32 USD -> 717.22 INR

    10.00 EUR -> 717.00 INR

    That is with whatever rates Google is pulling from it's sources, should be fairly current and accurate. Try it yourself to see, you'll probably be able to find a difference with little trouble, though sometimes it is equivalent, depending on the conversions and baselines. EUR->USD->GBP and EUR->GBP gave me the same end result when I tried. That may have changed by the time you try it though, the rates changes quickly.

  11. #11
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Code:
    printf("\nUSD  %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",inputUSD,inputUSD_CAD,inputUSD_EUR,inputUSD_HKD,inputUSD_BPS,inputUSD_MEP,inputUSD_INR);
    printf("\nCAD  %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n");
    The first line looks OK but the second line seems somewhat lacking in parameters.
    I'd be surprised if it does not produce odd results.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-17-2010, 02:15 PM
  2. Lost on Queues - Help GREATLY appreciated.
    By TetsuoShima in forum C Programming
    Replies: 3
    Last Post: 07-13-2010, 05:35 AM
  3. Stuck......help greatly appreciated.
    By matt.s in forum C Programming
    Replies: 5
    Last Post: 10-14-2009, 06:10 PM
  4. Any assistance would be greatly appreciated
    By iiwhitexb0iii in forum C Programming
    Replies: 18
    Last Post: 02-26-2006, 12:06 PM
  5. Your help would be greatly appreciated
    By Sway2 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2002, 07:55 AM

Tags for this Thread