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