I need to add quit or exit menu option to my program. and I also need to add which will be displayed when the program exits a running total of all the converted dollar amounts entered. The highest converted dollar amount out of all the currencies entered, along with the name of that currency. everthing I have tried crashes any hlep would be great. :confused:
Code:#include <stdio.h>
#include <stdlib.h>
float conversion(float, float);
float get_dollar(void);
void get_menu(void);
main(void)
{
/*define currency*/
float UK = .558005;
float Euro = 0.8264;
float Canadian = 1.32;
float YEN = 109.37;
float AUD = 1.30073;
char input = ' ';
float amount = 0.0;
float total_amt = 0.0;
//display the menu options
get_menu();
/*Get input for menu option. The method used is the getch() function which
gets the first character input only. This is good for my menu since I am only trying
to get the numbers 1 2, or 3. I cant enter more then one character, and if
I enter an invalid character the case statement below will catch this.
*/;
input = getch();
Printf("%c\n",input);
switch(input)
{
case '1':
/*Do conversion for british currency */
/*Get input for dollar amount */
amount = get_dollar();
total_amt = conversion(amount, UK);
/* Print out conversion amount */
printf("\n\n %4.2f UK Pound = %4.2f US dollars\n",amount, total_amt);
break;
case '2':
/*Do conversion for Danish currency */
amount = get_dollar();
total_amt = conversion(amount,Euro);
/* Print out conversion amount */
printf("\n\n %4.2f Euro Dollar = %4.2f US dollars\n",amount, total_amt);
break;
case '3':
/*Do conversion for Candian currency */
amount = get_dollar();
total_amt = conversion(amount,Canadian);
/* Print out conversion amount */
printf("\n\n %4.2f Canadian Dollar = %4.2f US dollars\n",amount, total_amt);
break;
case '4':
/*Do conversion for Candian currency */
/* Print out conversion amount */
printf("\n\n %4.2f Japan Yen = %4.2f US dollars\n",amount, total_amt);
break;
case '5':
/*Do conversion for Candian currency */
amount = get_dollar();
total_amt = conversion(amount,AUD);
/* Print out conversion amount */
printf("\n\n %4.2f Australian Dollar = %4.2f US dollars\n",amount, total_amt);
break;
default:
/* catch the invalid menu option and display to screen */
printf("\n\n You have entered an invalid menu option\n");
break;
}
printf("Hit any key to quit.....\n");
getche();
}
/* Present menu options*/
void get_menu()
{
printf("1. UK Pounds\n");
printf("2. Euro dollar\n");
printf("3. Canadian Dollar\n");
printf("4. Japan Yen\n");
printf("5. Australian Dollar\n");
printf("\n\nPlease enter the number associated with the currency:");
}
/* The function takes an input amount and the conversion rate and
then divides the input amount by the conversion rate to get the
new converted amount.
*/
float conversion(float input_amt, float curr_value)
{
float new_amount = 0.0;
new_amount = input_amt / curr_value;
return new_amount;
}
/* The function prompts for an input dollar amount. */
float get_dollar()
{
double inp_dol = 0.0;
char inp_string[20]="";
char *pEnd;
printf("\nPlease enter a non zero dollar amount:");
scanf("%s",inp_string);
//The following code allows for validation of input. If the user enters any non numeric number or
//a number of less than or equal to zero the code below will prompt the user again.
inp_dol = strtod(inp_string, &pEnd);
while(pEnd[0] != '\0' || inp_dol <= 0.0)
{
printf("\nPlease enter a non zero dollar amount:");
scanf("%s",inp_string);
inp_dol = strtod(inp_string, &pEnd);
}
return inp_dol;
}