Reputation: 437
I'm writing code that will take in an array of integers which can then be converted into a desired base, such as base-16.
For some reason, the terminal makes its way through the program and prints out
"converted number= "
Here is my code:
#include <stdio.h>
#include <cs50.h>
int convertedNumber[64];
int base;
int digit = 0;
void getNumberAndBase(void) {
int size;
printf("How many numbers to be converted??\n");
size = GetInt();
int array[size];
for (int i = 0; i < size; i++) {
printf("Number to be converted?\n");
array[i] = GetInt();
}
printf("Base?\n");
do {
base = GetInt();
if (base < 2 || base > 16) {
printf("Bad base - must be between 2 and 16. Try again!\n");
}
} while (base < 2 || base > 16);
void convertNumber(int size, int array[size]);
}
void convertNumber(int size, int numberToConvert[size]) {
for (int i = 0; i < size; i++) {
do {
convertedNumber[digit] = numberToConvert[i] % base;
digit++;
numberToConvert[i] /= base;
} while (numberToConvert[i] != 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), displayConvertedNumber(void);
getNumberAndBase();
displayConvertedNumber();
return 0;
}
Upvotes: 2
Views: 93
Reputation: 134346
In your code, you never made a call to convertNumber()
function. You should change the last part of getNumberAndBase()
function from
void convertNumber(int size, int array[size]);
to
convertNumber (size, array);
to make a call.
That said, you should define convertedNumber
, base
and digit
inside main()
and then pass them as some of the arguments to the functions (to be able to make use of it inside called functions). There's no reason for them to be global, in general.
Also, move the function declarations out of main()
; put them in file scope.
Upvotes: 5
Reputation: 3413
You have several bugs in your code. Here's a working version of your code that is as unchanged as possible (I resisted the urge to clean it up). I added comments to the changes I made.
#include <stdio.h>
#include <cs50.h>
/* I moved your prototypes up here rather than leaving them inline. */
void displayConvertedNumber(void);
void convertNumber(int size, int *array);
int convertedNumber[64];
int base;
int digit = 0;
void getNumberAndBase (void)
{
/* I moved your variable declarations here. If you want your C to be
portable, define your variables at the beginning of your function.
Don't expect "int i = 0;" to work in your for loop on all C compilers. */
int size;
int array[size];
int i;
printf("How many numbers to be converted??\n");
size = GetInt();
for(i = 0; i < size; i++){
printf("Number to be converted?\n");
array[i] = GetInt();
}
printf("Base?\n");
do{
base = GetInt();
if(base < 2 || base > 16)
{
printf("Bad base - must be between 2 and 16. Try again!\n");
}
} while(base < 2 || base > 16);
/* I corrected your call to this function. */
convertNumber(size, array);
}
void convertNumber (int size, int numberToConvert[size])
{
int i;
for(i = 0; i < size; i++)
{
do{
convertedNumber[digit] = numberToConvert[i] % base;
digit++;
numberToConvert[i] /= base;
}
while(numberToConvert[i] != 0);
/* I added a call to display the number here. The way
you've written your code means each number has to be
displayed after it is converted. You cannot convert
them all first and then attempt to display them since
you're using a single variable and index (convertedNumber
and digit) for the conversion. */
displayConvertedNumber();
}
}
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");
/* I reset your digit variable here. Otherwise it would have
been left at -1 since that was the exit condition for your
loop above. */
digit = 0;
}
int main (void)
{
getNumberAndBase();
/* I removed the other function call because now everything is
handled in this function. */
return 0;
}
Upvotes: 1