Thread: Currency Conversion - Function Problems

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

    Currency Conversion - Function Problems

    Code:
    /*Currency Conv Control Module*/
    #include <stdio.h>
    #include "currencyconv.h" /* defines constants, declares functions */
    
    int main(void)
    
    {
    
    float us_dollars;
    float currency;
    int user_input;
        while ((user_input = menu()) != QUIT)
        {
            switch(user_input)
        {
                case 1 : currency = EUR;
                break;
                case 2 : currency = GBP;
                break;
                case 3 : currency = INR;
                break;
                case 4 : currency = CAD;
                break;
                case 5 : currency = AUD;
                break;
        }
    
    us_dollars = getus_dollars();
    convert_rate(currency, us_dollars);
    
    }
    
    printf("Goodbye!");
    
    getch();
    return 0;
    }
    
    /* currencyconv.c -- function support module */
    #include <stdio.h>
    #include "currencyconv.h"
    int menu(void)
    {
    int user_input; 
    int selection;
    
    printf("\n%s%s\n", DECO, DECO);
    
    printf("                         CURRENCY CONVERTER\n\n\n");
    printf("           You've just entered the Currency Conversion Program\n\n\n");
    printf("        Select a number between 1 and 5 to pick the currency you\n");
    printf("             would like to convert to from US Dollars\n\n");
    printf("         Select number 6 to exit the program\n\n\n");
    printf("      Enter Number (1) Key to Select EUR or Euro\n\n");
    printf("      Enter Number (2) Key to Select GBP or British Pounds\n\n");
    printf("      Enter Number (3) Key to Select INR or Indian Rupee\n\n");
    printf("      Enter Number (4) Key to Select CAD or Canadian Dollars\n\n");
    printf("      Enter Number (5) Key to Select AUD or Australian Dollars\n\n");
    printf("      Enter Number (6) Key to Quit\n\n");
    
    printf("%s%s\n", DECO, DECO);
    
    while ((selection = scanf("%d", &user_input)) != 1 || (user_input < 1 || user_input > 6))
        {
        if (selection != 1)
            scanf("%*s");
            printf("Your number is not a selection. Please enter a number between 1 and 6.\n");
        }
            return user_input;
            getch();
        }
    
    
    int getus_dollars(void)
    {
        int us_dollars;
        printf("Please enter the amount in US Dollars you would like to convert: ");
            while (scanf("%f", &us_dollars) != 1)
            {
                scanf("%*s");
                printf("Error!!! Please enter that amount again in US Dollars, such as 8.00\n");
            }    
            return us_dollars;
            getch();
    }
    
    
    float convert_rate(float currency, float us_dollars)
    {
        float total = 0.00;
        total = us_dollars * currency; 
            printf("Your US Dollars in XXX is $%0.2f.\n", total);
            //return total;
            //getch();
    }
    
    /* currencyconv.h -- constants and declarations for currencyconv.c */
    /*All constants/rates obtained on 11/3/13 from XE.com*/
    
    #define EUR 0.741
    #define GBP 0.628
    #define INR 61.75
    #define CAD 1.04
    #define AUD 1.06
    #define QUIT 6
    
    #define DECO  "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    
    int menu(void);        // shows list of choices


    Hello All -

    This is $$ converter program I created that is split up over 3 files. It takes the users input in US Dollars and converts it to the currency selected. When executing the program I am encountering a small problem. It actually isn't converting anything and returns 0.00 dollars. Could someone please look at this and tell me where the problem lies and how I can fix it.

    I would like for it to convert the amount entered into the currency selected. I would also like to add in the print statement the type of currency the user has selected.

    Any help would be greatly appreciated.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    1. Fix your indentation, and you will see several places where your curly braces are misaligned or not closing the proper blocks you want (the scanf() while loop is a big one)
    2. Don't use getch() it's non-standard and getchar() will achieve the same thing.
    3. line 87 you call getch() after a return; statement. Processing does not proceed beyond return, anything you put after a return, exit, break, continue, is not going to be called

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    2
    Quote Originally Posted by nonpuz View Post
    1. Fix your indentation, and you will see several places where your curly braces are misaligned or not closing the proper blocks you want (the scanf() while loop is a big one)
    2. Don't use getch() it's non-standard and getchar() will achieve the same thing.
    3. line 87 you call getch() after a return; statement. Processing does not proceed beyond return, anything you put after a return, exit, break, continue, is not going to be called
    I have fixed all the issues that you listed but it is still not executing the code.

  4. #4
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by annleft View Post
    I have fixed all the issues that you listed but it is still not executing the code.
    The problem is probably your getus_dollars function.

    First, your scanf() is wrong; you're using %f but passing a pointer to an integer as the second argument. Perhaps int us_dollars; should be a float.

    I think that just changing that would improve things, but I'd also probably have getus_dollars() return a float instead of an int.

    Edit: Also in convert_rate() you have no return value! (It's commented out)

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. Currency Conversion problem
    By Rich Whitehead in forum C Programming
    Replies: 2
    Last Post: 09-17-2007, 12:40 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