Newbie Help: Currency Converter
Here's what I'm supposed to do:
Quote:
" The user should be asked to enter their country selection from the menu. If they pick either Britain, Denmark or Japan, they should be asked whether they wish to convert from Euro to either Sterling/Krone/Yen (select just one of these depending on their choice) or vice versa. Your program should then display this equivalent currency amount.
If the user selects USA from the menu, they should be asked one extra question. Not only should they be asked whether they wish to covert from Euro to Dollar or vice-versa, they must also enter the current exchange rate between these currencies. Your program must then use this rate and calculate the converted currency value.
Your program should continually run and re-display the main menu allowing the user to start over again. Only when the user enters option 5 (i.e. Exit program option), your program should terminate. The program must take account of input errors by the user and display appropriate error messages."
But whenever I enter a character that is not a number, the program doesn't display an Error. How do I resolve this?
Please take into consideration that I have only learnt the following at this point: if else, switch statements. while, do-while, for loops. I'm currently learning Arrays.
Is there anyway to simplify the code? (as it looks too complicated at this stage)
Code:
#include <stdio.h>
#include <conio.h>
main()
{
/* Define Variables */
int option, option2;
float eur_sterling, sterling_eur, sterling, sterling_euro;
float eur_krone, krone_eur, krone, krone_euro;
float eur_yen, yen_eur, yen, yen_euro;
float euro_dollar1, euro_dollar2, euro_dollar;
float dollar_eur1, dollar_eur2, dollar_euro;
/* Start loop */
do
{
/* Display menu asking user to select a country or quit */
printf("Euro Currency Converter\n");
printf("1. Britain\n");
printf("2. Denmark\n");
printf("3. Japan\n");
printf("4. USA\n");
printf("5. Exit Program\n");
printf("Please select an option: ");
scanf("%d", &option);
/* Initiate if statement for 1. Britain */
if (option == 1)
{
/* Ask user if converting Euro to Sterling or vice versa */
printf("If converting from Euro to Sterling, press 1\n");
printf("If converting from Sterling to Euro, press 2\n");
scanf("%d", &option2);
if (option2 == 1)
{
/* Ask user to enter amount for conversion */
printf("Please enter the amount you wish to convert (Euro to Sterling): ");
scanf("%f", &eur_sterling);
/* Calculate Exchange rate from Euro to Sterling */
sterling = eur_sterling * 0.70;
printf("%.2f Euro = %.2f Sterling\n\n", eur_sterling, sterling);
}
else if (option2 == 2)
{
/* Ask user to enter amount for conversion */
printf("Please enter the amount you wish to convert (Sterling to Euro): ");
scanf("%f", &sterling_eur);
/* Calculate Exchange rate from Sterling to Euro */
sterling_euro = sterling_eur * 1.42;
printf("%.2f Sterling = %.2f Euro\n\n", sterling_eur, sterling_euro);
}
else
{
printf("Error: Invalid Code\n\n");
}
}
/* Initiate else if statement for 2. Denmark */
else if (option == 2)
{
/* Ask user if converting Euro to Krone or vice versa */
printf("If converting from Euro to Krone, press 1\n");
printf("If converting from Krone to Euro, press 2\n");
scanf("%d", &option2);
if (option2 == 1)
{
/* Ask user to enter amount for conversion */
printf("Please enter the amount you wish to convert (Euro to Krone): ");
scanf("%f", &eur_krone);
/* Calculate Exchange rate from Euro to Krone */
krone = eur_krone * 7.45;
printf("%.2f Euro = %.2f Krone\n\n", eur_krone, krone);
}
else if (option2 == 2)
{
/* Ask user to enter amount for conversion */
printf("Please enter the amount you wish to convert (Krone to Euro): ");
scanf("%f", &krone_eur);
/* Calculate Exchange rate from Krone to Euro */
krone_euro = krone_eur * 0.13;
printf("%.2f Krone = %.2f Euro\n\n", krone_eur, krone_euro);
}
else
{
printf("Error: Invalid Code\n\n");
}
}
/* Initiate else if statement for 3. Japan */
else if (option == 3)
{
/* Ask user if converting Euro to Yen or vice versa */
printf("If converting from Euro to Yen, press 1\n");
printf("If converting from Yen to Euro, press 2\n");
scanf("%d", &option2);
if (option2 == 1)
{
/* Ask user to enter amount for conversion */
printf("Please enter the amount you wish to convert (Euro to Yen): ");
scanf("%f", &eur_yen);
/* Calculate Exchange rate from Euro to Yen */
yen = eur_yen * 138;
printf("%.2f Euro = %.2f Yen\n\n", eur_yen, yen);
}
else if (option2 == 2)
{
/* Ask user to enter amount for conversion */
printf("Please enter the amount you wish to convert (Yen to Euro): ");
scanf("%f", ¥_eur);
/* Calculate Exchange rate from Yen to Euro */
yen_euro = yen_eur * 0.01;
printf("%.2f Yen = %.2f Euro\n\n", yen_eur, yen_euro);
}
else
{
printf("Error: Invalid Code\n\n");
}
}
/* Initiate else if statement for 4. USA */
else if (option == 4)
{
/* Ask user if converting Euro to Dollar or vice versa */
printf("If converting from Euro to Dollar, press 1\n");
printf("If converting from Dollar to Euro, press 2\n");
scanf("%d", &option2);
if (option2 == 1)
{
/*Ask user to enter exchange rate from Euro to Dollar */
printf("Please enter the exchange rate (1 Euro = ? Dollar): ");
scanf("%f", &euro_dollar1);
/* Ask user to enter amount for conversion */
printf("Please enter the amount you wish to convert (Euro to Dollar): ");
scanf("%f", &euro_dollar2);
/* Calculate Exchange rate from Euro to Dollar */
euro_dollar = euro_dollar1 * euro_dollar2;
printf("%.2f Euro = %.2f Dollar\n\n", euro_dollar2, euro_dollar);
}
else if (option2 == 2)
{
/*Ask user to enter exchange rate from Dollar to Euro */
printf("Please enter the exchange rate (1 Dollar = ? Euro): ");
scanf("%f", &dollar_eur1);
/* Ask user to enter amount for conversion */
printf("Please enter the amount you wish to convert (Dollar to Euro): ");
scanf("%f", &dollar_eur2);
/* Calculate Exchange rate from Dollar to Euro */
dollar_euro = dollar_eur1 * dollar_eur2;
printf("%.2f Dollar = %.2f Euro\n\n", dollar_eur2, dollar_euro);
}
else
{
printf("Error: Invalid Code\n\n");
}
}
else if (option == 5)
{
printf("Exit Program\n");
}
/* If neither options 1-5 were selected print Error */
else
{
printf("Error: Invalid Code\n\n");
}
}
while (option != 5);
/* End loop */
getch();
return 0;
} //End main