cscanfgets

Need to write a program acts like an integer calculator


New with CS, and new with C language. Please help with the program needs to support all +, -, *, /, % functions and return the user input equation's result. The program should be continuously accepting new input until the user enters a blank line. and shouldn't print any output except for the equation's result.

e.g. input and output:

8 + 2 (enter) - user input

10 - output

8 - 2 (enter) - user input

6 - output

(enter) - user input(program terminates)

the hint was provided are use gets() & sscanf() to take input from user.

 #include <stdio.h>
    
    int main()
    {
            char equation[10];
            int x, y, result;
            char eqsign[2];
            int switchnum;
            
            gets(equation);
            sscanf(equation, "%d %s %d", &x, eqsign, &y);
            
            if (eqsign == '+')
                switchnum = 0;
            else if (eqsign == '-')
                switchnum = 1;
            else if (eqsign == '*')
                switchnum = 2;
            else if (eqsign == '/')
                switchnum = 3;
            else if (eqsign == '%')
                switchnum = 4;
            
            while(equation != "\0")
            {
                switch(switchnum)
                    case '0':
                        result = x + y;
                        printf("%d", result);
                        break;
                   case '1':
                        result = x - y;
                        printf("%d", result);
                        break;
                   case '2':
                        result = x * y;
                        printf("%d", result);
                        break;
                   case '3':
                        result = x / y;
                        printf("%d", result);
                        break;
                   case '4':
                        result = x % y;
                        printf("%d", result);
                        break;    
            }
            return 0;
}

The code I have now is not running correctly, please provide any suggestions with C! Having a lot of problems on how to compare the operator form input. Any help would be appreciated!


Solution

  • Since the operator is a single character, there is no need to make a string of it. Also, there is no need for switchnum, as you can switch directly on the operator value.

    #include <stdio.h>
    
    int main()
    {
        char equation[22];
        int x, y, result;
        char operator;
                
        if (!fgets(equation,sizeof(equation),stdin)) {
            perror("Reading equation");
            return -1;
        }
        if (sscanf(equation, "%d %c %d", &x, &operator, &y) != 3) {
            fprintf(stderr,"Invalid equation!\n");
            return -2;
        }
        switch(operator) {
            case '+':
                result = x + y;
                break;
            case '-':
                result = x - y;
                break;
            case '*':
                result = x * y;
                break;
            case '/':
                result = x / y;
                break;
            case '%':
                result = x % y;
                break;
            default:
                fprintf(stderr,"Invalid operator '%c'!\n",operator);
                return -3;
        }
        printf("%d", result);
        return 0;
    }