Hi, I would like to create a C program which requires the input in this form: number1 operator number2, where number1 and number2 are numbers of type double and operator is one of the characters +,-,*,/.
There is my attempt to write this code:
Code:
#include <stdio.h>
double main()
{
char operator;
double num1, num2;
printf("Type what you want to calculate\n");
scanf("%d%c%d", &num1, &operator, &num2);
switch(operator);
{
case '+':
printf("num1+num2=%.2f",num1+num2);
break;
case '-':
printf("num1-num2=%.2f",num1-num2);
break;
case '*':
printf("num1*num2=%.2f",num1*num2);
break;
case '/':
if(num2 != 0)
{
printf("num1/num2=%.2f",num1/num2);
break;
}
else
{
printf("Error! operator is not correct");
break;
}
default:
printf("Error! operator is not correct");
break;
}
return 0;
}
Now I have to solve these problems:
- This code above doesn't work and I don't know why.
- I don't know how to fix the case when some user enters the input in this form: or I mean how the program knows that is the same as
- I don't know how to fix the case when the user enters the input in the incorrect form for example
I am a beginner in C, please take note of this while explaining.
Thanks