i am currently writing a program that is meant to evaluate a mathematical expression of the form a*b-c*d-f . This expression can be of any length and possibly of float variables.
however, my piece of code is meant to evaluate only about two variables and not more... So i need help with a loop that can evaluate an expression of any length.
Code://here is my code #include <stdio.h> #include <stdlib.h> int main(void) { int s; char op; float exp1, exp2, sum = 0; printf ("\nThis program evaluates a mathematical expression of any length."); printf("\nEnter 'Q' to exit."); for ( ; ; ) { printf("\nEnter Expression: "); while(getchar() != '\n') { s = scanf(" %f %c %f", &exp1,op,exp2); getchar(); // i don't however feel comfortable putting //getchar here... i need help with this loop if ( s != 3) { printf("\nExiting..."); exit(EXIT_SUCCESS); } if ( op == '+') sum += exp1 + exp2; else if ( op == '-') sum += exp1 - exp2; else if ( op == '/') sum += exp1 / exp2; else if ( op == '*') sum += exp1 * exp2; } printf("\nExpression: %.2f", sum); } return 0; } //this code may run only for two expressions and not more....