-4

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!

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jo W
  • 1
  • 4
  • 1
    Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please take the [tour] and read about [ask] good questions. Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Jan 28 '22 at 21:12
  • Also please read [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) as well as [Open letter to students with homework problems](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems) – Some programmer dude Jan 28 '22 at 21:12
  • 3
    And never ***ever*** use `gets`! It's so [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) is has been removed from the C language all together. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Jan 28 '22 at 21:14
  • 1
    `while(equation != "\0")` That's not how strings are compared in C. Use `strcmp`. Also, how do you expect that loop to end once it has been entered? There is nothing that changes `equation` in the loop. – kaylum Jan 28 '22 at 21:14
  • Also, you loop makes no sense (besides the string comparison issue, do you ever modify `equation` inside the loop?); You need to check what [`sscanf` *returns*](https://en.cppreference.com/w/c/io/fscanf#Return_value); And add a bounds in the `%s` format specifier for `sscanf`. – Some programmer dude Jan 28 '22 at 21:16
  • `if (eqsign == '+')` The compiler should be giving you warnings that you should not ignore. `eqsign` is a string but you are comparing it against a char. Either do `eqsign[0] == '+'` or use `strcmp(eqsign, "+")` – kaylum Jan 28 '22 at 21:16
  • Using `%s` for input to `char eqsign[2];` is asking for trouble. If you must use strings, restrict the length with `%1s`. – Weather Vane Jan 28 '22 at 21:22
  • 2
    *"the hint was provided are use gets()"* - burn that reference guide/tutorial. That thing is considered so vile an evil it was formally *removed* from the C standard library over a decade ago, and is, today, only provided by implementations that want to continue to support train-wreck-awaiting code at least that old, or older. – WhozCraig Jan 28 '22 at 21:26

1 Answers1

1

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;
}
SGeorgiades
  • 1,771
  • 1
  • 11
  • 11