-2

私はC++を学び始めており、演習として簡単なゲームをコーディングしています。私のロジックはほぼ完成していると思いますが、gccが私のコードについて不平を言っている理由がわかりません。

これは私のコードです:

#include <stdio.h>

void readGuess(int[4]);
int blackScore(int[4], int[4]);
int anotherGame(void);
int whiteScore(int[4],int[4]);
void printScore(int[4],int[4]);

int main(void){
    int codes[5][4] = {{1,8,9,2}, {2,4,6,8}, {1,9,8,3}, {7,4,2,1}, {4,6,8,9}};

    int i;
    for (i=0; i<5; i++){
        int guess[4];
        readGuess(guess);
        while(blackScore(guess, codes[i]) != 4) {
            printScore(guess, codes[i]);
            readGuess(guess);
        }

        printf("You have guessed correctly!!");
        if(i<4){
            int another = anotherGame();
            if(!another)
                break;
        }
    }




    return 0;
}
void readGuess(int[4] guess) {
    scanf("%d %d %d %d",&guess,&guess+1,&guess+2,&guess+3);
}

int blackScore(int[4] guess, int[4] code){
    int score = 0;
    int i;
    for(i = 0; i<4;++i){
        if(code[i]==guess[i]){
            score++;
        }
    }
    return score;
}

int whiteScore(int[] guess, int[] code){
    int score = 0;
    int i;
    for(i = 0; i<4;++i){
        int j;
        for(j = 0; j<4;++j){
            if(i!=j && (code[i] == guess[i])){
                score++;
            }
        }
    }
    return score;
}

void printScore(int[4] guess, int[4] code){
    printf("(%d,%d)\n",blackScore(guess,code),whiteScore(guess,code));
}

int anotherGame(void){
    while(1){
        printf("Would you like to play another game? [Y/N]\n");
        char result;
        result = getchar();
        if(result == 'Y')
            return 1;
        else if (result == 'N')
            return 0;
    }
}

そして、これらはエラーです:

testSource.c:34:23: error: expected ‘;’, ‘,’ or ‘)’ before ‘guess’
testSource.c:38:23: error: expected ‘;’, ‘,’ or ‘)’ before ‘guess’
testSource.c:49:22: error: expected ‘;’, ‘,’ or ‘)’ before ‘guess’
testSource.c:63:24: error: expected ‘;’, ‘,’ or ‘)’ before ‘guess’
testSource.c: In function ‘anotherGame’:
testSource.c:70:3: warning: ISO C90 forbids mixed declarations and code

問題が何であるかを理解するのを手伝ってくれませんか?

4

3 に答える 3

1

に変更int[4] guessint *guessます。

于 2012-05-23T11:00:28.907 に答える
1

パラメータは配列のサイズを宣言するべきではありません。int guess[]パラメータの代わりに使用int[4] guessしてください。

また、GCCのスイッチを使用しない限りresult、上部で宣言する必要があります。anotherGame-c99

于 2012-05-23T11:02:11.703 に答える
0

あなたはあなたの引数宣言をJavaと混同していると思います:

void readGuess(int[4] guess) {

そのはず

void readGuess(int guess[]) {

他の機能でも同じ問題があります。

また、プロトタイプは、たとえば、実際の機能と常に一致するとは限りませんwhiteScore

于 2012-05-23T11:01:40.317 に答える