Thread: Currency Converter

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    4

    Currency Converter

    Basic homework problem. I did all of this, and it works fine. It gives the right numbers in the right format. And then I looked back at the problem statement and it said I was supposed to use if-else-if statements -within- the switch to do the actual conversions, which doesn't make much sense to me. All of the other parameters seem to work out, any suggestions on how to redo it with if-else-if?

    Code:
    #include <stdio.h>
    
    int main()
    {
    const double CAD = 1.0207;
    const double EUR = 1.2926;
    const double GBP = 1.6186;
    const double CLP = 0.0021;
    const double CNY = 0.1584;
    const double ZAR = 0.1208;
    
    int code;
    double cad;
    double eur;
    double gbp;
    double clp;
    double cny;
    double zar;
    int count = 0;
    
        printf("\nEnter country code, zero to end: ");
        scanf("%d", &code);
    
        while (code != 0)
      {
         switch (code)
       {  case 1:
              printf("Enter CAD: ");
              scanf("%lf", &cad);
              printf("%.2f CAD = ", cad);
              cad = cad * CAD;
              printf("%.2f USD\n", cad);
              count = count + 1;
          break; 
          case 2:
              printf("Enter EUR: ");
              scanf("%lf", &eur);
              printf("%.2f EUR = ", eur);
              eur = eur * EUR;
              printf("%.2f USD\n", eur);
              count = count + 1;
          case 3:
              printf("Enter GBP: ");
              scanf("%lf", &gbp);   
              printf("%.2f GBP = ", gbp);
              gbp = gbp * GBP;
              printf("%.2f USD\n", gbp);
              count = count + 1;
          break;
          case 4:
              printf("Enter CLP: ");
              scanf("%lf", &clp);   
              printf("%.2f CLP = ", clp);
              clp = clp * CLP;
              printf("%.2f USD\n", clp);
              count = count + 1;
            break;
            case 5:
              printf("Enter CNY: ");
              scanf("%lf", &cny);   
              printf("%.2f CNY = ", cny);
              cny = cny * CNY;
              printf("%.2f USD\n", cny);
              count = count + 1;
          break;
          case 6:
              printf("Enter ZAR: ");
              scanf("%lf", &zar);   
              printf("%.2f ZAR = ", zar);
              zar = zar * ZAR;
              printf("%.2f USD\n", zar);
              count = count + 1;
          break;
          default:
              printf("%d is an invalid country code!", code);
          break;
        }
        printf("\nEneter country code, zero to end: ");
        scanf("%d", &code);   
      }
        printf("\n%d conversions done.\n\n", count);
            return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I don't see any need to use more if-than-else statements. If you want to consider an even more compact program the 'switch' isn't necessary if you keep the conversion factors in an array and index it according to currency code. No need for separate variables 'cad', 'eur', 'gbp', etc.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    4
    I don't see a need for it either, which is why it's confusing. But that's the way the assignment wants it, if-else-if within the switch within the while loop. I don't know what variables I would even put into if-else-if statements for something that's just a conversion.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Can you post the problem statement.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    4
    1. General:
    1.1. Save your code as prog2.c.
    1.2. Named constants must be used to represent the exchange rates. Use data type double to hold the conversion factors and currency amounts.
    1.3. Follow the line spacing and format shown in the sample output. All currencies are printed to two decimal places.
    2. main:
    2.1. Defines named constants and then variables.
    2.2. Priming prompt and read if required.
    2.3. Use a while loop to perform the conversions as long as the country code is not zero. Inside the loop:
    2.3.1. If the country code is invalid print the error message otherwise:
    2.3.1.1. Use a switch statement to prompt for the currency amount.
    2.3.1.2. Read the currency amount.
    2.3.1.3. Convert to USD using an if-else-if statement.
    2.3.1.4. Print the result of the conversion using either an if-else-if or switch (if either required) for the currency designation.
    2.3.2. Perform a subsequent prompt and read if required.
    2.4. Print a blank line, the count of the number of conversions done, and an ending blank line.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Ahh, see, you did the prompting, converting, and printing all within a single "switch()" statement - which is a very good implementation.

    But the assignment seems to require these be broken down into separate steps.

    Code:
    switch(...)
        // prompt for currency
    if(...)
        // convert to USD
    switch(...)
        // print result

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    4
    Oh, I see. Yeah, that makes more sense, I think I have it figured out now. Thanks! =]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converter currency in C
    By MyRedz in forum C Programming
    Replies: 7
    Last Post: 10-16-2008, 10:39 PM
  2. Basic C Help - Currency Converter
    By liljp617 in forum C Programming
    Replies: 9
    Last Post: 09-07-2008, 10:12 PM
  3. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 01:21 PM
  4. Currency Converter
    By mikmac in forum C Programming
    Replies: 3
    Last Post: 06-10-2003, 11:50 PM
  5. currency converter
    By Shalinee in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2003, 03:27 AM