Thread: Help on Code

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    1

    Unhappy Help on Code

    Hi Everyone:

    I’m new in programming. Actually I’m learning C. As a part of homework, we developed the following code in C:
    Code:
    #include <stdio.h>
    
    int main (void)
    
    /*Program*/
    
    {
    float YEN = 0.0139;/*Amount of Euro Dollars to one US Dollar*/
    float EUR = 1.22266; /* Amount of Suisse Franc to one US Dollar*/
    float UKP = 1.8410; /*Amount of British Pound to one US DOllar*/
    float CAD = 0.7167; /*Amount of Canadian Follar to one US DOllar*/
    float AUD = 0.7167;/*Amount of Australian Dollar to one US DOllar*/
    float money;
    float total;
    int choice;
    
    while (choice != '5')
    /*Menu Printing the title and author of program*/
    {printf("Takeda Shinden \n06/10/04\n");
    printf(" Currency Conversion Program\n");
    printf("University of the Virgin Islands\n");
    printf("CS106\n");
    printf("Programming Concepts I\n");
    printf("\n"); /*print a blank line to separate the heading*/
    printf("\n"); /*print a blank line to separate the heading*/
    
    
    /*The Following lines shows the user choices of currencies and asks to select on of them*/
    printf("Enter 1 for Japan Yen\n");
    printf("Enter 2 for Euro Dollars\n");
    printf("Enter 3 for United Kindom Pounds\n");
    printf("Enter 4 for Canadian Dollars\n");
    printf("Enter 5 for Australian Dollars\n");
    printf("Enter 0 to exit the program\n\n");
    
    printf("Please select the currency you want to convert USD to:\n\n");
    scanf("%d",&choice);
    if (choice>5)
    {
    printf("\nPlease Select a number between 0 and 5! \n\n",);
    }
    if (choice==0)
    {
    printf("!Thanks for Your Support, Have a nice week!\n");
    fflush( stdin );   /* fflush clears stdin, gets past the error line */
    getch(); /*stops the screen from closing */
    break;
    
    }
    printf("\n\nHow much money do you want to convert? (US Dollars): ");
    scanf("%f",&money);
    if (choice==1)
    {
    total = money *YEN;/*one US Dollar equals 0.9139 Japan Yens*/
    printf("\nYou will have %f Japan Yens! \n\n", total);
    }
    if (choice==2)
    {
    total = money * EUR;/* one US Dollar equals 1.22266 Euro Dollars*/
    printf("\nYou will have %f Euro Dollars! \n\n", total);
    }
    if (choice==3)
    {total = money *UKP; /*one US Dollar equals 1.8410 United Kindom Pounds */
    printf("\nYou will have %f United Kindom Pounds! \n\n", total);
    }
    if (choice==4)
    {
    total = money *CAD; /*one US Dollar equals 0.7347 Canadian Dollars*/
    printf("\nYou will have %f Canadian Dollars! \n\n", total);
    }
    if (choice==5)
    {
    total = money * AUD;/* one US Dollar equals 0.7167 Australian Dollars */
    printf("\nYou will have %f Canadian Dollars! \n\n", total);
    }
    }
    return 0; /*End of program*/
    }
    Now I’m entitled to correct the following on it:

    Changes:

    The Professor told me that It is going into an infinite loop when character is accepted. Use fflush function immediately after accepting the input from the user. So I did it, but I don’t know if I did it correctly.

    Then I need to make the following changes:

    When the user enters an invalid input, let him know that and do not exit from the program. Let the user enter the valid input again and let him run the program as many times as he wants until he decides to exit from the program.

    I think that it is done, but not sure, but I have no Ideas how to make them, could somebody help me?


    Thanks

    Takeda
    Last edited by Salem; 06-12-2004 at 03:08 PM. Reason: tagged - though the poster should edit this and post some indented code

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    21
    you need to have
    Code:
    #include <conio.h>
    and your statment
    Code:
     printf("\nPlease Select a number between 0 and 5! \n\n",);
    doesnt need the \n at the start of the string

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you familiar with the switch statement? I'd suggest using that.
    Also, you'll want to inform your teacher that they're a moron, and that fflush(stdin) is undefined behavior, meaning, it doesn't work on all compilers, and shouldn't be used. If he has any doubt, have them read the C standard (any version, it doesn't matter, it's always been undefined).

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM