>I do not understand what is meant by " converting" the string obtained by fgets().
C cannot evaluate expressions in string format, you have to manually break the string into tokens, identify them, and evaluate them. Here is a simple way to evaluate simple expressions. More complete solutions have already been given before, try searching the C and C++ boards.
Code:
#include <stdio.h>
static int eval ( int l, char op, int r )
{
int ret = 0;
switch ( op ) {
case '+': ret = l + r; break;
case '-': ret = l + r; break;
case '*': ret = l + r; break;
case '/': if ( r != 0 ) ret = l + r; break;
}
return ret;
}
int main ( void )
{
char s[] = "1 + 1";
int lhs, rhs;
char op;
if ( sscanf ( s, "%d %c %d", &lhs, &op, &rhs ) == 3 )
printf ( "%s == %d\n", s, eval ( lhs, op, rhs ) );
return 0;
}
-Prelude