Question

I am trying to put the names from the scanf into an array and have no idea where it would go in the program. I need the array of names to create a bill for each card based on name size and upper or lower characters.

here is the function for the file output part of the program.

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;
}
Was it helpful?

Solution

The comment that 'most of the code is just printing to a file' means that the card printing code should probably be in a function of its own called from the function shown — so there'd be no need for us to analyze it. The card printing code definitely makes it harder to see the wood for the trees.

With a function:

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

the code in createoutfilecard() reduces to:

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);
            }
        }
    }
}

There are still problems here. For example, if the program fails to create (open) the file, it quietly ignores the problem. Still, that's better than crashing because the file pointer was null. The code for validating the inputs is convoluted and incomplete. The second attempt to specify the size is not validated until the start of processing the next card; the prompt for the name is not given if the size is wrong. It is still true that if the code is not called with y == 1, there are going to be problems. I've changed the function to return void since the only value returned was 0, and there's no point in making the caller check that.

Stylistically, loops in C should not usually be written:

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

You should normally use the idiomatic C convention of counting from zero up to (but not including) the limit:

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

Now, with those gripes and grumbles out of the way, you seem to want to create a list of names so you can cost the number of cards produced.

The simplest technique is simply to add an array of fixed size character strings:

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

Then, in the code after if (scanf("%s", word) == 1) (congratulations on checking this input — the other uses of scanf() should also be checked, though), you can write:

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

After the loop is complete, you can do your costing.

Please note the guidelines on how to create an SSCCE (Short, Self-Contained, Correct Example) and do not include so much extraneous material in the programs you ask questions about. Not everyone is as patient as me!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top