If user inputs "sum(x,y)" how can I get x and y as doubles

I am writing a simple calculator program such that the user is entered into the programs own console environment. Each new line starts with a $. User will enter commands such as '''sum(x,y)''' '''mul(x,y)''' etc. and the program will take x and y as doubles and execute a function i.e. '''double mul(x, y){return x*y)''' and display the returned value.

I have gotten it to correctly identify the command based on matching the first three characters in the string.

I cant seem to read the numerical values correctly no matter what I do. Any suggestions are greatly appreciated!

The Code below is from main()

    printf("%s", " $    ");
    fgets(input, sizeof(input), stdin);

        //char *starting_pos = strchr(input,dlm);
        //int position = (starting_pos == NULL ? -1 : (uintptr_t)starting_pos);

        //arg1 = input[position + 1];
        //arg2 = input[position + 3];
        printf("DEBUG: arg1 is %d arg2 is %d\n", arg1,arg2);
        double output = sum(arg1,arg2);
        printf("DEBUG: Output should be: %d\n", output);

    }else if ...

The commented section is the remnants of another way I was trying. If anyone can tell me what the right way to do this is I would be very grateful.

1 answer

  • answered 2021-03-03 02:45 David C. Rankin

    Good job in reading with fgets() and then parsing values with sscanf(). If I understand the question, and you may have multiple operators followed by two doubles in parenthesis, e.g. SUM(1.1, 2.2), and you want to separate and save the operator, and the double values, then you can use a format string such as:

    #define MAXOP   32
        char op[MAXOP];
        sscanf (input, "%31[^( ] (%lf,%lf)", op, &x, &y)

    This will read from input separating everything up to a whitespace or '(' into op and then reading the double values into x and y. By reading the op to a whitespace or '(', you allow input of sum(1.1, 2.2) or sum (1.1, 2.2) seamlessly.

    For example, you could do:

    #include <stdio.h>
    #define MAXC  1024
    #define MAXOP   32
    int main (void) {
        while (1) {
            char input[MAXC], op[MAXOP];
            double x, y;
            fputs ("\nenter OP(x, y): ", stdout);
            fflush (stdout);
            if (!fgets (input, MAXC, stdin)) {
                puts ("(user canceled input)");
                return 0;
            if (*input == '\n')
            if (sscanf (input, "%31[^( ] (%lf,%lf)", op, &x, &y) == 3)
                printf (" op: '%s'  x: %.2f  y: %.2f\n", op, x, y);
                fputs ("  error: invalid input.\n", stderr);

    Example Use/Output

    enter OP(x, y): mult(10.1,12.4)
     op: 'mult'  x: 10.10  y: 12.40
    enter OP(x, y): mult (8.3, 12.1)
     op: 'mult'  x: 8.30  y: 12.10
    enter OP(x, y): sum(1.1, pickles)
      error: invalid input.
    enter OP(x, y): sum(1.1, 2.2)
     op: 'sum'  x: 1.10  y: 2.20
    enter OP(x, y):

    Let me know if you have further question, or if I misunderstood the question.