Rui
Rui

Reputation: 39

Switch case problems in c language

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;
}

Upvotes: 1

Views: 560

Answers (3)

SamuelMR
SamuelMR

Reputation: 100

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;
}    

The break keyword tells the program to stop testing the switch cases after the statement is completed.

Upvotes: 3

John Bollinger
John Bollinger

Reputation: 180058

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);
}

Upvotes: 8

Bernardo Almeida
Bernardo Almeida

Reputation: 468

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

Upvotes: 5

Related Questions