2

我用 C 语言做了一个数字猜谜游戏,可以用 GCC 4.7.1 很好地编译。但它给VC2010带来了很多错误。主要是语法错误问题。我不知道为什么它会出现这些错误,因为我已经使用 VC2010 编译了许多其他 C 源代码而没有这些错误。

我使用 VC(命令行)使用以下命令编译它:cl guessgame.c

VC 给出以下错误:

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for
Copyright (C) Microsoft Corporation.  All rights reserved.

guessgame.c
guessgame.c(8) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(9) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(10) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(11) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(12) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(13) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(14) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(20) : error C2065: 'chances' : undeclared identifier
guessgame.c(21) : error C2065: 'randRangeX' : undeclared identifier
guessgame.c(21) : error C2065: 'randRangeY' : undeclared identifier
guessgame.c(23) : error C2065: 'i' : undeclared identifier
guessgame.c(23) : error C2065: 'chances' : undeclared identifier
guessgame.c(23) : error C2065: 'i' : undeclared identifier
guessgame.c(23) : error C2065: 'i' : undeclared identifier
guessgame.c(25) : error C2065: 'chances' : undeclared identifier
guessgame.c(25) : error C2065: 'i' : undeclared identifier
guessgame.c(26) : error C2065: 'userAns' : undeclared identifier
guessgame.c(28) : error C2065: 'userAns' : undeclared identifier
guessgame.c(28) : error C2065: 'randomNumber' : undeclared identifier
guessgame.c(30) : error C2065: 'chances' : undeclared identifier
guessgame.c(30) : error C2065: 'i' : undeclared identifier
guessgame.c(36) : error C2065: 'wrong_guesses' : undeclared identifier
guessgame.c(39) : error C2065: 'wrong_guesses' : undeclared identifier
guessgame.c(41) : error C2065: 'randomNumber' : undeclared identifier

这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    srand(time(NULL));
    int randomNumber = (rand() % 245)+5;
    int userAns;
    int randRangeX = randomNumber-((rand() % 3)+5);
    int randRangeY = randomNumber+((rand() % 3)+5);
    int chances = 3;
    int wrong_guesses = 0;
    int i;

    printf("Number guessing game v1.1.\n");
    printf("Copyright(c) 2013 - Ahnaf Tahmid.\n");
    printf("-----------------------------------\n\n");
    printf("Rules: 1) Guess the number.\n");
    printf("       2) You have %d chances.\n\n", chances);
    printf("The number is between %d and %d.", randRangeX, randRangeY);

    for(i = chances; i >= 1; i--)
    {
        printf("\nGuess %d: ", (chances-i)+1);
        scanf("%d", &userAns);
        while(getchar() != '\n');
        if(userAns == randomNumber)
        {
            printf("\nGood guess! You guessed it in %d turn(s).\n", (chances-i)+1);
            break;
        }
        else
        {
            printf("Aww, bad guess.\n");
            wrong_guesses += 1;
        }
    }
    if(wrong_guesses == 3)
    {
        printf("\nGame Over!\n\nThe answer was %d.\n", randomNumber);
    }
    getch();
    return 0;
}
4

3 回答 3

4

VC2010 支持 C89,它要求变量在其作用域块的开头声明。如果srand在变量声明之后移动调用并延迟初始化依赖于rand().

int main()
{
    int randomNumber;
    int userAns;
    int randRangeX;
    int randRangeY;
    int chances = 3;
    int wrong_guesses = 0;
    int i;
    srand(time(NULL));
    randomNumber = (rand() % 245)+5;
    randRangeX = randomNumber-((rand() % 3)+5);
    randRangeY = randomNumber+((rand() % 3)+5);
于 2013-08-29T08:10:39.780 回答
2

在 C89 中,所有变量都必须在块的开头声明。这已在 C99 中删除。

但是VC2010不支持C99。所以你必须把

srand(time(NULL));

毕竟声明了变量。


参考:

C89 §3.6.2 复合语句或块

句法

复合语句

{ 声明列表选择 声明列表选择 }

声明列表

宣言

声明列表声明

声明列表

陈述

语句列表语句

请注意,在 C89 中,声明列表必须位于块中的语句列表之前。

C99 §6.8.2 复合语句

句法

复合语句

{块项目列表选择}

块项目列表:

块项目

块项目列表块项目

块项目:

宣言

陈述

在 C99 中,声明和声明可以混合使用。

于 2013-08-29T08:11:35.693 回答
0

解决其他答案指出的问题的另一种方法是打开一个新范围:

int main()
{
  srand(time(NULL));

  {
    int randomNumber = (rand() % 245)+5;
    int userAns;
    int randRangeX = randomNumber-((rand() % 3)+5);
    int randRangeY = randomNumber+((rand() % 3)+5);
    int chances = 3;
    int wrong_guesses = 0;
    int i;
    ...
于 2013-08-29T08:42:46.947 回答