Search code examples
cio

How can I retrieve three variables from a user input in C


I need to make a small calculator for school, using the following template: "operation(operator1, operator2)". E.G.: "Add(1, 2)" will return 3, "Multiply(2, 5)" will return 10. I know I can use a strtok to retrieve the operation, but I'm not sure how to retrieve the operators. Currently I have the following:

#include <stdio.h>
int main()
    {
        char Math_Input[32]; //This is where I will store the input in the following template: operation("operator1, operator2")
        float Operator_1, Operator_2;

        printf("What calculation do you wish to do?: ");
        fgets(Math_Input,20,stdin); //Here I retrieve the entire line and I'll store it in Math_Operation, next step is to retrieve the operation, operator1 and operator2

        char * Math_Operation = strtok(Math_Input, "(");

        printf("%s\n", Math_Operation);

    }

Update:

After a bit of code given which is using sscanf() I revised my code to the following:


#include <stdio.h>
int main()
    {
        char Math_Input[32]; //This is where I will store the input in the following template: operation("operator1, operator2")
        char Math_operation[16];
        float Operator_1, Operator_2;

        printf("What calculation do you wish to do?: ");
        fgets(Math_Input,20,stdin); //Here I retrieve the entire line and I'll store it in Math_Input, next step is to retrieve the operation, operator1 and operator2
        if (3 != sscanf(Math_Input, "%15s (%f ,%f )", Math_operation, &Operator_1, &Operator_2)) {
            fprintf(stderr, "Incorrect line >%s<\n", Math_Input);
                printf("Operation: %s \n", Math_operation);
                printf("Operator1: %d \n", Operator_1);
                printf("Operator2: %d \n", Operator_2);
            }
        else {
                // Ok process the operation...
                printf("Else");
                printf("Operation: %s \n", Math_operation);
                printf("Operator1: %d \n", Operator_1);
                printf("Operator2: %d \n", Operator_2);
            }

    }

I used a few printf's to debug. This should work in theory right? But when I test it out like this: (so this is my console) it doesnt seem to work...

What calculation do you wish to do?: Add(1, 2)
Incorrect line >Add(1, 2)
<
Operation: Add(1,
Operator1: 0
Operator2: 0

Process returned 0 (0x0)   execution time : 3.698 s
Press any key to continue.

Solution

  • If the input is expect to be in the format operation(value_1, value2), it looks like a correct use case for sscanf. The scanf family is said to be a poor man's parser, because it has no support for error recovery. But here you have already got your input line from fgets and only need a trivial parsing. So I would just use:

        char Math_Input[32]; //This is where I will store the input in the following template: operation("operator1, operator2")
        char Math_operation[16];
        float Operator_1, Operator_2;
    
        printf("What calculation do you wish to do?: ");
        fgets(Math_Input,20,stdin); //Here I retrieve the entire line and I'll store it in Math_Operation, next step is to retrieve the operation, operator1 and operator2
        if (3 != sscanf(Math_Input, "%15[^ (] (%f ,%f )", Math_operation, &Operator_1, &Operator_2)) {
            fprintf(stderr, "Incorrect line >%s<\n", Math_input);
        }
        else {
            // Ok process the operation...
        }
        ...
    

    The good news with sscanf is that it will ignore any non significative blank character.

    Additional improvements: make sure that fgets could get a complete line (not more than 19 characters) else read until the end of line in you later need a loop