我开始学习 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
大家能帮我看看是什么问题吗?