-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 回答