So I want my program to do an arithmetic calculation between 2 values:
1.first, I want my program to check if the 2 values entered are numbers
2.If that's correct I wan't it to run a switch statement that depending on the operator will do the calculation
3.If the operator entered is not a valid one(*, +, -, /) then I wan't an error returned saying that the operator is not valid
4.If the values are not numbers I wan't to display that the values entered are not valid
for example if I enter K * 2 I want the program to say "Error invalid value"
If I enter 20 ? 2 the program should return "Error valid operator"
I have tried using {} and () to wrap the switch function but nothing seems to work
#include <stdio.h>
int main (void)
{
float value1, value2;
char Operator;
printf("Enter your two values expression (x (+, -, *, /) y): ");
scanf("%f %c %f", &value1, &Operator, &value2);
if( (value1>=0 && value1<=9) || (value2>=0 && value2<=9) )
switch(Operator)
{
case '+':
printf("The result is: %.2f \n", value1+value2);
break;
case '-':
printf("The result is: %.2f \n", value1-value2);
break;
case '*':
case 'x':
case 'X':
printf("The result of this multiplication is: %.2f \n",
value1*value2);
break;
case '/':
if(value1==0 || value2==0)
printf("Error 1 you can't divide by 0 \n");
else
printf("The result of this division is: %.2f \n", value1/value2);
break;
default:
printf("Error please enter a valid operator \n");
break;
}
else printf("Error please enter a valid value \n");
}
When I enter an invalid value the program returns "Error please enter a valid operator" instead of the else statement "Error please enter a valid value"
Also if the first value is correct but the second is not, let's say: 10*c Instead of returning "Error please enter a valid value" it returns 0.