【问题标题】:Coin Toss program not working Visual Studio 2010抛硬币程序不工作 Visual Studio 2010
【发布时间】: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


【解决方案1】:
#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;
}

【讨论】:

  • 效果最好。谢谢!
【解决方案2】:

这是一个修改后的代码,我在 eclipse 中运行良好:

#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);

        return 0;
}

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

    if (result == 1)
        return 1;

    if (result == 2)
        return 0;
}

错误: 在 main 中,您忘记了“return 0”语句。 在翻转函数中,您编写了不应该存在的“返回 NULL”。在 vs 中试试这段代码。

【讨论】:

  • 使用选项-pedantic 编译代码时会收到警告。 GCC 可能会忽略这个警告。但不是 VC。
  • flip() 中,if 后面必须有一个else 的情况,而不是两个if。因为只有在if 条件中的任何一个为真时,函数才会返回,它赢了'不返回任何其他情况。
【解决方案3】:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

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

srand(time(NULL));

for(counter = 1; counter <= 100; counter++){
    result = rand()%2;

    if(result == 1){         
        heads++;
    }
    else{
        tails++;
    }
}
printf("Heads: %d\tTails: %d\n", heads, tails);     
return 0;
}

【讨论】:

  • 该代码还包含一些逻辑错误。您应该编写整个代码。
  • 我觉得最根本的问题是 srand() 函数在类型声明语句之上被调用。所有类型声明语句都必须在块的开头。
猜你喜欢
  • 2014-05-25
  • 1970-01-01
  • 1970-01-01
  • 2011-05-25
  • 2020-07-12
  • 1970-01-01
  • 2011-07-31
  • 2011-06-17
  • 2012-11-17
相关资源
最近更新 更多