【发布时间】:2013-06-27 05:38:40
【问题描述】:
一本书的练习提示我创建一个模拟抛硬币的程序。我的朋友说他在本机 GNU 编译器中运行了我的代码,并且它可以工作,但是当我尝试在 Visual Studio 2010 中运行它时收到以下错误:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int result;
int flip();
int main(void)
{
srand(time(NULL));
int heads = 0;
int tails = 0;
unsigned counter;
for(counter = 1; counter <= 100; counter++)
{
result = flip();
if(result == 1)
{
printf("Heads\n");
heads++;
}
else
{
printf("Tails\n");
tails++;
}
}
printf("Heads: %d\tTails: %d\n", heads, tails);
}
int flip()
{
result = 1 + rand() % 2;
if (result == 1)
return 1;
if (result == 2)
return 0;
return NULL;
}
syntax error: ')' (line 10)
'counter': undeclared identifier (15, 23)
'heads': undeclared identifier (19, 23)
't': undeclared identifier (10, 10)
syntax error: missing ')' before 'type' (line 10)
syntax error: missing ';' before '{' (line 11)
syntax error: missing ';' before 'type' (9, 10, 10, 10)
感谢您的回复。
【问题讨论】:
-
如果您正在编译 .c 文件,也许 [此链接][1] 可以帮助您解决问题。 [1]:stackoverflow.com/questions/2706453/…
-
将该 srand 语句拉到类型声明语句下方。 C 不允许在任何地方使用类型声明语句。
-
@ram C 不允许在任何地方进行类型声明是什么意思?顺便说一句,声明是正确的,否则它不会在 gcc 中工作。
-
这是完整的代码吗?因为,我在程序的任何地方都看不到错误消息中提到的“t”,所以尝试使用调试器。
-
尝试使用选项 -pedantic 编译代码。这将在 GCC 中向您发出警告。 “警告:ISO C90 禁止混合声明和代码”。 VC 不允许此警告通过。
标签: c visual-studio-2010 coin-flipping