I am trying to get a small piece of a program i am writing to receive a simple math expression, with two int variables and 1 char for the operator, and then i will work it with an if(){}if else statement. I just don't know how to separate the string into variables. Can anyone help? I have read a lot of stuff on the web and it is more confusing than my c programming class.

include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define bufSize 8160

int main(){
     char evalString [bufSize];
     char evalOp;
     int evalInt1, evalInt2, evalAns;


     printf("Enter valid Simple Equation:\n ");
     fgets(evalString,bufSize,stdin);
     
     //this is where i am stuck, what will read the  char and both int's and store them in memory

     evalAns = (evalInt1 evalOp evalInt2) //not sure that will work either
printf("%d %c %d = %d", evalInt1, evalOp, evalInt2, evalAns)
     scanf("%*c");
     return 0;
}

Thanks!

Edited 6 Years Ago by thomasekugm: n/a

thanks Aia, but how do i incorporate that? Pretend i am a college student in an intro to c class. Oh yeah, i am ! lol

thanks Aia, but how do i incorporate that? Pretend i am a college student in an intro to c class. Oh yeah, i am ! lol

There was a tiny example at the bottom of the linked page.
But for what it is worth, here's another example

#include <stdio.h>

int main(void)
{
    char input[] = "2 3 +";
    int a, b;
    char operator;

    if (sscanf(input, "%d %d %c", &a, &b, &operator) == 3) {
        printf("%d %c %d = ?\n", a, operator, b);
    }

    return 0;
}

Of course, I left for you the part where you have to figure out what operator the user input.

Hint: a switch might work.

Edited 6 Years Ago by Aia: n/a

Comments
Superb example :)
This article has been dead for over six months. Start a new discussion instead.