0

im new to programming and im a first year in college, my teacher gave me and my group mates a task to create a console calculator but we run into a problem since this is our first problem.

the problem is that if the input does not satisfied or met any condition in my if statement it loops the error.

here is the code:

#include <stdbool.h>

double calculate(double number1, char operation, double number2);

bool isInteger(double val)
{
    int truncated = (int)val;
    return (val == truncated);
}

int main()
{
    double number1, number2, result;
    char operation;
    int validator = 0, operationValid = 0;
    
    while(validator == 0)
    {
        fflush(stdin);
        printf("Enter number and operator to solve:\n\nExapmple [a + b][a - b][a * b][a / b]\n");
        scanf("%lf %c %lf", &number1, &operation, &number2);
        
        while(operationValid == 0)
        {
            fflush(stdin);
            
            if(operation == '+' || operation == '-' || operation == '*' || operation == '/')
            {
                if(isInteger(number1) && isInteger(number2))
                {
                    result = calculate(number1, operation, number2);
                    printf("%.1lf %c %.1lf = %.1lf", number1, operation, number2, result);
                    operationValid = 1;
                    validator = 1;
                }
                else
                {
                    printf("Invalid Inputed numbers!");
                    operationValid = 0;
                    validator = 0;
                }   
            }
            else
            {
                printf("Invalid Operation!");
                operationValid = 0;
                validator = 0;
            }
        }
    }
}

double calculate(double number1, char operation, double number2)
{
    double a, b, c;
    char x;
    
    a = number1;
    b = number2;
    x = operation;
    
    if(x == '+')
    {
        c = a + b;
    }
    else if(x == '-')
    {
        c = a - b;
    }
    else if(x == '*')
    {
        c = a * b;
    }
    else if(x == '/')
    {
        c = a / b;
    }
    return c;
}

please if someone can provide help and maybe a little explanation. Thanks.

  • 2
    `fflush(stdin);` is undefined behaviour and often does not work (platform/implementation dependent). Don't use it or rely on it. See: [Using fflush(stdin)](https://stackoverflow.com/questions/2979209/using-fflushstdin). Instead use a function like `fgets` with `sscanf` in place of `scanf`. The former is guaranteed to consume the input whereas the latter leaves any unmatched input in the stream. – kaylum Oct 13 '21 at 11:16
  • @kaylum How do i implement or use it? do you have some sample codes? – kurtivan23 Oct 13 '21 at 11:29
  • Which tutorial or text book did you consult (besides the spec) about how to use `fgets` and `sscanf` and what is left unclear about it? – Gerhardh Oct 13 '21 at 11:39
  • i dont know, maybe the code itself is wrong, but what we want is that the calculator will validate if operation is + - * / and input numbers are digit, with loop if error prompt to beginning but idk how to do that. since my teacher just gave as some small pdf and video discussion, which is very hard. and no examples at all. – kurtivan23 Oct 13 '21 at 11:42
  • @kaylum i think we cant use those since we are still in scanf since that is the only given get input function in our discussion. so i dont really know if better use it. – kurtivan23 Oct 13 '21 at 11:55
  • @kurtivan23 That would be odd to have that restriction. `scanf` is just not suited to handling anything more than simple well formed data. Anyway if you must use it then start by checking its return value. If it is returns anything other than 3 in this case it means there was an error or mismatch. In that case you can for example call `scanf("%c"` in a loop to clear the data. – kaylum Oct 13 '21 at 19:50

0 Answers0