Can someone please help me with this program? My goal is to
create a test driver for the function put_result(). The program compiles and works as intended. However, I came across a bug that I am having an extremely difficult time trying to fix.

I tested for several inputs and these were my results:

Enter integer: 9
9
Enter integer: 15
15
Enter integer: 100
1
Enter integer: 101
11
Enter integer: 190
19

The problem is that it is not reading in any zero's. I manually traced the program and discovered that the bug lies in this section of presults.c

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);
        }

The weight of 101 is 100. Since it is greater than 1, it goes into the while loop. The result of putchar is the character '1'. It then goes into the function supress_msd(100, 100) and the new value for ans is 0. The new value of wt becomes 0, and that is where the loop ends. So therefore, the problem must be in supress_msd(). Can someone please give me a suggestion of how I can edit this function so that it would return the remaining digits of the input after the previously printed one?

If I would enter 112, suppress_msd returns 12 and the weight becomes 10.

If I enter 100, suppress_msd returns 0 and the weight becomes 0. This is not my desired result. I expect for suppress_msd to return 00 and for the weight to be 10.

This is becoming extremely frustrating now. Could someone please please help me?

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);
        }

There are all the files:

/* This driver will test out the function put_result() */


#include <stdio.h>
#include "presults.h"
#define DEBUG

main()
{
 /* Declare variables */
 int integer;   /* Integer prompted at input */
 int input;     /* Reads input */

        /* Prompt user for input */
        printf("Enter integer: ");

        /* While there are more integers at input */
        while( scanf("%d", &integer) != EOF)
        {
         /* Call function to print digit */
         put_result(integer);

         /* Print a blank line */
         printf("\n");

         /* Update loop */
         printf("Enter integer: ");

        }
}
/*  This file contains prototypes for presults.c  */

#define BASE    10

void put_result(int ans);
/*  This function is given an integer and prints it to the dispaly
        one digit at a time.
*/

int sig_dig_value(int n, int wt);
/*  This function is given an integer and the current weight.  It
        returns the integer value of the most significant digit.
*/

int supress_msd(int n, int wt);
/*  This function is given an integer and the current weight.  It
        returns the integer with the most significant digit removed.
*/

int weight(int n);
/*  This function is given an integer.  It returns the weight (a power
        of 10) of the most significant digit.
*/
#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;
}
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);
        }

Edited 6 Years Ago by tquiva: n/a

Comments
this is actually a very well phrased question, with clear use of code, and descriptive examples of what was expected contrasted w/ what was wrong. I wish every poster could be so clear and coherent.

dude, that's WAY too much code to look at.

but it's nicely formatted and commented. so thanks for that.

i'll try and give it a once over, but in the meantime, you should really learn to use fgets() inconjunction with strtol() to get your input. using scanf() can do it correctly, but it is overly-complicated to make it robust, and prone to input error IMO.

EDIT: okay, this problem obviously has nothing to do with scanf. sorry, it's just the usual suspect
.

Edited 6 Years Ago by jephthah: n/a

actually, sorry, that is just too much code to look at. especially since i dont really understand what you're trying to do. and you've got some funky non-standard libraries going on in there, that i wouldn't be able to compile it even if i wanted to.

can you explain, simply, what exactly youre trying to do? maybe one of us can give you a hint as to how we might do it and then you could compare methods.

i really suspect that your code is about 10-20 times too large for what you need to do.

.

Edited 6 Years Ago by jephthah: n/a

actually, that's too much code to look at. especially since i dont really understand what you're trying to do.

can you explain, simply, what exactly youre trying to do? maybe one of us can give you a hint as to how we might do it and then you could compare methods.

so sorry.
Basically, I want to enter an integer at input. The driver reads it and puts it into the function put_result. The function then returns the integer as a character/digit. My problem is that it is not returning any 0's and I discovered the problems lies in the function suppress_msd.

Edited 6 Years Ago by tquiva: n/a

I forgot to provide the chrutil.h code, but here it is

