I want to use a get choice function to let user choose a letter from a menue, and i want to use this value to switch it in a float function…i know its only possible to use switch as type int, but will the value i will get is going to be float?
float calc(float number1, float number2)
{
int answer = get_choice();switch (answer) { case 'a': answer = number1 + number2; break; case 's': answer = number1 - number2; break; case 'm': answer = number1 * number2; break; case 'd': answer = number1 / number2; break; } return answer; }char get_choice(void)
{
int choice;printf("Enter the operation of your choice:\n"); printf("a. add s. subtract\n"); printf("m. multiply d. divide\n"); printf("q. quit\n"); while ((choice = getchar()) == 1 && choice != 'q') { if (choice != 'a' || choice != 's' || choice != 'm' || choice != 'd') { printf("Enter the operation of your choice:\n"); printf("a. add s. subtract\n"); printf("m. multiply d. divide\n"); printf("q. quit\n"); continue; } } return choice; }
There is no need to reuse the variable. You can also switch a
charvariable directly.