Hey all, i've just a quick question with my getNumbeAndBase() function. The program being executed is not continuing to the convertNumber() function after for example, "Number to be converted: 2001" and a number in valid range between "2 & 16" the loop just asks for another number to be converted but gives the correct response when a number smaller than 2 or larger then 16 is inputted, therefore the correct and working if statement. Please only comment on noted function not rest of the program. This was worked out of a programming book. Thanks.
Code:
// Program to convert a positive integer to another base
#include <stdio.h>
#include <stdlib.h>
int convertedNumber[64];
long int numberToConvert;
int base;
int digit = 0;
void getNumberAndBase (void)
{
do {
printf("Number to be converted? ");
scanf("%li", &numberToConvert);
if(numberToConvert == 0)
{
exit(1);
}
do {
printf("Base? ");
scanf("%i", &base);
{
if(base < 2 || base > 16)
{
printf("Bad base - must be between 2 and 16\n");
}
}
} while (base < 2 || base > 16);
} while (numberToConvert != 0);
}
void convertNumber (void)
{
do {
convertedNumber[digit] = numberToConvert % base;
++digit;
numberToConvert /= base;
}
while (numberToConvert != 0 );
}
void displayConvertedNumber (void)
{
const char baseDigits[16] =
{ '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
int nextDigit;
printf("Converted number = ");
for(--digit; digit >= 0; --digit ) {
nextDigit = convertedNumber[digit];
printf("%c", baseDigits[nextDigit]);
}
printf("\n");
}
int main (void)
{
void getNumberAndBase (void), convertNumber (void), displayConvertedNumber (void);
getNumberAndBase ();
convertNumber ();
displayConvertedNumber ();
return 0;
}