I have attempted to create a program that is crash proof but I seem to be missing the right codes to keep this program from crashing. I need for the program to not accept alpha, characters, or negative integers. Can someone help, please! See my futile attempt. If I need a switch, where would I place it within the program to prevent any errors.
#include <stdio.h>
int main(void)
{
/*
The conversion rates of five currencies equivalent to one US dollar.
Live mid-market rates as of 2006.06.18 16:29 Universal Time.
*/
// Declare and define the conversion rates of five currencies equivalent to one US dollar.
/* Euro= Euro Dollars */
/* WON = South Korean WON */
/* Franc = Switzerland Franc */
/* Krone = Denmark Krone */
/* Krona = Sweden Krona */
char cur[6][20]={"Euro ", "WON ", "Franc", "Krone", "Krona", "Exit"}; //name of the five currencies
float rates[5] ={1.23, 1232.55, 1.23, 6.01, 7.45}; //equivalent exchange rates
float usd; /* US Dollars */
float amount; /* Amount to Convert*/
int r, i;
printf("Written by: Luther Brewer\n\n");
printf("\nThis Currency Conversion program converts each of the five currencies\n");
printf("listed below into a US Dollar equivalent amount \n");
printf("\nThe list of currencies available for conversion are (number, name): \n");
for (i=0;i<6;i++) //a for loop to display the currencies to convert from
{
printf("\n%d, %s", i+1, cur[i]);
}
choice: printf("\n\nPlease select the number of the currency you want to convert to USD:\n\n");
scanf("%d", &i);
if ((i >6)||(i <=0))
{
printf("Choice is not available! \n\n");
goto choice;
}else if (i == 6){
printf("\n\nHave a nice day! \n\n");
}else{
ask: printf("\nHow much money do you want to convert to USD?: \n\n ");
scanf("%f", &amount); //Wait for user's input
if (amount <=(float)0)
{
printf("\nCannot convert to US Dollars, if the number is zero or negative! \n\n");
goto ask;
}else{
usd = amount /rates[i-1]; /*Convert to US Dollars*/
// Calculation: finding the equivalent amount of the US dollar from the 5 input currencies.
printf("\nYou will have %f US Dollars! \n\n", usd);
}
}
fflush (stdin);
getchar();
return 0;/*End of program*/
}
Start Free Trial