如何将scanf中的get名称放入数组中?

时间:2013-04-30 03:26:43

标签: c nested-loops character-arrays

我正在尝试将scanf中的名称放入数组中,并且不知道它将在程序中的位置。我需要一系列名称来根据名称大小和大写或小写字符为每张卡创建一个帐单。

这是程序文件输出部分的功能。

int createoutfilecard(int y)
{
    int spaceCounter; // The number of spaces we want to start with based on what level tree we are making
    int originalSpaceCounter; // We need to keep this for the end with the stem of the tree
    int spaceLoopCounter; // This is for our space control structure
    int spaceLoopCounter2; // This is just to have a different one for the end space control structure
    int treeLoopLevel = 0; // This is the level of tree we want to make
    int starNumber = 1; // This is the number of stars we need and we always start with 1
    int starLoopCounter; // This is the counter for the star loop structure
    int x; // Just a generic counter for the overall loop
    char word[50];
    int c;
    int b;
    int q;
    int p;
    char str[80];
    if (y== 1){

        printf("Enter the tree size: ");
        scanf("%d", &treeLoopLevel);
        //for i < total number of cards
        //loop through entire program
        //printf("invalid input");

        printf("Enter the number of cards: ");
        scanf("%d", &q);}
    for (p=1; p<=q; p++){

        if (treeLoopLevel>=1 && treeLoopLevel<=10){
            printf("Enter the recipient's name:     ");
            }
        else{
            printf("invalid input");
            printf("Enter the tree size: ");
            scanf("%d", &x);}

        if (scanf(" %49s", &word)==1){
            system("cls");
            strcpy(str,"christmascard");
            strcat(str,word);
            strcat(str,".txt");
            printf("The file will be located at %s\n", str);
            outFile=fopen(str, "w");
            for (c=0; c<=79; c++){
            fprintf(outFile, "-");
            }
            fprintf(outFile, "\n");
        while (b=0)
                fprintf(outFile, "\n                                                              \n");
                fprintf(outFile, "-        Happy Holidays %s                                                   -" , word);  


                spaceCounter = 39; //(treeLoopLevel * 2) + 1;  This is going to be our space counter limit for our loop further down
                originalSpaceCounter = spaceCounter - 1; // We will need the original number minus one for the trunk of the tree at the end

                fprintf(outFile, "\n");

                for(x = 1; x <= treeLoopLevel; x++) // Overall Loop
                {

                    for(spaceLoopCounter = 1; spaceLoopCounter <= spaceCounter; spaceLoopCounter++) // This does our beginning spaces
                    {
                        if(spaceLoopCounter == 1) // We need to start with the minus only
                        {
                            fprintf(outFile, "-");
                        }
                        else // We need the rest to be spaces so any other condition then the first one is going to be a space
                        {
                            fprintf(outFile, " ");
                        };

                    };

                    for(starLoopCounter = 1; starLoopCounter <= starNumber; starLoopCounter++) // This is the star loop that only goes up to what starNumber is set to
                    {
                        fprintf(outFile, "*");
                    };

                    starNumber = starNumber + 2; // This is for incrementing the number of stars for our counter limiter for the star loop above

                    for(spaceLoopCounter2 = 1; spaceLoopCounter2 <= spaceCounter; spaceLoopCounter2++) // This is going to be for the right side so we have the right number of spaces
                    {
                        fprintf(outFile, " ");
                    };

                    fprintf(outFile, "-\n");
                    spaceCounter = spaceCounter - 1; // This modifies the space counter after it has been used on both sides for the next iteration of the overall loop
                };

                fprintf(outFile, "-"); // Tree trunk line minus sign on the left

                for(spaceLoopCounter = 1; spaceLoopCounter <= originalSpaceCounter; spaceLoopCounter++) // Spaces for the tree trunk on the left
                {
                    fprintf(outFile, " ");
                };

                fprintf(outFile, "||"); // Tree Trunk

                for(spaceLoopCounter = 1; spaceLoopCounter <= originalSpaceCounter; spaceLoopCounter++) // Spaces for the tree trunk on the right
                {
                    fprintf(outFile, " ");
                };

                fprintf(outFile, "-\n"); // Tree trunk line minus sign on the right
                fprintf(outFile, "-"); // Tree trunk pot minus sign on the left

                for(spaceLoopCounter = 1; spaceLoopCounter <= originalSpaceCounter; spaceLoopCounter++) // Spaces for the tree trunk pot on the left
                {
                    fprintf(outFile, " ");
                };

                fprintf(outFile, "()");

                for(spaceLoopCounter = 1; spaceLoopCounter <= originalSpaceCounter; spaceLoopCounter++) // Spaces for the tree trunk pot on the right
                {
                    fprintf(outFile, " ");
                };

                fprintf(outFile, "-\n"); // Tree trunk line minus sign on the right
        };

        for(c=0;c<58;c++)
        {

            fprintf(outFile, "\n");
            fprintf(outFile, "-"); //print left side

            for(b=0;b<78;b++)  
            {
                fprintf(outFile, " "); //print spaces
            }

            fprintf(outFile, "-"); //print right side

        }
        fprintf(outFile, "\n");
        for (c =1; c <=80;c++) 
            fprintf(outFile, "-");

        fclose(outFile); 
        }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

“大多数代码只是打印到文件”的注释意味着卡片打印代码可能应该是从所显示的函数调用它自己的函数 - 所以我们不需要分析它。卡片打印代码肯定会让人更难看到树木。

有一个功能:

static void print_card(FILE *outFile, const char *word, int treeLoopLevel);

createoutfilecard()中的代码缩减为:

static void createoutfilecard(int y)
{
    int treeLoopLevel = 0;
    char word[50];
    int q;
    int p;
    char str[80];
    if (y== 1)
    {
        printf("Enter the tree size: ");
        scanf("%d", &treeLoopLevel);

        printf("Enter the number of cards: ");
        scanf("%d", &q);
    }
    for (p=1; p<=q; p++)
    {
        if (treeLoopLevel>=1 && treeLoopLevel<=10)
        {
            printf("Enter the recipient's name:     ");
        }
        else
        {
            printf("invalid input");
            printf("Enter the tree size: ");
            scanf("%d", &treeLoopLevel);    // Was using x!
        }

        if (scanf(" %49s", word)==1)
        {
            system("cls");
            strcpy(str,"christmascard");
            strcat(str,word);
            strcat(str,".txt");
            printf("The file will be located at %s\n", str);
            FILE *outFile=fopen(str, "w");
            if (outFile != 0)
            {
                print_card(outFile, word, treeLoopLevel);
                fclose(outFile);
            }
        }
    }
}

这里仍有问题。例如,如果程序无法创建(打开)文件,它会悄悄地忽略该问题。但是,这比崩溃更好,因为文件指针为空。验证输入的代码是复杂且不完整的。在开始处理下一张卡之前,不会验证指定大小的第二次尝试;如果大小错误,则不会给出名称提示。如果不使用y == 1调用代码,仍然会出现问题。我已经将函数更改为返回void,因为返回的唯一值是0,并且调用者检查它是没有意义的。

在风格上,通常不应写出C中的循环:

for (p=1; p<=q; p++)

您通常应该使用惯用的C约定,从零开始计算(但不包括)限制:

for (p = 0; p < q; p++)

现在,有了这些抱怨和抱怨,您似乎想要创建一个名单列表,这样您就可以花费生产的卡片数量。

最简单的技术就是添加一个固定大小的字符串数组:

enum { MAX_CARDS = 20 };
enum { MAX_NAME_LEN = 50 };
char names[MAX_CARDS][MAX_NAME_LEN];
int  n_names = 0;

然后,在if (scanf("%s", word) == 1)之后的代码中(祝贺检查此输入 - 还应检查scanf()的其他用途),您可以写:

strcpy(names[n_names++], word);

循环完成后,您可以进行成本核算。

请注意有关如何创建SSCCE(Short, Self-Contained, Correct Example)的指南,并且不要在您提出问题的程序中包含太多无关的内容。不是每个人都像我一样耐心!