Help using switch in C

I'm in grade 9, so still new to C. Can anyone tell me how to do this? When someone enters a value greater than 4, then he should print the label β€œdefault:” of the case switch statement. I tried to use time for this, but he gave errors. Code

#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; } } } 
+6
source share
4 answers

Your code is very bad, the reasons indicated in the comments to your post; But I think you should learn by improving your own code, which helped me in the early days:

  • You must check the first input (using your switch) before you ask the user to enter two values. This software logic
  • In the default case, add goto O; or even goto S;

    default: printf ("Wrong choice. Try again .. \ n"); go too; break;

  • You might want to complete the program after you said goodbye - it would be more reasonable for me.

  • I really really recommend reorganizing the code to run without go-to. This is a really nice job to solve with a single loop around it.

What errors, in particular, do you mean?

~ edit ~

I think that I will illustrate what I mean with some code, that how simple your program is, I hope this helps a little and you do not just copy the code, but try to understand why it is "better" (at least shorter and slightly easier to maintain and read) than yours;)

 #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!"); } 
+3
source

My solution using a while loop:

 int main(){ int o, a1, a2; printf("Hi!\r\n"); while(1){ printf("1234 - operations, else - exit\r\n"); scanf("%d", &o); if(o < 1 || o > 4) break; /* breaks infinite while loop and goes straight to "bye!" */ printf("Enter args: "); scanf("%d %d", &a1, &a2); switch(o){ /* here your switchcase statements without default just make opearions and output result */ } } printf("Bye!\r\n"); return 0; } 

The Goto operator is dangerous, not illegal, but you must use it wisely. Try creating a program using loops and if () {} else {} branches first.

0
source

In the default case, the brackets are not in the right place

  if (r==1) { printf("\n \n Restarting Loop.."); sleep(1); goto S; } else { printf("\n \t \t \t Bye!"); goto O; break; } // <------------------------------------------------------- Need this default: { // <----------------------------------------------------- Need this printf("Choice ain't correct"); break; } 
0
source

You are missing the closing parenthesis immediately before your default: a case, so it fails.

-1
source

Source: https://habr.com/ru/post/954247/


All Articles