Some Help in Currency Program

Discussion in 'Computer Science & Culture' started by PlrBr533, Aug 24, 2004.

Thread Status:
Not open for further replies.
  1. PlrBr533 Registered Member

    Messages:
    2
    Hello C Programmers.
    I have written some code for my introductory class to C programming. The issue I am having is that I do not know how to create code so that it awaits for user input to know howmuch money to convert.
    If anyone may help me it wouldbe appreciated.

    Below is my source code:

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

    //Declaration of constant values for exchange rates
    #define aud_rate 0.7139 //Australian Dollar
    #define gbp_rate 1.8495 //British Pound
    #define can_rate 0.7615 //Canadian DOllar
    #define jpy_rate 0.0091 //Japanes Yen
    #define nzd_rate 0.6528 //New Zealand Dollar

    int main()
    {
    //Initialization of Monetary Units variables

    float USD = 1.00;
    float AUD = USD * aud_rate;
    float GBP = USD * gbp_rate;
    float CAN = USD * can_rate;
    float JPY = USD * jpy_rate;
    float NZD = USD * nzd_rate;
    char input;
    int quit = 0;

    printf("**********CURRENCY CONVERTER**********\n\n\n\n"); //Title of Program
    fflush(stdin);
    printf("To view Currency Table press Enter\n\n"); //Pause, wait for user input
    getchar();
    printf("________________________________________________________________\n");
    printf("| MU | MU Amount | US Equivalent | US Conversion $1.00 |\n"); //Table Header
    printf("|--------------------------------------------------------------|\n");
    printf("| AUD | $1.00 | $0.7139USD | $1.4008AUD |\n"); //Australian Dollar
    printf("|--------------------------------------------------------------|\n");
    printf("| GBP | $1.00 | $1.8495USD | $0.5407GBP |\n"); //British Pound
    printf("|--------------------------------------------------------------|\n");
    printf("| CAN | $1.00 | $0.7615USD | $1.3132CAN |\n"); //Canadian Dollar
    printf("|--------------------------------------------------------------|\n");
    printf("| JPY | $1.00 | $0.0091USD | $110.55JPY |\n"); //Japanese Yen
    printf("|--------------------------------------------------------------|\n");
    printf("| NZD | $1.00 | $0.6528USD | $1.5319NZD |\n"); //New Zealand Dollar
    printf("|______________________________________________________________|\n\n");

    while(!quit)
    {

    printf("\nPlease enter a currency to convert to US Dollar or q to Quit \n");
    printf("1 - Australian Dollar\n");
    printf("2 - British Pound \n");
    printf("3 - Canadian Dollar \n");
    printf("4 - Japanese Yen\n");
    printf("5 - New Zealand Dollar\n");
    printf("q - to Quit \n");
    printf("> ");
    input = getchar();
    getchar();
    switch(input)
    {
    case '1' :
    printf("One US dollar equals %.2f Australian Dollars\n",AUD);
    break;
    case '2' :
    printf("One US Dollar equals %.2f British Pounds\n",GBP);
    break;
    case '3' :
    printf("One US Dollar equals %.2f Canadian Dollars\n",CAN);
    break;
    case '4' :
    printf("One US Dollar equals %.2f Japanese Yen\n",JPY);
    break;
    case '5' :
    printf("One US Dollar equals %.2f New Zealand Dollars\n",NZD);
    break;
    case 'q' :
    printf("Thank you for using this program.\n\n");
    quit = 'q';
    break;
    default:
    printf("I don't know that one. Please try again.\n");
    } // End of Switch
    //The loop asked for a user input. That choice was matched against a list of choices
    //and the proper currency converted. The loop will display the menu and ask for a
    //input until the user selects 'q'.

    } //End of While loop

    printf("Don't spend it all in one place!\n");


    //end statement
    fflush(stdin);
    printf("\nPress Enter to end program"); //Await return carraige to end program
    getchar();
    return 0;
    }
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. curioucity Unbelievable and odd Registered Senior Member

    Messages:
    2,429
    You may want to make thw 'switch' part into only accepting inputs from 1 to 5, maybe something like this:

    if(input=='q') //end program
    else if(input=='1'||input=='2'|| so on ||input=='5'){
    //make a statement asking for how much money is to be converted, like below
    printf("enter amount of USD you want to convert: ");
    scanf("%.2f", &USD);

    switch(input){
    case 1: printf("amount of money: %.2f\n", AUD);
    //and so on
    case 5: printf("...%.2f\n", NZD);
    } //end switch
    else // wrong input error message



    Sorry, hopefully that's clear though. I'm always terible at explaining things.....
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. PlrBr533 Registered Member

    Messages:
    2
    curiocity,

    thanks i am going to try this out and see what happens.
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
Thread Status:
Not open for further replies.

Share This Page