Currency Converter

Dave Sinkula 0 Tallied Votes 242 Views Share

This problem is a typical homework question. The code presented here may contain some elements that may be a little "ahead of the game" in coursework, but it presents some of the basic elements that might be used in such an exercise.

#include <stdio.h>
#include <string.h>

const char Description[] =
"Currency Conversion As of June 11, 2003 2:35am GMT";

void convert(double amount, const char *from, const char *to)
{
   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;
   /*
    * Search for the name of the currency you want to convert from.
    */
   for ( i = 0; i < sizeof Xchg / sizeof *Xchg; ++i )
   {
      if ( strcmp(Xchg[i].name, from) == 0 )
      {
         /*
          * Found the 'from' currency name; 'i' is the index of it.
          * Search for the name of the currency you want to convert to.
          */
         for ( j = 0; j < sizeof Xchg / sizeof *Xchg; ++j )
         {
            if ( strcmp(Xchg[j].name, to) == 0 )
            {
               /*
                * Found the 'to' currency name; 'j' is the index of it.
                * Calculate the new value: multiply by the 'from' rate and
                *                          divide by the 'to' rate.
                */
               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); /* 'to' name not found */
         return;
      }
   }
   printf("Cannot convert from \"%s\"\a\n", from); /* 'from' name not found */
}

int main(void)
{
   puts(Description);
   convert( 20.0, "USD",  "Pound");
   convert(100.0, "Yen",  "Lire" );
   convert( 20.0, "Euro", "Krone");
   convert( 20.0, "USD",  "Krone");
   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"
*/
bumsfeld 413 Nearly a Posting Virtuoso

David, you know how to explane things good!

Sealteam56 0 Newbie Poster

Hello,

I am new to this forum but noticed you all seem very helpful. I am learning C-programming and been given a task to complete a currency conversion program. I have about exhasuted all my level of knowledge and have used up the books I have as well.

If someone here could please review this program and perhaps let me know what I have done wrong here that would be great.

The issue is that the program is to allow for a user to select a currency value from the displayed list, which as far as I can tell it will.

Than the user is to input a value to be converted, it is here that I have missed something as everytime I now enter a value I get back my error message which was coded, at least was my plan to check for only numeric values.

Again any help would be great!!!

/*The conversion rates of five currencies equivalent to one US dollar*/ 
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int choice;
    int currency;
    float USD, UK, HK, EUR, NZ, AUS;
    char buffer[30];
    
     /********************************************
    ******Current Currency Exchange Rates*******
    *
    * British Pound(UK) 0.5408  6/15 8:50pm 
    * Hong Kong Dollar (HK) 7.7617  6/15 8:50pm 
    * Euro (EUR) 0.7914  6/15 8:50pm 
    * New Zealand (NZ) Dollar 1.6079  6/15 8:50pm 
    * Australian Dollar (AUS) 1.3556 6/15 8:50pm
    * United States Dollar (US)
    *
     **********************************************/
     
   
    USD = 1.00;
    UK = 0.5408;
    HK = 7.7617;
    EUR = 0.7914;
    NZ = 1.6079;
    AUS = 1.3556;
    
    do  // main loop runs until choice 0 is entered
    {
        /*Version and title of program and menu selection*/
        
        printf("\n\n        Welcome to Currency Conversion Utility v. 4.2.0 \n\n\n");
    
        printf("Please select the currency type you would like to convert into US dollars.\n\n");
    
        printf("1. for British Pounds \n");
        printf("2. for Hong Kong Dollars \n");
        printf("3. for Euros \n");
        printf("4. for New Zealand Dollars \n");
        printf("5. for Australian Dollars \n\n");
    
    
    
        printf("Press 0 and then return to exit the Currency Converter \n");
 
        choice = 6;  /* must be initialized to an invalid value*/ 
                     /* it's not initialized the first time, it might be in the range of 1..5*/
        do     /* 1st input loop for the choice*/
        {
            printf("\n\nEnter the number of your choice: Than hit Enter \n\n\n");
            /*reads the characters from the file associated with fp into the string pointed to.*/
 fgets(buffer, sizeof(buffer),  stdin);
         /* Error Checking for Numeric Values only*/
            if ( ( sscanf(buffer, "%u", &choice) == 0) || choice > 5 || choice < 0 ) 
        
            {
                printf("\n\n\a                  Error please select agian  \n");
            }
        }
        while(choice < 0 || choice > 5);
        
        if ( choice > 0 ) 
         {  /* no need to ask for the amount if exit was selected*/
            
            currency = 100000;  // see above
            
            do    /* 2nd input loop for the amount*/
             {
       
                printf("\n\n\nEnter the amount you want to convert to U.S. Dollars: Than hit Enter \n\n");
       
                fgets(buffer, sizeof(buffer),  stdin);
                
                /* Error Checking for Numeric Values only*/
                if ( ( sscanf(buffer, "%d", &currency) == 0) || currency > 99999 || currency < 1 )
                {
                    printf("\n\n\a       This is an Errneous amount based on your input\n"); 
                    printf("       please enter a numeric value");
                }
            }      
            while(currency > 99999 || currency < 1);  /* changed the condition to int values*/
        }
        
        switch(choice) 
        {
      
            case 1:
                printf("\n\n         You will have %.2f US dollars \n\n", USD/UK*currency);
                break;       
            case 2:  
                printf("\n\n         You will have %.2f US dollars \n\n", USD/HK*currency);
                break;
            case 3:
                printf("\n\n         You will have %.2f US dollars\n\n", USD/EUR*currency); 
                break;
            case 4:
                printf("\n\n         You will have %.2f US dollars \n\n", USD/NZ*currency); 
                break;
            case 5:
                printf("\n\n         You will have %.2f US dollars \n\n", USD/AUS*currency); 
                break;
            case 0:
                break;
                return 0;
        }
    
 } 
     
    while(choice!=0 && currency != 0); /* saves the second switch.*/
                                        /* the switch would not work if I had changed currency to float*/
    puts("bye.");
    puts("press enter.");
    getchar(); 
    
    return 0;
}

I think what is possibly the error is that the value inputted is not viewed by the code as a valid input no matter what is entered.

Allow me to thank you now for your help.

Randy
:rolleyes:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.