I'm making a program for addition, subtraction, division and multiplication with the help of switch operator. Here I want to make a program that will stop only when user entered any other character instead of 'Y'. The problem is: when I run the program, after the result of respective case it only print 'do you want to continue' and after that the program stops. Please help! Thank you in advance! :)
#include<stdio.h>
int main() {
int n,a,b;
char answer;
while(1) {
printf("enter \n1.Addition \n2.subtraction \n3.division \n4.multiplication\t");
scanf("%d",&n);
switch(n) {
case 1: {
printf("enter value of a and b:\t");
scanf("%d %d",&a,&b);
printf("addition of %d+%d is %d",a,b,a+b);
goto ans;
}
case 2: {
printf("enter the value of a and b:\t");
scanf("%d %d",&a,&b);
printf("Subtraction of %d-%d is %d",a,b,a-b);
goto ans;
}
case 3: {
printf("enter the value of a and b:\t");
scanf("%d %d",&a,&b);
printf("Division of %*% is %d",a,b,a/b);
goto ans;
}
case 4: {
printf("enter the value of a nad b:\t");
scanf("%d %d",&a,&b);
printf("Multiplication of %d*%d is %d",a,b,a*b);
goto ans;
}
default: printf("invalid value!");
goto ans;
}
ans: {
printf("Do you want to continue?(Y/N):\t");
scanf("%c",&answer);
if(answer=='Y') continue;
else break;
}
}
return 0;
}