Thread: Help me debug this code

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    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

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    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.
    Last edited by Ancient Dragon; 10-28-2005 at 07:23 AM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    thanks!

    I though it was somthing like that, I will try looking for one of those

  4. #4
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    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.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    Thanks for the help, its working fine now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  2. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  3. Debugging leads to buggy code and longer hours?
    By no-one in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-28-2002, 11:14 AM
  4. anyone bored enough to help me debug some code?
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 01-18-2002, 07:55 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM