Home > Enterprise >  Switch case problems in c language
Switch case problems in c language

Time:06-29

it is my first time using switch..case and I need to do a program where the user choose an operator, select 2 numbers and then it shows the result, but when I do that it goes for all the options and I don't know why

Here's my code:

#include <stdio.h>

int main() {

  char op;
  double first, second;
  printf("Enter an operator ( , -, *, /): ");
  scanf("%c", &op);
  printf("Enter two operands: ");
  scanf("%lf %lf", &first, &second);

  switch (op) {
    case ' ':
      printf("%.1lf   %.1lf = %.1lf", first, second, first   second);
    case '-':
      printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
    case '*':
      printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
    case '/':
      printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
    default:
      printf("Error! operator is not correct");
  }

  return 0;
}

CodePudding user response:

You're missing the break in the finish of all your cases

CodePudding user response:

The case labels in a switch statement are exactly that: labels. They do not subdivide the body of the statement into smaller blocks; rather, they simply mark different places (different statements, in fact) to which control can branch. The behavior you describe is the natural result. If you don't do anything to prevent it, control flow that branched into the switch body at one label continues uninterrupted through other labels.

To break out of a switch at any point, use a break statement. It is usual to put a break before each case label and before the default label, if any. Example:

  switch (op) {
    case ' ':
      printf("%.1lf   %.1lf = %.1lf", first, second, first   second);
      break;
    case '-':
      printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
      break;
    case '*':
      printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
      break;
    case '/':
      printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
      break;
    default:
      printf("Error! operator is not correct");
  }

Some sources even advocate putting a break at the end of the last case, though that has no functional effect.

Occasionally, however, the programmer intentionally allows fall-through behavior. Here's a not-altogether-implausible prototype example:

switch (c) {
    case 'a':
        // fall through
    case 'e':
        // fall through
    case 'i':
        // fall through
    case 'o':
        // fall through
    case 'u':
        handle_vowel(c);
        break;
    default:
        handle_consonant(c);
}

CodePudding user response:

You are missing a break after the statement in each case. Here is the main syntax of a switch statement in C and C , but it can be applied to other programming languages too.

switch(expression) { 
   
 case value1:    
     statement_1; 
     break;
 case value2:    
    statement_2; 
    break;

  ...

 case value_n:    
     statement_n; 
     break;


 default:     
     default statement;
 
}    

  •  Tags:  
  • c
  • Related