# Help me debug this code

• 10-28-2005
Blgihted
Help me debug this code
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:

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); }```
should look like:

+ 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
• 10-28-2005
Ancient Dragon
you need to flush the keyboard after scanf() to remove the '\n' that is still in the keyboard buffer. There are several threads about that, Here is just one of them . There is no standard way to do that, but find the threads and they will give you some ways to work around that problem.
• 10-28-2005
Blgihted
thanks!

I though it was somthing like that, I will try looking for one of those
• 10-28-2005
lemnisca
Don't try to flush the input. This is unnecessary, non-standard and generally a bad idea. You are better off firstly checking the return value of scanf, which you should be doing anyway, and secondly just adding a space to the end of your format string like so:
Code:

`scanf("%c%lf ", op, num);`
(note the space afre %lf). This will match any whitespace, including a newline, so the newline will not be left on the buffer and you are ready for your next scanf call.

But definitely check the return value regardless, that's something you should always do.
• 10-28-2005
Blgihted
Thanks for the help, its working fine now