I've gotten this far, but I don't know how to use the 'function' properly... I've tried several things, but keep coming back to this base code because I know it works.
Can anyone give me a better idea of how to take this part of the coding in the switch:
And make it a function? Problem is that I can't figure out how to pass the menu selection as an argument and get the right result. I keep getting mixed responses.Code:printf("\nPlease Enter the Amount in x.xx Hong Kong Dollars to Convert.\n$"); scanf("%f", &f_input_AMT); //get AMT input from User for HKD conversion f_result[4] = f_input_AMT * f_const_HKD; printf("\n$%.2f Hong Kong Dollars is equal to $", f_input_AMT); printf("%.2f US Dollars.\n\n", f_result[4]);
I understand that I'm going to have to do something like this:
I just don't quite get how to figure out how to get the argument from my menu selection and tell the program what constant to use in the calculation.Code:getCurrencyValue(void) printf("\nPlease Enter the Amount in x.xx (first argument) to convert. \n$)", (first argument); scanf("%f", $f_input_AMT); //get amt for conversion f_result[4] = f_input_AMT * f_const_(need argument here to define what constant to use); printf("\n$.2f (first argument) is equal to $", f_input_AMT); printf("%.2f US Dollars.\n\n", f_result[4]);
Any help, tips, pointers are appreciated.
Full Program
Code://Currency Conversion Program //Written by ***********, University of Phoenix //Version 3 4/2/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 4/2/2006 //this global variable defnition is to remain constant throughout use in the program #define f_const_USD 1.00; //1.00 USD = 1.00 USD *United States Dollar* #define f_const_GBP 1.73710; //1.00 GBP = 1.73710 USD *United Kingdom Pounds* #define f_const_CAD 0.855872; //1.00 CAD = 0.855872 USD *Canadian Dollars* #define f_const_MXN 0.0919608; //1.00 MXN = 0.0919608 USD *Mexican Pesos* #define f_const_JPY 0.00849675; //1.00 JPY = 0.00849675 USD *Japanese Yen* #define f_const_HKD 0.128871; //1.00 HKD = 0.128871 USD *Hong Kong Dollars* main(void) //Main Program for Currency Conversion { //The following f_input variable is for storing the AMT input from the user, declared initially as '0' float f_input_AMT = 0.00; //The following f_result array is for storing calculations within the program declared initially as '0' float f_result[4] = {0.00}; // f_result[0] = United Kingdom Pounds // f_result[1] = Canadian Dollars // f_result[2] = Mexican Pesos // f_result[3] = Japanese Yen // f_result[4] = Hong Kong Dollars //variable declaration for menu input, default is '0' int i_menu_choice = '0'; do //Do while structure so program will continue until user selects 6 to exit { do //Error Checking Loop Structure for Menu Selection { //create menu to choose currency conversion type printf("\n\tCurrency Conversion\n"); //Title of Program printf("\n1\tConvert United Kingdom Pounds to US Dollars"); printf("\n2\tConvert Canadian Dollars to US Dollars"); printf("\n3\tConvert Mexican Pesos to US Dollars"); printf("\n4\tConvert Japanese Yen to US Dollars"); printf("\n5\tConvert Hong Kong Dollars to US Dollars"); printf("\n6\tExit Currency Conversion Program\n\n"); //Accept input for menu selection printf("Please make your selection now: "); i_menu_choice = getchar(); //switch structure for menu selection switch (i_menu_choice) { case '1': printf("\nYou Selected Conversion from United Kingdom Pounds to US Dollars."); do //begin do structure to error check f_input_AMT for digits { printf("\nPlease Enter the Amount in x.xx United Kingdom Pounds to Convert.\n$"); scanf("%f", &f_input_AMT); //get AMT input from User for GBP conversion f_result[0] = f_input_AMT * f_const_GBP; printf("\n$%.2f United Kingdom Pounds is equal to $", f_input_AMT); printf("%.2f US Dollars.\n\n", f_result[0]); } while (isdigit (f_input_AMT)); //end do structure to error check f_input_AMT for digits break; case '2': printf("\nYou Selected Conversion from Canadian Dollars to US Dollars."); do //begin do structure to error check f_input_AMT for digits { printf("\nPlease Enter the Amount in x.xx Canadian Dollars to Convert.\n$"); scanf("%f", &f_input_AMT); //get input from User for CAD conversion f_result[1] = f_input_AMT * f_const_CAD; printf("\n$%.2f Canadian Dollars is equal to $", f_input_AMT); printf("%.2f US Dollars.\n\n", f_result[1]); } while (isdigit (f_input_AMT)); //end do structure to error check f_input_AMT for digits break; case '3': printf("\nYou Selected Conversion from Mexican Pesos to US Dollars."); do //begin do structure to error check f_input_AMT for digits { printf("\nPlease Enter the Amount in x.xx Mexican Pesos to Convert.\n$"); scanf("%f", &f_input_AMT); //get input from User for MXN conversion f_result[2] = f_input_AMT * f_const_MXN; printf("\n$%.2f Mexican Pesos is equal to $", f_input_AMT); printf("%.2f US Dollars.\n\n", f_result[2]); } while (isdigit (f_input_AMT)); //end do structure to error check f_input_AMT for digits break; case '4': printf("\nYou Selected Conversion from Japanese Yen to US Dollars."); do //begin do structure to error check f_input_AMT for digits { printf("\nPlease Enter the Amount in x.xx Japanese Yen to Convert.\n$"); scanf("%f", &f_input_AMT); //get AMT input from User for JPY conversion f_result[3] = f_input_AMT * f_const_JPY; printf("\n$%.2f Japanese Yen is equal to $", f_input_AMT); printf("%.2f US Dollars.\n\n", f_result[3]); } while (isdigit (f_input_AMT)); //end do structure to error check f_input_AMT for digits break; case '5': printf("\nYou Selected Conversion from Hong Kong Dollars to US Dollars."); do //begin do structure to error check f_input_AMT for digits { printf("\nPlease Enter the Amount in x.xx Hong Kong Dollars to Convert.\n$"); scanf("%f", &f_input_AMT); //get AMT input from User for HKD conversion f_result[4] = f_input_AMT * f_const_HKD; printf("\n$%.2f Hong Kong Dollars is equal to $", f_input_AMT); printf("%.2f US Dollars.\n\n", f_result[4]); } while (isdigit (f_input_AMT)); //end do structure to error check f_input_AMT for digits break; case '6': return 0; //end program default: printf("\nYou Have Made and Invalid Selection. Please Try Again.\n\n\n"); i_menu_choice = getchar(); } //end switch } //end error check do while for menu selection criteria while (!isdigit(i_menu_choice) || (i_menu_choice , '1') && (i_menu_choice > '6')); } //end program run do while statement while (i_menu_choice != '6'); } //end main program



LinkBack URL
About LinkBacks