/* File: chrutil.h */
/*   This file contains various  macros and prototypes for character processing */

#define   ERROR     -2

#define   IS_DIGIT(c)   ((c) >= '0' && (c) <= '9')
#define   IS_LOWER(c)   ((c) >= 'a' && (c) <= 'z')
#define   IS_UPPER(c)   ((c) >= 'A' && (c) <= 'Z')
#define   IS_WHITE_SPACE(c)   ((c) == ' ' || (c) == '\t' || (c) == '\n')
#define   IS_PRINT(c)   ((c) >= 32 && (c) < 127)

#define  LOWER     0
#define  UPPER     1
#define  DIGIT     2
#define  PUNCT     3
#define  SPACE     4
#define  CONTROL   5
#define  SPECIAL   6


int dig_to_int(char ch);
char int_to_dig(int n);
char uppercase(char ch);
int getint();

int delimitp(char c);
int whitep(char c);
int punctp(char c);
int vowelp(char c);
int letterp(char c);
int illegal(char c);      /* Tests if c is legal. */

I also forgot to remove the display.h as it is no longer necessary. So if you want to compile it, just delete it.

Edited 6 Years Ago by tquiva: n/a

I'm sorry, i've got problems here and i can't follow your functions. where did these functions come from?

you've got these libraries tfdef.h and chrutil.h with all these functions all over the place i have no access to. i've never seen them before. i could cipher it out with the full prototypes, but that would be ridiculously time consuming.

If you're forced to use these functions, im afraid im not going to be much help.

otherwise i would want your program requirements and rewrite it in standard C.


EDIT: sorry, but the header doesn't tell me very much. i cant use the header file on it's own, i'd have to have pre-compiled libraries linked in my environment. I don't have any way to compile this.

Edited 6 Years Ago by jephthah: n/a

just going on Google searches, these functions appears to be some specialized libraries tfdef.h and chrutils.h peculiar to a professor at the University of Hawaii.

Is your assignment requiring you to use all of these functions?

This is the main driver:

/* File: driver4.c */
/* This driver will test out the function put_result() */


#include <stdio.h>
#include "presults.h"
#define DEBUG

main()
{
 /* Declare variables */
 int integer;   /* Integer prompted at input */
 int input;     /* Reads input */

        /* Prompt user for input */
        printf("Enter integer: ");

        /* While there are more integers at input */
        while( scanf("%d", &integer) != EOF)
        {
         /* Call function to print digit */
         put_result(integer);

         /* Print a blank line */
         printf("\n");

         /* Update loop */
         printf("Enter integer: ");

        }
}

This is the function put_result and it's header:

/* File: presults.c */

#include "presults.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;
}
/* File: presults.h */

/*  This file contains prototypes for presults.c  */

#define BASE    10

void put_result(int ans);
/*  This function is given an integer and prints it to the dispaly
        one digit at a time.
*/

int sig_dig_value(int n, int wt);
/*  This function is given an integer and the current weight.  It
        returns the integer value of the most significant digit.
*/

int supress_msd(int n, int wt);
/*  This function is given an integer and the current weight.  It
        returns the integer with the most significant digit removed.
*/

int weight(int n);
/*  This function is given an integer.  It returns the weight (a power
        of 10) of the most significant digit.
*/

And this is the character utility function with it's header:

/* File: chrutil.c */
/*   This file contains various utility functions for processing characters  */

#include <stdio.h>
#include "tfdef.h"
#include "chrutil.h"

/*   Function converts ch to an integer if it is a digit. Otherwise, it
     prints an error message.
*/
int dig_to_int(char ch)
{
     if (IS_DIGIT(ch))
          return ch - '0';
     printf("ERROR:dig_to_int:  %c is not a digit\n", ch);
     return ERROR;
}

/*   Function converts a positive integer less than 10 to a corresponding
     digit character.
*/
char int_to_dig(int n)
{
     if (n >= 0 && n < 10)
          return n + '0';
     printf("ERROR:int_to_dig:  %d is not in the range 0 to 9\n", n);
     return (char)NULL;
}



