# it won't print the 0 if i enter 102

• 04-16-2009
maybabier
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...
• 04-17-2009
matsp
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