C/addition and putchar
Expert: Zlatko - 7/4/2010
QuestionHi again Zlatko,
Thanks for fixin the issue last time with he reverseString. I got the addition prgram to work the way I wanted (see below) with just using putchar function and NO printf's. But only problem is if I enter a single digi for first value and a 2 digit number for second value, the program would pint only one of the two digits for the second value. Why is that,what was I doing wrong?
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define TRUE 1
int getNumber(void);
int digitCheck(char*);
char* myItoA(int number);
void reverseString(char* input);
int main()
{
int number1;
int number2;
int i;
int j;
int add;
printf("add two numbers\n\n");
number1 = getNumber();
number2 = getNumber();
add = number1 + number2;
putchar('\n');
for(i = 0; myItoA(number1)[i]!='\0'; i++)
{
putchar(myItoA(number1)[i]);
}
putchar(' ');
putchar('+');
putchar(' ');
for(j = 0; myItoA(number1)[j]!='\0'; j++)
{
putchar(myItoA(number2)[j]);
}
putchar(' ');
putchar('=');
putchar(' ');
for(i = 0; myItoA(add)[i]!='\0'; i++)
{
putchar(myItoA(add)[i]);
}
putchar('\n');
return 0;
}
/*
function getNumber
gets a numeric string from the user and returns the integer
representation of the string
*/
int getNumber(void)
{
char input[16];
int number = 0;
int inputLen;
int i;
while (TRUE)
{
printf("Input a number <0-99>: ");
gets(input);
if (digitCheck(input) == 0)
break;
printf("ERROR: invalid number entered. Please Re-Enter.\n\n");
}
inputLen = strlen(input);
for (i = 0; i < inputLen; i++)
{
int digit = input[i] - '0';
number *= 10;
number += digit;
}
return number;
}
/*
function digitCheck
checks that input is all digits and checks that input length is 1 or 2
returns 0 if the input is good
returns 1 if the input is bad
*/
int digitCheck(char* input)
{
int i, inputLen;
inputLen = strlen(input);
if (inputLen< 1 || inputLen>2)
return 1;
for (i = 0; i < inputLen; i++)
{
if (input[i]<'0' || input[i]>'9')
return 1;
}
return 0;
}
/*
function myItoA
takes integer result and
converts to ascii for printing out
*/
char* myItoA(int number)
{
static char array[20];
char character;
int digit;
int counter = 0;
do
{
digit = number % 10;
number /= 10;
character = digit + '0';
array[counter] = character;
counter++;
} while(number != 0);
array[counter] = '\0';
reverseString(array);
return array;
}
/*
function reverseString
takes array from myItoA and corrects output because
it's backwards as is.
*/
void reverseString(char* input)
{
int i, j;
char t;
int len = strlen(input);
i = len / 2;
for(j=0; j < i; j++)
{
t = input[j];
input[j] = input[len-j-1];
input[len-j-1] = t;
}
}
AnswerHello Mike.
This is a bug that happens often. You have copied code from one part of the program into another and in your copy, you did not change all the variables.
for(j = 0; myItoA(number1)[j]!='\0'; j++)
{
putchar(myItoA(number2)[j]);
}
In the loop condition you have myItoA(number1)[j]!='\0, where number1 should be number2, so the loop was running only for the length of the first number.
I should mention that you are making a call to myItoA twice for each iteration of the loop. The first time is in the loop condition and the second time is in the putchar. Remember, the loop condition is evaluated before each entry into the loop body. This is not a big problem in your particular program, but if you want to look professional, you should evaluate the function once, save the result, and then use only the result. Also, if myItoA happened to do something like allocate memory, or open files, or consume other system resources, you could have potential problems. The lines would be better as:
char* result;
result = myItoA(number2);
for(j = 0; result[j]!='\0'; j++)
{
putchar(result[j]);
}
Or the whole loop can be eliminated with:
puts(myItoA(number2));