-6

my code doesn't work as expected where i want to run the result of operation what is the problem? run results told me "compiler error" but i can't find the mistake

int main(void) {

int operation,num1,num2;

printf("please enter operation (sum,sub,mul,div) : ");
scanf("%d",& operation);
printf("please enter two number: ");
scanf("%d%d",& num1,& num2);



switch (operation)
​{
    case sum :
      printf("The summation is : %d", num1+num2);
      break; 
    case sub :
      printf("The submition is : %d", num1-num2);
      break;

    case mul :
      printf("The multiplication n is : %d", num1*num2);
      break;

    case div :
      printf("The division is : %d", num1/num2);
      break;

    default:
      printf("Not valid");
}

  return 0;
}
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
sara hamdy
  • 402
  • 2
  • 12
  • "compiler error" means that your code is not correctly written and cannot be properly compiled. – Marco Bonelli Feb 04 '20 at 14:24
  • what is the reason? – sara hamdy Feb 04 '20 at 14:25
  • 1
    You seem to be missing one or more `#include`s. Are you sure you don't need them? – Jongware Feb 04 '20 at 14:26
  • 1
    If you get compiler errors you cannot even try to run. Usually reading compiler errors turns out extremely helpful. If you want us to read them, then please show them here, as full, verbatim quotes in text form. Make sure to identify the mentioned lines. Line numbers are not helpful. So please add `// first error`, `// second error` ... – Yunnosch Feb 04 '20 at 14:26
  • 3
    There are multiple reasons... you're trying to `switch` over non-existing case variables, you are trying to read a string inside an integer... – Marco Bonelli Feb 04 '20 at 14:26
  • 1
    Does this answer your question? [C/C++: switch for non-integers](https://stackoverflow.com/questions/4165131/c-c-switch-for-non-integers) – RamblinRose Feb 04 '20 at 14:29
  • 1
    One problem: You're asking the user to enter a string, and then you are trying to use the scanf %d to save it to an integer variable. – hymie Feb 04 '20 at 14:31
  • 1
    If you get compiler errors, you should post the exact error message – klutt Feb 04 '20 at 14:37
  • 1
    Hey Sara, please try to extract the specific problem you are having and use that as a thread title instead, so that future users having the same problem can find your question and any solutions. – CausingUnderflowsEverywhere Feb 04 '20 at 14:45
  • 1
    Also "compile error" always shows more information such as the error itself, and what line it happened on. This information is rather crucial to help out and debug. – CausingUnderflowsEverywhere Feb 04 '20 at 14:46
  • 1
    Please read this: [ask] and this: [mcve] and then [edit] your question accordingly. – Jabberwocky Feb 04 '20 at 14:47
  • error:use of undeclared identifier – sara hamdy Feb 04 '20 at 15:19

2 Answers2

5

Add the following:

#define sum 1
#define sub 2
#define mul 3
#define div 4

and:

printf("please enter operation (sum - 1, sub - 2, mul - 3, div - 4) : ");
scanf("%d",& operation);

Now the switch statement should work.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
-1

If that is literally your code, sum, sub, mul, div are variables that you did not declare. Keep in mind that you are comparing operation with these variables. It will work if these are all declared and all of them have values (hence the switch control structure).

try this:

#include <stdio.h>

int main(void) {

int operation,num1,num2;

printf("please enter operation (sum,sub,mul,div) : ");
scanf("%d",& operation);
printf("please enter two number: ");
scanf("%d%d",& num1,& num2);

#define sum '+'
#define sub '-'
#define mul '*'
#define div ':' //careful with special character '/''

switch (operation)
 {
    case sum:
      printf("The summation is : %d", num1+num2);
      break; 
    case sub :
      printf("The submition is : %d", num1-num2);
      break;

    case mul :
      printf("The multiplication n is : %d", num1*num2);
      break;

    case div :
      printf("The division is : %d", num1/num2);
      break;

    default:
      printf("Not valid");
}

this is because case structures must evaluate constants, not variables...

EDIT: sorry, also check for input formats %d is for number, so it should have been 1,2,3,4 instead of +,-,*,/ - thank you @Paul Ogilvie

K09P
  • 480
  • 4
  • 13