it won't print the 0 if i enter 102

This is a discussion on it won't print the 0 if i enter 102 within the C Programming forums, part of the General Programming Boards category; Code: #include "presults.h" #include "display.h" #include "chrutil.h" int put_result(int ans) /* This function is given an integer and prints it ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    30

    it won't print the 0 if i enter 102

    Code:
    #include "presults.h"
    #include "display.h"
    #include "chrutil.h"
    
    
    int 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)
    	{
    
    		putchar('0');
    		ans = ans;
    	}
    	/*  if the integer is negative, print the '-' and make it pos.  */
    	if(ans < 0)
    	{
    
    		putchar('-');
    		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  */
    
    		putchar(int_to_dig(sig_dig_value(ans,wt)));
    
    		/*  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;
    }
    all other numbers work well besides 0.
    if i put 10001 it prints 11
    if i put 102 it prints 12...

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Wow, that's a lot of code for something that I'd probably write in 5-7 lines.

    Code:
    	while((n/wt) != 0)
    		wt = wt * BASE;
    This effectively prevents any "zero" values from being printed, I think.

    Here's my obfuscated solution [it doesn't deal with negative tho'] (don't use this!):
    Code:
    void printnumber(int n)
    {
        char str[13]={}, *s=&str[12];do{*--s='0'+n%10;n/=10;}while(n);
        puts(s);
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf statement executed twice
    By ananddr in forum C Programming
    Replies: 4
    Last Post: 05-07-2009, 01:17 AM
  2. A Diary Gone Nuts
    By chris Ray in forum C++ Programming
    Replies: 14
    Last Post: 12-09-2008, 12:32 PM
  3. merging linked lists
    By scwizzo in forum C++ Programming
    Replies: 15
    Last Post: 09-14-2008, 06:07 PM
  4. how to print a file on the screen
    By tameeyore in forum C Programming
    Replies: 4
    Last Post: 09-18-2005, 01:00 PM
  5. print astrik when enter password
    By ck12 in forum C++ Programming
    Replies: 0
    Last Post: 10-18-2001, 11:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21