I have createed this simple calculator but I am having a problem with its behavior. When i enter the first item it works fine, but after each succesive item it throws out my current total for no reason then goes and does what it should. THis is baffleing me, maybe someone else sees what i'm missing. Code and supposed and actual outputs below:
should look like:Code:void scan_data(char *op, double *num); void do_next_op(char *op, double *num); #include <stdio.h> #include <math.h> double total=0; int main(void) { double num=0; char op = 'i'; printf("Preform basic math functions and enter q when your finished to exit\nenter in the format: (operator) (operand) (ex. + 5)\n\n"); // while they want to keep going, tell them so far, when they quit give final result while(op != 'q') { scan_data(&op, &num); do_next_op(&op, &num); if(op != 'q') printf("result so far is %.1lf\n", total); } printf("final result is %.1lf\n", total); return 0; } void scan_data(char *op, double *num) { scanf("%c%lf", op, num); } // preforms the desired operation void do_next_op(char *op, double *num) { if(*op == 'q') return; else if(*op == '+') total += *num; else if(*op == '-') total -= *num; // checks for division by zero, if so does no operations and warns user else if(*op == '/') { if(*num == 0.0) printf("ERROR: division by zero, please try again, "); else total /= *num; } else if(*op == '*') total *= *num; else if(*op == '^') total = pow(total, *num); }
+ 5
result so far is 5.0
^ 2
result so far is 25.0
actually looks like:
+ 5
result so far is 5.0
^ 2
result so far is 5.0
result so far is 25.0



LinkBack URL
About LinkBacks