/*  Function reads the next integer from the input  */
int getint()
{    int n = 0;
     int got_dig = FALSE;
     signed char ch;


     ch = getchar();                     /* read next char                  */
     while (IS_WHITE_SPACE(ch))          /* skip white space                */
              ch = getchar();
     while (IS_DIGIT(ch)) {              /* repeat as long as ch is a digit */
          n = n * 10 + dig_to_int(ch);   /* accumulate value in n           */
          got_dig = TRUE;
#ifdef DEBUG
printf("debug:getint: ch = %c\n", ch);   /* debug statement */
printf("debug:getint: n = %d\n", n);     /* debug statement */
#endif
          ch = getchar();                /* read next char                  */
     }
     if(ch == EOF) return EOF;           /* test for end of file            */
     if(!got_dig)  return ERROR;         /* test for no digits read         */
     return n;                           /* otherwise return the result     */

}

/* Function tests if c is an alphabetic letter. */
int letterp(char c)
{
     if (IS_LOWER(c) || IS_UPPER(c))
          return TRUE;
     return FALSE;
}


/*   Function returns TRUE if c is a delimiter, i.e., it is a white space
     or a punctuation. Otherwise, it returns FALSE.
*/
int delimitp(char c)
{
     if (whitep(c) || punctp(c))
          return TRUE;
     return FALSE;
}

/* Function returns TRUE if c is white space; returns FALSE otherwise. */
int whitep(char c)
{
     if (c == '\n' || c == '\t' || c == ' ')
          return TRUE;
     return FALSE;
}

/* Function returns TRUE if c is a punctuation; returns FALSE otherwise. */
int punctp(char c)
{
     if (c == '.' || c == ',' || c == ';' || c == ':'
               || c == '?' || c == '!')
          return TRUE;
     return FALSE;
}


/*   Function checks if c is a vowel. */
int vowelp(char c)
{
     switch(c) {
          case 'a':
          case 'A':
          case 'e':
          case 'E':
          case 'i':
          case 'I':
          case 'o':
          case 'O':
          case 'u':
          case 'U':  return TRUE;
          default:   return FALSE;
     }
}

/* Function tests if c is printable. */
int illegal(char c)
{
   if (IS_PRINT(c) || IS_WHITE_SPACE(c))
     return FALSE;
   return TRUE;
}
/* File: chrutil.h */
/*   This file contains various  macros and prototypes for character processing */

#define   ERROR     -2

#define   IS_DIGIT(c)   ((c) >= '0' && (c) <= '9')
#define   IS_LOWER(c)   ((c) >= 'a' && (c) <= 'z')
#define   IS_UPPER(c)   ((c) >= 'A' && (c) <= 'Z')
#define   IS_WHITE_SPACE(c)   ((c) == ' ' || (c) == '\t' || (c) == '\n')
#define   IS_PRINT(c)   ((c) >= 32 && (c) < 127)

#define  LOWER     0
#define  UPPER     1
#define  DIGIT     2
#define  PUNCT     3
#define  SPACE     4
#define  CONTROL   5
#define  SPECIAL   6


int dig_to_int(char ch);
char int_to_dig(int n);
char uppercase(char ch);
int getint();

int delimitp(char c);
int whitep(char c);
int punctp(char c);
int vowelp(char c);
int letterp(char c);
int illegal(char c);      /* Tests if c is legal. */


And this is tfdef.h:

/* File: tfdef.h */
#define TRUE 1
#define FALSE 0

Edited 6 Years Ago by tquiva: n/a

If I enter 100 ... I expect for suppress_msd to return 00 and for the weight to be 10.

this is totally off the cuff, and i havent tried to compile it, but this is the basic idea of what you're trying to do. I don't understand why it has to be so complicated.

int numVal, valid;
char weight[3];                         // for two digits
char numStr[16], suppress_msd[16];     // oversized

