I am in 9th class so still a beginner in C. Can anyone tell how to do this? When anybody enters value more than 4 then it should print 'default:' label of switch case statement. I tried using do while for that but it gave errors. The code is
#include <stdio.h>
#include <unistd.h>
void main()
{
int n1,n2,a=0,c,r,o;
S:
printf("\n \n 1. Addition \n 2. Substract \n 3. Multiply \n 4. Divide \n \n");
printf("\n Enter your choice: \t");
scanf("%d",&o);
printf("Enter two numbers: \t");
scanf("%d %d",&n1,&n2);
switch (o)
{
case 1:
a=n1+n2;
printf("\n Please wait..");
sleep(1);
printf("\n Answer is %d",a);
printf("\n Perform another action too? 1 for Yes and 0 for No \t",c);
scanf("%d",&c);
if (c==1)
{
goto S;
}
if (c==0)
{
printf("\n \n \n Bye!");
}
else
{
printf("Choice ain't correct!");
}
L:
printf("\n \n Restart loop? 1 for Yes and any other number for No \t",r);
scanf("%d",&r);
if (r==1)
{
printf("\n \n Restarting Loop..");
sleep(1);
goto S;
}
else
{
printf("\n \t \t \t Bye!");
goto L;
}
break;
case 2:
a=n1-n2;
printf("\n Please wait..");
sleep(1);
printf("\n Answer is %d",a);
printf("\n Perform another action too? 1 for Yes and 0 for No \t",c);
scanf("%d",&c);
if (c==1)
{
goto S;
}
if (c==0)
{
printf("\n \n \n Bye!");
}
else
{
printf("Choice ain't correct!");
}
M:
printf("\n \n Restart loop? 1 for Yes and any other number for No \t",r);
scanf("%d",&r);
if (r==1)
{
printf("\n \n Restarting Loop..");
sleep(1);
goto S;
}
else
{
printf("\n \t \t \t Bye!");
goto M;
}
break;
case 3:
a=n1*n2;
printf("\n Please wait..");
sleep(1);
printf("\n Answer is %d",a);
printf("\n Perform another action too? 1 for Yes and 0 for No \t",c);
scanf("%d",&c);
if (c==1)
{
goto S;
}
if (c==0)
{
printf("\n \n \n Bye!");
}
else
{
printf("Choice ain't correct!");
}
N:
printf("\n \n Restart loop? 1 for Yes and any other number for No \t",r);
scanf("%d",&r);
if (r==1)
{
printf("\n \n Restarting Loop..");
sleep(1);
goto S;
}
else
{
printf("\n \t \t \t Bye!");
goto N;
}
break;
case 4:
a=n1/n2;
printf("\n Please wait..");
sleep(1);
printf("\n Answer is %d",a);
printf("\n Perform another action too? 1 for Yes and 0 for No \t",c);
scanf("%d",&c);
if (c==1)
{
goto S;
}
if (c==0)
{
printf("\n \n \n Bye!");
}
else
{
printf("Choice ain't correct!");
}
O:
printf("\n \n Restart loop? 1 for Yes and any other number for No \t",r);
scanf("%d",&r);
if (r==1)
{
printf("\n \n Restarting Loop..");
sleep(1);
goto S;
}
else
{
printf("\n \t \t \t Bye!");
goto O;
break;
default:
printf("Choice ain't correct");
break;
}
}
}
Switch case statement evaluates a given expression and based on the evaluated value(matching a certain condition), it executes the statements associated with it. Basically, it is used to perform different actions based on different conditions(cases).
You can use char 's for the switch expression and cases as well. In the code below, option matches case 'b' , hence its case block is executed.
Syntax of switch...case If there is a match, the corresponding statements after the matching label are executed. For example, if the value of the expression is equal to constant2 , statements after case constant2: are executed until break is encountered. If there is no match, the default statements are executed.
Your code is really bad, reasons stated in the comments at your post; But I think you should learn by improving your own code, that's what helped in my early days:
In default-case, add a goto O; or even goto S;
default: printf("Choice ain't correct. Try again..\n"); goto O; break;
You may want to terminate program after you said bye - would be more reasonable to me
I really really really recommend to refactor the code to run without go-to's. Its a really nice assignment to solve with a single loop around it.
What errors in particular do you mean?
~ edit ~
I think I'll illustrate what I mean with some code, that's how simple your program is, hope that helps a little and you don't just copy the code but try to understand why that's "better" (at least shorter and a bit easier to maintain and read) than your's ;)
#include <stdio.h>
#include <unistd.h>
int main()
{
int n1,n2,a=0,c,o;
int terminate = 0;
while(!terminate)
{
printf("\n \n 1. Addition \n 2. Substract \n 3. Multiply \n 4. Divide \n \n");
printf("\n Enter your choice: \t");
scanf("%d",&o);
if(o < 0 || o> 4)
{
printf("Choice ain't correct!\n");
continue; // restarts loop
}
printf("Enter two numbers: \n ");
scanf("%d %d",&n1,&n2);
switch(o)
{
case 1: a = n1 + n2;
break;
case 2: a = n1-n2;
break;
case 3: a = n1*n2;
break;
case 4: a = n1/n2;
break;
default:
// never reached, since validation of o was done before switch
break;
}
sleep(1);
printf("\n Answer is %d",a);
printf("\n Perform another action too? 1 for Yes and 0 for No \t",c);
scanf("%d",&c);
if (c!=1)
{
terminate = 1; // this causes the loop to terminate
}
}
printf("\n \n \n Bye!");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With