I am working on the all-knowing currency conversion program. It works, and I'm fairly happy with it - except for error checking. It doesn't do so well. I'm trying to use the isdigit function to ensure that the user types in an integer for the menu selection rather than a letter, but I can't quite seem to get it straight.
This is what I have...
Code://Currency Conversion Program //Written by ***********, University of Phoenix //Version 2 3/26/2006 #include <stdio.h> #include <system.h> #include <stdlib.h> #include <ctype.h> //global variable declarations and initialization with foreign exchange rates from <http://www.xe.com/ucc.convert.cgi> as of 3/19/2006 //these global variables are to remain constant throughout use in the program float f_const_USD = 1.00; //1.00 USD = 1.00 United States Dollar float f_const_GBP = 0.569280; //1.00 USD = 0.569280 United Kingdom Pounds float f_const_CAD = 1.15940; //1.00 USD = 1.15940 Canada Dollars float f_const_MXN = 10.7010; //1.00 USD = 10.7010 Mexico Pesos float f_const_JPY = 115.860; //1.00 USD = 115.860 Japan Yen float f_const_HKD = 7.75760; //1.00 USD = 7.75760 Hong Kong Dollars main() //Main Program for Currency Conversion { //The following f_input variable is for storing the USD input from the user, declared initially as '0' float f_input_USD = 0.00; //The following f_result variables are for storing calculations within the program declared initially as '0' float f_result_USD = 0.00; float f_result_GBP = 0.00; float f_result_CAD = 0.00; float f_result_MXN = 0.00; float f_result_JPY = 0.00; float f_result_HKD = 0.00; //variable declaration for menu input, default is '0' int i_menu_choice = 0; while (i_menu_choice <6) { i_menu_choice = 0; //create menu to choose currency conversion type printf("\n\tCurrency Conversion\n"); //Title of Program printf("\n1\tConvert US Dollars to United Kingdom Pounds"); printf("\n2\tConvert US Dollars to Canadian Dollars"); printf("\n3\tConvert US Dollars to Mexican Pesos"); printf("\n4\tConvert US Dollars to Japanese Yen"); printf("\n5\tConvert US Dollars to Hong Kong Dollars"); printf("\n6\tExit Currency Conversion Program\n\n"); //Accept input for menu selection printf("Please make your selection now: "); scanf("%d", &i_menu_choice); //switch structure for menu selection switch (i_menu_choice) { case 1: printf("\nYou Selected Conversion from US Dollars to United Kingdom Pounds."); printf("\nPlease Enter the Amount in x.xx US Dollars to Convert.\n$"); scanf("%f", &f_input_USD); //get USD input from User for GBP conversion break; case 2: printf("\nYou Selected Conversion from US Dollars to Canadian Dollars."); printf("\nPlease Enter the Amount in x.xx US Dollars to Convert.\n$"); scanf("%f", &f_input_USD); //get USD input from User for CAD conversion break; case 3: printf("\nYou Selected Conversion from US Dollars to Mexican Pesos."); printf("\nPlease Enter the Amount in x.xx US Dollars to Convert.\n$"); scanf("%f", &f_input_USD); //get USD input from User for MXN conversion break; case 4: printf("\nYou Selected Conversion from US Dollars to Japanese Yen."); printf("\nPlease Enter the Amount in x.xx US Dollars to Convert.\n$"); scanf("%f", &f_input_USD); //get USD input from User for JPY conversion break; case 5: printf("\nYou Selected Conversion from US Dollars to Hong Kong Dollars."); printf("\nPlease Enter the Amount in x.xx US Dollars to Convert.\n$"); scanf("%f", &f_input_USD); //get USD input from User for HKD conversion break; case 6: return 0; //end program default: printf("\nYou Have Made and Invalid Selection. Please Try Again.\n\n\n"); scanf("%d", &i_menu_choice); } //end switch //begin if structure for calculations if (isdigit (i_menu_choice)) { if (i_menu_choice == 1) { f_result_GBP = f_input_USD * f_const_GBP; printf("\n$%.2f US Dollars is equal to ", f_input_USD); printf("%.2f United Kingdom Pounds.\n\n", f_result_GBP); } //end if else if (i_menu_choice == 2) { f_result_CAD = f_input_USD * f_const_CAD; printf("\n$%.2f US Dollars is equal to ", f_input_USD); printf("%.2f Canadian Dollars.\n\n", f_result_CAD); } //end else if else if (i_menu_choice == 3) { f_result_MXN = f_input_USD * f_const_MXN; printf("\n$%.2f US Dollars is equal to ", f_input_USD); printf("%.2f Mexican Pesos.\n\n", f_result_MXN); } //end if else if (i_menu_choice == 4) { f_result_JPY = f_input_USD * f_const_JPY; printf("\n$%.2f US Dollars is equal to ", f_input_USD); printf("%.2f Japanese Yen.\n\n", f_result_JPY); } //end if else if (i_menu_choice == 5) { f_result_HKD = f_input_USD * f_const_HKD; printf("\n$%.2f US Dollars is equal to ", f_input_USD); printf("%.2f Hong Kong Dollars.\n\n", f_result_HKD); } //end if structure for calculations } //end isdigit if structure else { printf("\nThat is not a valid choice. Please try again."); getchar(); } //printf("\n%d Was Your Menu Selection.\n\n", i_menu_choice); //This line was here simply for error checking } //end of while } //end main program



LinkBack URL
About LinkBacks



