1

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.

  • 2
    You need to check what `scanf` [***returns***](https://en.cppreference.com/w/c/io/fscanf#Return_value). – Some programmer dude Dec 26 '18 at 08:56
  • 2
    And please mind your indentation. Right now it looks like the `switch` statement is outside the `if`. Also consider using `{}` to surround larger blocks of code inside statements. Both these things will make the code much easier to read and understand. – Some programmer dude Dec 26 '18 at 08:58
  • 1
    But if I add {} after the If statement wrapping the switch statement I get an error with the else function :( , why is that? – Alejandro Rosales Dec 26 '18 at 09:05
  • 1
    out of your problem, why do you refuse to divide 0 by something != 0 ? you do not have to test if value1 is 0 or not, just to check value2 – bruno Dec 26 '18 at 09:06
  • 1
    You need to change `||` to `&&`. Right now you're entering the `switch` if *either* operand is valid. But what you want is to only enter the `switch` if *both* operands are valid. – Tom Karzes Dec 26 '18 at 10:43

3 Answers3

2

as it was already said when you enter 20 ? 2 because you have if( (value1>=0 && value1<=9) || (value2>=0 && value2<=9) ) the error will concern the value rather than the operator. This test of the values must be removed.

you just have to check the result of scanf

  • if it 0 the first entered value is not a valid float

  • if it is 1 only a valid float was entered, it is not followed by a space then the operator

  • it if is 2 only a valid float then a space then the operator was entered, after the operator there is not a space then a valid float

  • it if is 3 you know value1 and value2 are valid values and the operator was entered, you can do your switch to test operator and compute the result

bruno
  • 32,421
  • 7
  • 25
  • 37
0

The only problem in your code, besides your indentation and not wrapping some things inside { }s (although it should work this way too), is that you are reading in your numbers incorrectly.

  1. In the if( (value1>=0 && value1<=9) || (value2>=0 && value2<=9) ), you are checking only for values between 0 and 9. If you enter 10, it's not going to work. I see that you were trying to check for characters, like '0' or '9', but then you need another function to process what you've just read in.
    EDIT: I didn't notice at first, but your if has || instead of &&. Right now, the test will pass even if only one of your numbers is valid.
  2. You read in with a scanf("%f", ...). Let's say I enter a K there. The computer will try to process it like it was a floating point number.

I would suggest using a fgets and then processing it with another function, character by character, building up your numbers. If anyone knows a better way, feel free to comment. I hope this helps.

Zsombor
  • 45
  • 9
  • Never, ever suggest `gets()` on SO. [The `gets()` function is far too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) It is better to use `fgets()` and `sscanf()` to read a line and then parse it. – Jonathan Leffler Dec 26 '18 at 09:16
  • Although the `if` statement is wrong, as long as at least one of `value1` and `value2` is in the range `0 .. 9`, it doesn't matter if the other is outside the range; the test will pass. The `||` should be an `&&` if both numbers should be in the range `0 .. 9`. – Jonathan Leffler Dec 26 '18 at 09:20
0

To make it simple I have changed float to integer type.

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
int value1, value2;
char Operator;

printf("Enter your two values expression (x (+, -, *, /) y): ");
if (scanf("%d %c %d", &value1, &Operator, &value2) != 3)
{
  printf("Either of value1 or value2 is not a number. Enter valid input\n");
  return EXIT_FAILURE;
}
switch(Operator)
{
    case '+':
        printf("The result is: %d \n", value1+value2);
        break;

    case '-':
        printf("The result is: %d \n", value1-value2);
        break;

    case '*':
    case 'x':
    case 'X':
        printf("The result of this multiplication is: %d \n", value1*value2);

    break;

    case '/':

        if(value2==0)
        {
        printf("you can't divide by 0, Hence value2 must be an non zero integer\n");
        }
        else
            printf("The result of this division is: %d \n", value1/value2);
        break;

    default:
        printf("Error please enter a valid operator \n");
        break;

}
return;
}
Helping Bean
  • 147
  • 7