Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch case help in C

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;
        }
   }
}
like image 532
Jason Park Avatar asked Sep 20 '13 11:09

Jason Park


People also ask

What is the use of switch case in C?

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).

Can I use switch case for character in C?

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.

What is the syntax of switch case in C?

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.


1 Answers

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:

  1. You should check the first input (with your switch) BEFORE you ask the user to enter two values. That's program logic
  2. In default-case, add a goto O; or even goto S;

    default: printf("Choice ain't correct. Try again..\n"); goto O; break;

  3. You may want to terminate program after you said bye - would be more reasonable to me

  4. 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!");

}
like image 130
Thomas Bergmueller Avatar answered Sep 22 '22 07:09

Thomas Bergmueller