1

I am trying to use strtok to break a simple equation like 5 + 4 down and store each part into an array, and once I have done this, perform the operation indicated.

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
//#include "stdafx.h"

int main() {
    char uin[10], op[10], error = 'e';
    char *token;
    const char s[2] = " ";
    double num1, num2, fNum1, res;
    int i = 0;
    int x, y, z, a, b, c, op1;

    printf("Welcome to the calculator, please enter an equation\n");

    while (error == 'e') { // & assigns address and * gives access to what it points to
        a = &uin[0];
        b = &uin[2];
        c = &uin[4];

        gets(uin);
        rewind(stdin);
        printf("Is this what you entered? %s\n", uin);
        token = strtok(uin, s);
        //x = &token;
        //printf("The element in the array currently assigned to token is: %s\n", token);

        while (token != NULL) {
            if (isdigit(token[0])) {
                num1 = atof(token);
                printf("token is now: %1.2f\n", num1);
            } else
                strcpy(op, token);

            token = strtok(NULL, s);
            if (isdigit(token[0])) {
                num1 = atof(token);
            } else
                strcpy(op, token);

            //token = strtok(NULL, s);
            //y = &token;
            //printf("The element in the array currently assigned to token is: %s\n", token);
        }

        //token = strtok(NULL, s);
        //y = &token;
        //printf("The element in the array currently assigned to token is: %s\n", token);

        //token = strtok(NULL, s);
        //z = &token;
        //printf("The element in the array currently assigned to token is: %s\n", token);

    }

    system("pause");
}

I am really having a hard time with this. I think that I am using strtok correctly to take the first part of the gets(uin) and storing it, but I don't understand how to take the middle part (+ - * or /) and store it.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • In K&R book, they write a calculator (reverse polish). They just use `getchar`. That way you can test all the input as it come in. If it's a digit, or one of the symbols to do a calculation. They use switches to test for `+, -, *, /, **`. `getchar` give you very good control over everything. – Manny_Mar Feb 09 '19 at 20:57
  • 1
    When i tried compiling your code, a got a lot of warnings. `test.c:19:11: warning: assignment makes integer from pointer without a cast [-Wint-conversion] a = &uin[0];` A bunch of those, and also `test.c:(.text+0x54): warning: the gets function is dangerous and should not be used.` Use `fgets` instead. – Manny_Mar Feb 09 '19 at 21:18
  • Using `gets();` is now a *"Hanging-Offense"*, use `fgets` instead and throw away whatever reference suggested using `gets()`. See [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) – David C. Rankin Feb 10 '19 at 09:45

1 Answers1

1

strtok is not the proper tool for your purpose. strtok modifies the string, overwriting the separator. It would only work for expressions where all tokens are separated by spaces, an unnecessary and counterintuitive constraint. For a more effective approach, either use a pointer and explicit tests for each character or use non intrusive parsing helpers such as strspn(), strcspn() and strtold().

Furthermore, you should not use gets(), this function is obsolete and was removed from the latest version of the C Standard. It cannot be used safely: any sufficiently large input will corrupt your program and have undefined behavior. Use fgets() instead to read a full line into a sufficiently large array: 10 bytes is definitely too small. rewind() is also unnecessary.

chqrlie
  • 131,814
  • 10
  • 121
  • 189