Thread: Currency Conversion Program

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    4

    Currency Conversion Program

    Hi all,

    I'm new here but im just interested in some input into my program. Its for a college assignemt but i have done C programming before. Im just wondering if any of you guys would do it a diffrent way?

    I have the Euro to Pound section workin and the rest is simply copy and paste so im sorted. But as i said just some input and perhaps some ideas on how to do it other ways.

    Thanks in advance,



    Code:
    #include <stdio.h>
    
    
    int main()
    
    	{
    
       #define POUND 0.70
       #define KRONE 7.43
       #define YEN 127
    
    
    
       int menu_choice;
       float local_amount, remote_amount;
    
       /* Display Menu System */
       printf("1. Britain\n");
       printf("2. Denmark\n");
       printf("3. Japan\n");
       printf("4. USA\n");
       printf("5. Exit Program\n\n");
    
       /* Ask user for there choice */
       /* And Asign the choice to varabial manu_choice, which is of int data type */
    
       printf("Please Enter Your Country Selection From The Menu: \t");
       scanf("%d",&menu_choice);
    
       if (menu_choice == 1)
       {
          printf("\n\nDo you wish to convert from:\n\n");
          printf("1. Euro to Pound(Sterling)\n");
          printf("2. Pound(Sterling) to Euro\n\n");
          printf("Please Enter Your Choice From The Menu: \t");
          scanf("%d",&menu_choice);
    
          if (menu_choice == 1)
          {
          	printf("\n\nEuro to Pound(Sterling):\n\n");
    
             printf("Please Enter Euro Amount to be Converted: \t");
             scanf("%f",&local_amount);
    
             remote_amount = local_amount * POUND;
    
             printf("\n%f Euro is equal to %f Pound",local_amount, remote_amount);
    
             menu_choice = 0;
    
          }
    
          if (menu_choice == 2)
          {
    
          	printf("\n\nPound(Sterling) to Euro:\n\n");
    
             printf("Please Enter Pound(Sterling) Amount to be Converted: \t");
             scanf("%f",&local_amount);
    
             remote_amount = local_amount / POUND;
    
             printf("\n%f Pound is equal to %f Euro",local_amount, remote_amount);
    
             menu_choice = 0;
    
          }
    
       }
    
       if (menu_choice == 2)
       {
       	printf("2. Denmark\n");
       }
    
       if (menu_choice == 3)
       {
       	printf("3. Japan\n");
       }
    
       if (menu_choice == 4)
       {
       	printf("4. USA\n");
       }
    
       if (menu_choice == 5)
       {
       	printf("5. Thank You and Good Bye!\n");
       }
    
       return 0;
    
       }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Might look into using switch case instead of ifs.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Im just wondering if any of you guys would do it a diffrent way?
    >But as i said just some input and perhaps some ideas on how to do it other ways.

    When this question came up once before, I tried something like this.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    const char Description[] =
    "Currency Conversion As of June 11, 2003 2:35am GMT";
    
    void convert(const char *from, const char *to, double amount)
    {
       static const struct
       {
          const char *name;
          double rate;
       } Xchg[] = /* US Dollar conversion factor */
       {
          { "USD",      1.0         },
          { "Lire",     0.000603049 },
          { "Yen",      0.008464602 },
          { "Pound",    1.65123     },
          { "Peso",     0.0940910   },
          { "Canadian", 0.734061    },
       };
       size_t i,j;
    
       for ( i = 0; i < sizeof(Xchg)/sizeof(*Xchg); ++i )
       {
          if ( strcmp(Xchg[i].name, from) == 0 )
          {
             for ( j = 0; j < sizeof(Xchg)/sizeof(*Xchg); ++j )
             {
                if ( strcmp(Xchg[j].name, to) == 0 )
                {
                   double value = amount * Xchg[i].rate / Xchg[j].rate;
                   printf("%g %s = %g %s\n", amount, from, value, to);
                   return;
                }
             }
             printf("Cannot convert to \"%s\"\a\n", to);
             return;
          }
       }
       printf("Cannot convert from \"%s\"\a\n", from);
    }
    
    int main(void)
    {
       puts(Description);
       convert("USD",  "Pound",  20.0);
       convert("Yen",  "Lire",  100.0);
       convert("Euro", "Krone",  20.0);
       convert("USD",  "Krone",  20.0);
       return 0;
    }
    
    /* my output
    Currency Conversion As of June 11, 2003 2:35am GMT
    20 USD = 12.1122 Pound
    100 Yen = 1403.63 Lire
    Cannot convert from "Euro"
    Cannot convert to "Krone"
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  2. Minute to hour/minute conversion program
    By Remius in forum C Programming
    Replies: 7
    Last Post: 12-29-2007, 08:39 AM
  3. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 01:21 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Help with variables (newbee)
    By stugatza in forum C Programming
    Replies: 7
    Last Post: 08-18-2004, 10:40 AM