2016-07-20 13 views
2

私は、整数の配列を取り、ベース16などの目的のベースに変換できるコードを書いています。可変長配列が変換されないのはなぜですか?

何らかの理由で、端末はプログラムを介して、その方法を行い、ここで

"変換数="

を出力私のコードです:あなたは

#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; 
} 
+2

'無効convertNumber(int型のサイズ、int配列[サイズ]);'あなたは、一貫してあなたのコードをフォーマットしてください機能 –

+0

を_call_どのようにそれはないですが、それは読みにくいです。ありがとう – user3078414

+0

代わりに、 '(void)convertNumber(int size、int array [size]);' – babon

答えて

5

コード、convertNumber()関数を呼び出したことはありません。あなたは電話をかけるために

convertNumber (size, array); 

void convertNumber(int size, int array[size]); 

からgetNumberAndBase()機能の最後の部分を変更する必要があります。あなたはmain()convertedNumberbasedigitを定義し、(呼び出された関数の内部でそれを利用することができるようにする)関数の引数の一部として渡す必要がある、と述べた

。彼らが一般的にグローバルになる理由はありません。

また、関数宣言をmain()から移動します。それらをファイルスコープに入れます。

+0

また、 'main()'の中の 'convertedNumber'、' base'、 'digit'の宣言を動かし、それらをパラメータとして渡すのもよいでしょう。範囲がグローバルでなければならない理由はありません。 (グローバルの使用が必要な場合がありますが、一般的には別の方法で回避する必要があります) –

+0

@ DavidC.Rankinはい、私はグローバルを見逃していました。 –

1

コードにはいくつかのバグがあります。可能な限り変更されていないコードの作業バージョンがあります(私はそれをきれいにする衝動に抵抗しました)。私は、私が行った変更にコメントを追加しました。

#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; 
} 
関連する問題