/* basic input minimal error checking */
do
{
    valid = 0;
    printf("Enter number: ");
    fflush(stdout);
    fgets (numStr, sizeof(numStr), stdin);
    numVal = strtol(numStr, &ptr, 10);

    /* remove trailing newline from string */
    if (numStr[strlen(numStr)-1] == '\n')
    {
        numStr[strlen(numStr)-1] = 0;
        valid = 1;
    }
}
while(numStr == ptr || valid == 0;);
/* continue to get input if strtol didnt convert an int (ptr == string)
or if the valid flag is not set ... */


/* suppress msd and get weight, whatever this means */

strcpy( weight, numStr[0]);           // the msd, as string

strcpy( suppress_msd, &numStr[1]);  // all the rest, as string


/* you want your weight (apparently) multiplied by 10 */

strcat(weight,'0');

printf("\nYour number was %d.\n\"weight\" is %s, \"suppress msd\" is %s\n\n", numVal, weight, suppress_msd);

EDIT: changed results to be strings, not ints.

EDIT 2 : fixed exit condition on the do/while loop


.

Edited 6 Years Ago by jephthah: n/a

this is totally off the cuff, and i havent tried to compile it, but this is the basic idea of what you're tyring to do. I don't understand why it has to be so complicated.

char weight[3];                         // for two digits
char numstr[16], suppress_msd[16];     // oversized

/* basic input minimal error checking */
do
{
    valid = 0;
    printf("Enter number: ");
    fflush(stdout);
    fgets (numStr, sizeof(numStr), stdin);
    numVal = strtol(numStr, &ptr, 10);

    /* remove trailing newline from string */

    if (numStr[strlen(numStr)-1] == '\n')
    {
        numStr[strlen(numStr)-1] = 0;
        valid = 0;
    }
}
while(numStr == ptr && valid == 1;);




/* suppress msd and get weight, whatever this means */

strcpy( weight, numStr[0]);           // the msd, as string

strcpy( suppress_msd, &numStr[1]);  // all the rest, as string




/* you want your weight (apparently) multiplied by 10 */

strcat(weight,'0');

printf("\nYour number was %d.\n\"weight\" is %s. \"suppress msd\" is %s\n\n", numVal, weight, suppress_msd);

thank a lot man!
wait, so this goes into driver4.c or in presults.c ?

thank a lot man!
wait, so this goes into driver4.c or in presults.c ?

hell if I know! LOL :)

i don't know if it does exactly what you want to do. modify it for your own purposes.

Edited 6 Years Ago by jephthah: n/a

Hey, by the way, i just found at least one error in the code that you replied to above. use this fixed version.

int numVal, valid;
char weight[3];                        // for two digits
char numStr[16], suppress_msd[16];     // oversized
char *ptr;                             // for the strtol() function

/* basic input minimal error checking */
do
{
    valid = 0;
    printf("Enter number: ");
    fflush(stdout);
    fgets (numStr, sizeof(numStr), stdin);
    numVal = strtol(numStr, &ptr, 10);

    /* remove trailing newline from string */
    if (numStr[strlen(numStr)-1] == '\n')
    {
        numStr[strlen(numStr)-1] = 0;
        valid = 1;
    }
}
while(numStr == ptr || valid == 0;);
/* continue to get input if strtol didnt convert an int (ptr == string)
or if the valid flag is not set ... */


/* suppress msd and get weight, whatever this means */

strcpy( weight, numStr[0]);           // the msd, as string

strcpy( suppress_msd, &numStr[1]);  // all the rest, as string


/* you want your weight (apparently) multiplied by 10 */

strcat(weight,'0');

printf("\nYour number was %d.\n\"weight\" is %s, \"suppress msd\" is %s\n\n", numVal, weight, suppress_msd);

EDIT: changed results to be strings, not ints.

EDIT 2 : fixed exit condition on the do/while loop

EDIT 3 : added *ptr; declaration

Edited 6 Years Ago by jephthah: n/a

