0

我试图将 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;
}
4

1 回答 1

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(简短、自包含、正确示例)的指南,并且不要在您提出问题的程序中包含太多无关材料。不是每个人都像我一样有耐心!

于 2013-04-30T04:26:48.857 回答