0

我已经单独测试了每个部分,我知道它们可以工作,问题是当我将函数 roll_dice 粘贴回或虎钳 verse 时。当我将两者编译在一起时,我得到错误 C2143 missing ';' 在第 32 行的“type”之前,它是 play_game 函数。有人可以告诉我为什么我在他们分开工作时会出现这个错误,但当他们放在一起时却不工作。这是一个掷骰子游戏,我是使用 c 的初学者。

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

#define MAX_DIE 6
#define true 1
#define false 0

int num1 = 0;
int num2 = 0;
int roll = 0;
int point = 0;
int wins = 0;
int losses = 0;
int keep_rolling = 1;

int main(void)
{

  void roll_dice(int num1, int num2, int roll);
  {
    srand(time(NULL ));
    num1 = ("%d", rand() % MAX_DIE + 1);
    num2 = ("%d", rand() % MAX_DIE + 1);
    roll = (num1 + num2);
    printf("this is your number : %d\n", num1);
    printf("this is your 2nd number : %d\n", num2);
    printf("this is your total : %d\n", roll);
  }

  void play_game(int wins, int losses, int point, int roll);
  {

    if ((roll == 7) || (roll == 11))
    {
      printf("you rolled %d you won \n", roll);
      wins += 1;
    }
    else if ((roll == 2) || (roll == 3) || (roll == 12))
    {
      printf("you rolled %d you lost \n", roll);
      losses += 1;
    }
    else if ((roll == 1) || (roll == 4) || (roll == 5) || (roll == 6)
        || (roll == 8) || (roll == 9) || (roll == 10))
    {
      printf("you have pointed : %d\n", roll);
      point = roll;
      printf("you rolled %d you pointed \n", roll);

      while (keep_rolling = 1)
      {
        void roll_dice(int num1, int num2, int roll);

        if (roll == point)
        {
          printf("you rolled %d you won \n", roll);
          wins += 1;
          return keep_rolling = false;
        }
        else if (roll == 7)
        {
          printf("you rolled %d you lost \n", roll);
          losses += 1;
          return keep_rolling = false;
        }
        else
        {
          printf("you rolled : %d\n", roll);
          printf("your point is : %d\n", point);
        }
      }

    }

  }
}  
4

2 回答 2

0
void roll_dice(int num1, int num2, int roll);

您不能在函数内部定义函数。在上述情况下它编译得很好,因为它是一个声明。

于 2013-09-27T19:33:09.653 回答
0

定义functions外部main()函数并调用函数main()

你不应该在里面定义函数main()

您在它们的同时添加了分号defining。去掉最后的分号。

还有declare上面的功能main()

此处此处读取函数

于 2013-09-27T19:33:11.530 回答