I haven't learned fflush, fgets, strtol, strcpy yet. These are all new things. Is there a simpler way to do this?

I haven't learned fflush, fgets, strtol, strcpy yet. These are all new things. Is there a simpler way to do this?

you should learn them. but yeah, yeah, i know....

so use your original scanf input method.

but you definitely need to know 'sprintf' and you need to know 'strcpy'. any simpler than that, gets complicated. i'm not going there. jsut look them up

int integer, valid;
char weight[3];                        // for two digits
char numStr[16], suppress_msd[16];     // oversized
char *ptr;                             // for the strtol() function



///////////////////////////////////////////////////
///////////////////////////////////////////////////
////     HERE IS YOUR ORIGINAL INPUT ROUTINE   ////
////                                           ////

/* Prompt user for input */
printf("Enter integer: ");

/* While there are more integers at input */
while( scanf("%d", &integer) != EOF)
{
	 /* Print a blank line */
	 printf("\n");

	 /* Update loop */
	 printf("Enter integer: ");
}

////     I DO NOT RECOMMEND THIS METHOD        ////
////                                           ////
///////////////////////////////////////////////////
///////////////////////////////////////////////////


/* convert integert to string */
sprintf(numStr,"%d",integer);


/* suppress msd and get weight, whatever this means */
strcpy( weight, numStr[0]);           // the msd, as string
strcpy( suppress_msd, &numStr[1]);  // all the rest, as string


/* you want your weight (apparently) multiplied by 10 */
strcat(weight,'0');


/* it doesnt get any simpler */
printf("\nYour number was %d.\n\"weight\" is %s, \"suppress msd\" is %s\n\n", numVal, weight, suppress_msd);

so I replaced my driver4.c with this code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "presults.h"

main()
{
 /* Declare variables */
 int integer, valid;
 char weight[3];                        // for two digits
 char numStr[16], suppress_msd[16];     // oversized
 char *ptr;                             // for the strtol() function


/* Prompt user for input */
printf("Enter integer: ");

/* While there are more integers at input */
while( scanf("%d", &integer) != EOF)
  {
         /* Print a blank line */
         printf("\n");

         /* Update loop */
         printf("Enter integer: ");
  }

/* convert integert to string */
sprintf(numStr,"%d",integer);


/* suppress msd and get weight, whatever this means */
strcpy( weight, numStr[0]);           // the msd, as string
strcpy( suppress_msd, &numStr[1]);  // all the rest, as string


/* you want your weight (apparently) multiplied by 10 */
strcat(weight,'0');


/* it doesnt get any simpler */
printf("\nYour number was %d.\n\"weight\" is %s, \"suppress msd\" is %s\n\n", numVal, weight, suppress_msd);

}

and I get this error when compiled:
test5.c:34: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast
/usr/include/string.h:127: note: expected âconst char * __restrict__â but argument is of type âcharâ
test5.c:39: warning: passing argument 2 of âstrcatâ makes pointer from integer without a cast
/usr/include/string.h:135: note: expected âconst char * __restrict__â but argument is of type âintâ
test5.c:43: error: ânumValâ undeclared (first use in this function)
test5.c:43: error: (Each undeclared identifier is reported only once
test5.c:43: error: for each function it appears in.)

Since I am not familiar with strings, could you please help me out some more with this?

By the way, I noticed you took out my function put_result().
My task was to make this function work. In the way you wrote that program, do I not need that function anymore?

sorry for the confusion.. here is the fixed version of my wild attempt at this.

Now I'm sure this isn't anything like what you need for your assignment, but it's just an example of how you could approach this without using the non-standard library functions.

#include <stdio.h>
#include <string.h
#include <stdlib.h>
//#include "presults.h"

