Thread: Not sure where the problem is

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    Not sure where the problem is

    Hello, i'm working on part of a simplistic calculator and encountered a problem. The function that im working on is given an integer and prints it. It works for any number as long as it does not include 0. If a number like 2012 is read, the 0 is simply ignored and the function prints 212. I can't seem to figure out why this is happening. heres the code.
    insert
    Code:
    /*  This file contains the code to convert an integer into a
    	sequence of characters and print them.  The code uses
    	conditonal compilation to determine whether or not to
    	use the calculator display.
    */
    
    /* define for use without the display 
    #define DISPLAY
    */
    
    
    #include "presults.h"
    #include "display.h"
    #include "chrutil.h"
    
    
    void put_result(int ans)
    /*  This function is given an integer and prints it one digit at a time
    	either to the calc display or the stdout.
    */
    {  int wt;	/*  the weight of the msd  */
    	
    	/*  if the integer is 0, print it and return  */
    	if(ans == 0)
    	{
    		#ifdef DISPLAY
    		write_char('0');
    		#else
    		putchar('0');
    		#endif
    		return;
    	}
    	/*  if the integer is negative, print the '-' and make it pos.  */
    	if(ans < 0)
    	{
    		#ifdef DISPLAY
    		write_char('-');
    		#else
    		putchar('-');
    		#endif
    		ans = -ans;
    	}
    	/*  find the weight of the msd  */
    	wt = weight(ans);
    	/*  while there are more digits  */
    	while(wt >= 1)
    	{	/*  get msd, convert to char, and print it  */
    		#ifdef DISPLAY
    		write_char(int_to_dig(sig_dig_value(ans,wt)));
    		#else
    		putchar(int_to_dig(sig_dig_value(ans,wt)));
    		#endif
    		/*  strip the msd  */
    		ans = supress_msd(ans,wt);
    		/*  go on to next weight  */
    		wt = weight(ans);
    	}
    
    }
    
    
    int sig_dig_value(int n, int wt)
    /*  This function is given and integer and the current weight.  It
    	returns the integer value of the most significant digit.  */
    {  return n/wt; }
    
    int supress_msd(int n, int wt)
    /*  This function is given an integer and the current weight.  It
    	returns an integer with the most significant digit removed.  */
    {  return n % wt;  }
    
    int weight(int n)
    /*  This function is given an integer.  It returns the weight  (a power
    	of 10) of the most significant digit.  */
    {  int wt = 1;
    
    	while((n/wt) != 0)
    		wt = wt * BASE;
    
    	wt = wt / BASE;
    	return wt;
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you do not provide whole the code, but looking at the alg it seems that you strip digits from the left and calculate the weight (number of digits left)

    when you start with 2012 and strip left digit you get 012 which is automatically transferred to 12 - so next time your weight will be 2 not 3

    instead of recalculating weight on each iteration - just do it once in the beginning and then - use wt-- till you get to the last digit (wt == 0)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your weight would go from 4 to 2, in that example. Naturally, that means the slot for "weight 3" goes missing. Rather than recalculate your weight, you should just make it go down by one.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Figured it out. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM