C从另一个函数输出数组

时间:2015-11-24 09:16:46

标签: c

我正在尝试从我的函数'enter'复制数组获胜者,这样我就可以在'previous'函数上输出它。当选择前一个选项的选项时,我没有输出任何内容。它只是名为'previous'的最后一个函数无法正常工作,但产生问题需要大部分代码。

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

char enter(char names[][20]);
void menu();
void previous(char winner[][8]);

int main()
{
    char names[16][20];
    int i;

    printf("Please enter the names of the players:\n");

        /*Making the user enter 16 times*/
    for (i = 0; i < 16; i++) 
    {   
        scanf("%9s", &names[i]);
        fflush(stdin);
    }
    /*Clearing Screen*/
    system("cls");

    menu(names);
    return names[16][20];

}
void menu(char names[][20], char winner[][8])
{
    int choice;

    printf("Please select one of the following options:\n\n"
            "Press 1 to enter game  results\n"
            "Press 2 to display the current round\n"
            "Press 3 to display the players advancing to the next round\n"
            "Press 4 to display the previous round\n"
            "Press 5 to exit the program\n");

    scanf("%d", &choice);

    if(choice == 1)
    {
        enter(names);
    }

    system("cls");
    if(choice == 3)
    {
        previous(winner);
    }               
}
char enter(char names[][20])
{

    int result;
    int score1;
    int score2;
    int p, c, j, l, i;
    char winner[8][8];  
        system("cls");

        for(i = 0; i < 8; i++)
        {
            printf("\n\n%s vs %s",names[i],names[i+8]);

            score1 = 0;
            score2 = 0;

            for(j = 0; j < 5; j++)
            {
                printf("\n\nEnter game %d results, press 1 if %s won or"
                       " 2 if %s won :\n",(j+1), names[i], names[i+8]);
                scanf("%d", &result);

                if(result == 1)
                {
                    score1++;
                }
                if(result == 2)
                {
                    score2++;
                }

                printf("\n\n1Current score is %d-%d", score1, score2);

                if(score1 == 3)
                {
                    printf("\n\n%s adavances to the next round!",names[i]);
                    strncpy(winner[i], names[i], 10);
                    printf("\n\nPress Enter to Continue");
                    getch();
                    system("cls");
                    break;
                }

                if(score2 == 3)    
                {
                    printf("\n\n%s adavances to the next round!",names[i+8]);
                    strncpy(winner[i], names[i+8], 10);
                    printf("\n\nPress Enter to Continue");
                    getch();
                    system("cls");
                    break;
                }
            }
        }
        system("cls");
        printf("The players advancing to the next round are:\n\n");

        for(p = 0; p < 8; p++)
        {
            for(c = 0; c < 8; c++)
            {
                printf("%c",winner[p][c]);
            }
            printf("\n");
        }

        printf("\n\nPress Enter to Continue");
        getch();
        system("cls");
        menu(names, winner);
        return winner[8][8];            
}

void previous(char winner[][8])
{
    int i, j;

        for(i = 0; i < 8; i++)
        {
            for(j = 0; j < 8; j++)
            {
                printf("%c",winner[i][j]);
            }
            printf("\n");
        }
}     

1 个答案:

答案 0 :(得分:1)

程序中没有数组winner的数据!至少在你第一次打电话时没有。

菜单功能的签名是:

void menu(char names[][20], char winner[][8]);

但是你可以像这样从主叫它:

menu(names);

缺少winner参数。这不应该发生,但是您已经为此函数声明了原型,即:

void menu();

不幸的是,C将空的parens视为“你传递的任何参数”,而不是不带参数的函数。这意味着您的函数调用滑过。修复方法是为原型提供正确的签名,并从winner传递合适的main数组。

奇怪的是,您的enter函数提供了一个本地数组winner。当您致电enter时,此数组将始终为新数组。这可能不是你想要的。因此,您的程序应该有一个names和一个winner数组。 (你可以传递这些数组,但是你应该确保这些数组是一致的。当你真的想要对现有数组进行操作时,不要创建新的数组。)

您还可以递归地呼叫menu。这意味着你会更深入地进入呼叫结构而没有真正的好处。不要这样做;改为使用循环:do显示用户未选择“退出”的菜单while。 (有递归函数的应用程序,但这不是一个。)

相关问题