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


I forgot to switch back the iSelection to int after messing with it. I was trying the isdigit to verify that a digit was entered. The problem that I am having is that we are suppose to have the input checked to make sure that someone doesn't enter a letter, and for the monetary value it should be greater than 0 and not be a letter. When you enter a letter it just loops infinitely, obviously due to the fact I have no error check for this. 