Hello, I am having trouble with error checking on my program that has two inputs. The first is a menu selection, and the second is an amount greater than 0. For both inputs it must check against entering a letter or an invalid number. I tried a do/while statement for the menu selection, and that took care of the valid menu option part, but I can't seem to figure out how to handle a character input.
I tried to use isdigit to solve this problem, but I may not have implemented it correctly. If you guys could take a look at my code and maybe give some advice on what would be the best way to tackle this problem.
Code:
#include <stdio.h>
#include <ctype.h>
main()
{
char iSelection;
float fCurrSelect = 0.00;
float fUSCurr = 0.00;
do {
printf("\n\tCurrency Conversion Program 1.1\n\n");
printf("\nChoose the currency you wish to convert\n");
printf("\n1\tAustrailian dollar");
printf("\n2\tCanadian dollar");
printf("\n3\tEuro");
printf("\n4\tMexican peso");
printf("\n5\tSwiss franc\n\n");
scanf("%d", &iSelection);
} while (iSelection < 1 || iSelection > 5);
printf("\nHow much money would you like to convert to U.S. dollars?");
scanf("%f", &fCurrSelect);
switch(iSelection) {
case 1:
fUSCurr = (fCurrSelect/1.16686);
printf("\nThe Austrailian dollar amount %.2f converted to USD equals $%.2f", fCurrSelect, fUSCurr);
break;
case 2:
fUSCurr = (fCurrSelect/1.0072);
printf("\nThe Canadian dollar amount %.2f converted to USD equals $%.2f", fCurrSelect, fUSCurr);
break;
case 3:
fUSCurr = (fCurrSelect/0.695943);
printf("\nThe Euro amount %.2f converted to USD equals $%.2f", fCurrSelect, fUSCurr);
break;
case 4:
fUSCurr = (fCurrSelect/10.8509);
printf("\nThe Mexican peso amount %.2f converted to USD equals $%.2f", fCurrSelect, fUSCurr);
break;
case 5:
fUSCurr = (fCurrSelect/1.1522);
printf("\nThe Swiss franc amount %.2f converted to USD equals $%.2f", fCurrSelect, fUSCurr);
break;
default:
printf("\nInvalid option!");
}//end switch block
getchar();
}//end main