main()
{
/* Declare variables */
int integer, valid;
char weight[3];                        // for two digits
char numStr[16], suppress_msd[16];     // oversized
char *ptr;                             // for the strtol() function


/* Prompt user for input */
printf("Enter integer: ");

/* While there are more integers at input */

while( scanf("%d", &integer) <= 0)   // <--- NOTE, DON'T USE EOF!
{
     /* Print a blank line */
     printf("\n");

     /* Update loop */
     printf("Enter integer: ");
}

/* convert integert to string */
sprintf(numStr,"%d",integer);


/* suppress msd and get weight, whatever this means */
weight[0] = numStr[0];      // the single msd character, 
                            // copy to start of 'weight' string
                            
strcpy(suppress_msd, &numStr[1]);  // copy the rest of number 
                                   // to the 'suppresss_msd' string


// you want your weight (apparently) multiplied by 10 
// so add another zero character '0' to the end.   then add a null 
// character after that to terminate the array and form a string.

weight[1] = '0';   // note the difference between the zero '0' (ASCII 0x30)
weight[2] = '\0';  // and the NULL '\0' (ASCII 0x00) characters


/* it doesnt get any simpler */
printf("\nYour number was %d.\n\"weight\" is %s, \"suppress msd\" is %s\n\n", integer, weight, suppress_msd);

}

and now that i've looked at your functions up there a bit more, i see that it's not really that hard to figure out. '

And the fact is, in the workplace we're faced with non-standard in-house libraries, specialized libraries for 3rd party proprietary technology, and (worst of all) legacy code that usually doesn't make any sense at all.

so i need to calm down and I'll see if i can take another look at your functions.

Edited 6 Years Ago by jephthah: n/a

try this, this does the weight according to your function...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include "presults.h"

main()
{
    /* Declare variables */
    int integer, valid, i;
    char weight[16], numStr[16], suppress_msd[16];  // oversized

    /* Prompt user for input */
    printf("Enter integer: ");

    /* While there are more integers at input */

    while( scanf("%d", &integer) <= 0)   // <--- NOTE, DON'T USE EOF!
    {
         /* Print a blank line */
         printf("\n");

         /* Update loop */
         printf("Enter integer: ");
    }

    /* convert integert to string */
    sprintf(numStr,"%d",integer);


    /* suppress msd and get weight, whatever this means */
    weight[0] = numStr[0];      // the single msd character, 
                                // copy to start of 'weight' string

    // get the weight which is the msd multiplied by the base power of 10.
    // then add a NULL character at the end to form a string

    for (i=1; i < strlen(numStr); i++)
        weight[i] = '0';   // note the difference between the zero '0' (ASCII 0x30)

    weight[i] = '\0';  // and the NULL '\0' (ASCII 0x00) characters

                                
    strcpy(suppress_msd, &numStr[1]);  // copy the rest of number 
                                       // to the 'suppresss_msd' string

    printf("\nYour number was %d.\n\"weight\" is %s, \"suppress msd\" is %s\n\n", 
                                                     integer, weight, suppress_msd);

}

The problem is that it is not reading in any zero's. I manually traced the program and discovered that the bug lies in this section of presults.c

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);
        }

Okay, dude.....

I AM REALLY REALLY SORRY for hijacking your thread to go off on some ranting tangent that has been mostly unhelpful

really, I am sorry. I don't know WTF my problem was. Your code, once i got over the fact that it uses some non-standard libraries (horrors!) , is actually very clearly written, very nicely formatted, and very easy to understand

your problem is both simple and sneaky.

the fact is that when you have a zero in a power of 10's place, the modulus does not care that it's there.

so 305 only finds the 3 and the 5.

if you use integer division like you're doing to find the Weight, you will always have this problem.

the good news is, you really only need to call the weight function once, to get the highest power of 10. every power for the remaining digits is just one less power of 10. you dont need to calculate it, just decrement each time after the initial calculation:

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 /= BASE;                      // <-- here is the fix.
        }

now i haven't compiled your code so i havent verified it works, but i believe that it will. let me know.

and once again sorry for going of on some zealous rampage. i think i need my meds checked :$

it's all good man. don't worry about it.

I compiled with your correction and the problem is solved. Thank you so much for the help, really appreciate it!

This question has already been answered. Start a new discussion instead.