I have a class exercise that ask us write a C-program to read 2 numbers from user and perform an operation based on the operator entered by the user. The Pseudocode is this.
- Get 2 numbers from user.
- Get operator from user (e.g +,-,/,*).
- if operator entered = +, add the 2 numbers, else if ....
This is my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x,y;
char op; //op is the identifier meaning operator
printf("Enter x: ");
scanf("%d",&x);
printf("Enter y: ");
scanf("%d",&y);
printf("Enter op: ");
scanf("%ch",&op);
if(op == '+'){
printf("%d + %d = %d \n",x,y,x+y);
}else if(op == '-'){
printf("%d + %d = %d \n",x,y,x-y);
}else if(op == '*'){
printf("%d + %d = %d \n",x,y,x*y);
}else if(op == '/'){
printf("%d + %d = %d \n",x,y,x/y);
}else{
printf("Invalid operator entered!\n");
}
}
This is the output i get when i run the code
I've done this before, sometimes last year and everything ran smoothly, but now it isnt. i dont know if its from my IDE or something i'm getting wrong (I use Codeblocks as my IDE for C-lang )
i've tried to change the operator from a single to character to a string but its still not working.