I'm trying to make a program that asks the user what he/she wants to do, but when invalid inputs are entered, the program does not tell the user right away. Also, the sentinel value does not work. Thanks.
#include <stdio.h>
int main ()
{
int selection;
float sum, difference, product, quotient;
float num1, num2;
start:
printf("****************************\n");
printf("What would you like to do?\n");
printf("1 = add \n2 = subtract \n3 = multiply \n4 = divide \n5 = exit\n");
printf("***************\n");
printf( "Enter your selection:");
scanf("%d", &selection);
printf("Please enter the first number:");
scanf("%f", &num1);
printf("Please enter the second number:");
scanf("%f", &num2);
printf("*************************\n");
if ( selection==1 || selection==2 || selection==3 || selection==4 ) {
switch (selection)
{
case 1:
sum = num1 + num2;
printf("Operation Result: %.2f\n", sum);
break;
case 2:
difference = num1 - num2;
printf("Operation Result: %.2f\n", difference);
break;
case 3:
product = num1 * num2;
printf("Operation Result: %.2f\n", product);
break;
case 4:
quotient = num1 / num2;
if( num2==0){
printf("The denominator cannot be zero.\nRe-enter denominator: ");
scanf("%f", num2);
printf("Operation Result: %.f\n", quotient);
break;}
else printf("Operation Result: %.2f\n", quotient);
break;
}
}
if (selection<0 || selection >5)
printf("Invalid Operation Selected\n");
goto start;
if (selection==5){
printf( "Thanks for using this program. Bye Bye!!\n");
}
return 0;
}