一个书本练习提示我创建一个模拟抛硬币的程序。我的朋友说他在本机 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)
感谢您的任何回复。