尽管最后一个函数不是循环的一部分,程序仍然停留在循环周期中

时间:2014-11-20 14:52:52

标签: c loops

好吧所以我对C比较陌生,基本上我已经编写了一个程序,让用户通过4轮网球锦标赛。该程序完美运行直到最后一个功能(FR),在我输入中奖号码后,程序直接切换回R1,这怎么可能?如何在最后一轮结束后让程序结束?

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

#define R1CONTESTANTS 16
#define R2CONTESTANTS 8
#define R3CONTESTANTS 4
#define FRCONTESTANTS 2
#define WINNER 1


int main(void)
{
  GetNames();
  R1();
  R2();
  R3();
  FR();  
  return 0;
}

int GetNames()
{
  char Name[R1CONTESTANTS][20];
  int n;

  printf("Hello and Welcome to the 2014 Tennis Knockout Competition!\n");
  printf("Please enter the names of all 16 players (MAX 20 CHARACTERS): \n");
  /*Get Names loop*/
    for(n = 0; n < 16 ; n++)
    {
      printf("%d >>", n+1);
      scanf("%20s", Name[n]);
      fflush(stdin); 
    }
  R1(Name);
}

int R1(char Name[][20])
{
  char R2Name[R2CONTESTANTS][20];
  int m, i;
  int Winner[8];

  system("clear");
  printf("WELCOME TO ROUND 1! IT WILL CONSIST OF 8 MATCHES! GOOD LUCK!");
  for(m = 0, i = 0; m < 8 ; m++, i+=2)
        {
          printf("\nMatch %d of 8.\n", m+1);
          printf("\n(1) %s v %s (2)\n", Name[i], Name[i+1]);
          printf("\nWinner of Match %d(1/2): ", m+1);
          scanf("%d", &Winner[m]);
          fflush(stdin);
          switch(Winner[m])
          {
            case 1 : strncpy(R2Name[m], Name[i], 20);break;
            case 2 : strncpy(R2Name[m], Name[i+1], 20);break;
            default : printf("\nSomething went wrong. Please try again.\n");break;
          }
        }
  R2(R2Name);
}

int R2(char R2Name[][20])
{
  char R3Name[R3CONTESTANTS][20];
  int m, i;
  int R2Winner[4];

  system("clear");
  printf("WELCOME TO ROUND 2! IT WILL CONSIST OF 4 MATCHES! GOOD LUCK!");
  for(m = 0, i = 0; m < 4; m++, i+=2)
      {
        printf("\nMatch %d of 4.\n", m+1);
        printf("\n(1) %s v %s (2)\n", R2Name[i], R2Name[i+1]);
        printf("\nWinner of Match %d(1/2): ", m+1);
        scanf("%d", &R2Winner[m]);
        fflush(stdin);
        switch(R2Winner[m])
          {
            case 1 : strncpy(R3Name[m], R2Name[i], 20);break;
            case 2 : strncpy(R3Name[m], R2Name[i+1], 20);break;
            default : printf("\nSomething went wrong. Please try again.\n");break;
          }
       }
  R3(R3Name);
}

int R3(char R3Name[][20])
{
  char FRName[FRCONTESTANTS][20];
  int m, i;
  int R3Winner[2];

  system("clear");
  printf("WELCOME TO THE SEMI FINALS! GOOD LUCK!");
  for(m = 0, i = 0; m < 2; m++, i+=2)
         {
            printf("\nMatch %d of 2.\n", m+1);
            printf("\n(1) %s v %s (2)\n", R3Name[i], R3Name[i+1]);
            printf("\nWinner of Match %d(1/2): ", m+1);
            scanf("%d", &R3Winner[m]);
            fflush(stdin);
            switch(R3Winner[m])
            {
              case 1 : strncpy(FRName[m], R3Name[i], 20);break;
              case 2 : strncpy(FRName[m], R3Name[i+1], 20);break;
              default : printf("\nSomething went wrong. Please try again.\n");break;
            }
         }
  FR(FRName);
}

int FR(char FRName[][20])
{
  int FRWinner;
  char OWName[WINNER][20];

  system("clear");
  printf("YOU HAVE MADE IT TO THE FINAL ROUND! GOOD LUCK!");
  printf("\n\n(1) %s v %s (2)\n", FRName[0], FRName[1]);
  printf("\nWinner of Match and Tournament(1/2): ");
  scanf("%d", &FRWinner);
  fflush(stdin);
  switch(FRWinner)
  {
    case 1 : strncpy(OWName[0], FRName[0], 20);break;
    case 2 : strncpy(OWName[0], FRName[1], 20);break;
    default : printf("Something went wrong.");break;
  }

}

3 个答案:

答案 0 :(得分:0)

您从R1致电GetNames,然后再从main致电。您有每个步骤调用其后继者,以及main调用每个步骤。

答案 1 :(得分:0)

你不需要在main中调用R1(),R2()等...因为R1在GetNames的末尾被调用,R2在R1的末尾被调用,依此类推。 / p>

答案 2 :(得分:0)

您的代码有点搞笑,您似乎将这些方法链接在一起,并且还按照主要顺序调用它们。

所以你当前的节目流程是。

 GetNames() -> R1() -> R2() -> R3() -> FR()
 R1() -> R2() -> R3() -> FR()
 R2() -> R3() -> FR()
 R3() -> FR()
 FR()

然后有关于不传递参数的全部协议,如果编译器拒绝编译混乱或者它只是立即崩溃,这将是一个快乐的事故。

现在你有一些似乎在你手上的东西,危险的东西。