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