I have to make a calculator which will based on user's input, do specific operation. First of the input must be some kind of operator(+
, -
, *
, etc.) and after the code checks which one of those is user's choice
. I declared those operators as char
, but my code editor says that I can't put char
variables as case statements...What should I do?
Code:
#include <stdio.h>
#include <math.h>
int main(){
char choice = "+", "*", "-", "/";
int a, b;
float outcome;
scanf("%c", &choice);
switch (choice)
{
case "+":
scanf("d% d%", &a, &b);
outcome = a + b;
printf("%.1f", outcome);
break;
case "*":
scanf("%d %d", &a, &b);
outcome = a * b;
printf("%.1f", outcome);
break;
case "-":
scanf("%d %d", &a, &b);
outcome = a - b;
printf("%.1f", outcome);
break;
case "/":
scanf("%d %d", &a, &b);
outcome = a / b;
printf("%.1f", outcome);
break;
}
return 0;
}