gpt4 book ai didi

抛硬币程序无法运行 Visual Studio 2010

转载 作者:行者123 更新时间:2023-11-30 17:50:19 25 4
gpt4 key购买 nike

一本书的练习促使我创建一个模拟抛硬币的程序。我的 friend 说他在 native 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)
<小时/>

感谢您的回复。

最佳答案

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

int result;

int flip();

int main(void){
int heads = 0;
int tails = 0;
unsigned counter;

srand(time(NULL));//Executable statement after the declaration

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);
return 0;
}

int flip() {
result = 1 + rand() % 2;

return result == 1;
}

关于抛硬币程序无法运行 Visual Studio 2010,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17335278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com