Hi, I was practicing for our hands on exams tomorrow when I encountered a problem in my code. Here is a part of my code:

void interactive(){ //if argument is 0, program will enter the interactive mode.

    int choice;
    int year, month, day;
    char div[40];
    char tags[WORD_COUNT];
    char entry[WORD_COUNT];

        printf("\t\t....................................................\n");
        printf("\t\t  What do you wish to do?\n");
        printf("\n\n\t\t[1]\tAdd new entry\n\t\t[2]\tEdit entry\n\t\t[3]\tList Entry\n\t\t[4]\tQuit\n");
        scanf("%d", &choice);
        printf("\nYour choice is: %d\n\n", choice);
        inFile = fopen("journal.txt", "a");

        if(choice == 1){

            printf("Enter your entry's date [yy,mm,dd format]: ");
            scanf("%d, %d, %d", &year, &month, &day);
            fprintf(inFile, "------------------------------------------------------------------------------\n\n%s", div);
            fprintf(inFile, "Date: %d, %d, %d\n", year, month, day);


            printf("[Tags: Press ^Z to proceed] Enter your post's keywords: ");

            while(fgets(tags, WORD_COUNT, stdin)){

            len = strlen(tags);
            tags[len-1] = '\0';

            }

            fprintf(inFile, "\nTags: %s", tags);

            printf("\n[Press ^Z to post] Your entry:\n\n");

            while(fgets(entry, WORD_COUNT, stdin)){

            len2 = strlen(entry);
            entry[len2-1] = '\0';

            }

            fprintf(inFile, "\n\n>START OF ENTRY\n\n\n %s \n\n\n>END OF ENTRY\n\n\n", entry);
            fclose(inFile);


            }

        if(choice == 4){

            printf("Thank you for using ............................. \n\n");
            printf("Program will now terminate\n");
            exit(0);


            }

        if (choice > 4){

            printf("Your choice is not in the menu. Program will now terminate\n\n");

            }


    }

The problem is in this part:

while(fgets(entry, WORD_COUNT, stdin)){

            len2 = strlen(entry);
            entry[len2-1] = '\0';

            }

This is the part that retrieves the user input which will be copied to a text file. However, this only allows to retrieve a single line of text instead of retrieving everything that was entered by the user.

For example, the user enters:
---------------------------------------------------
Let's see... So far, nothing good happened to me. I look back and all I see were a bunch mistakes that I have committed. All I see were things I knew I shouldn't have done but...

<enter>

Am I still in control of my life? Sometimes, I think don't have the authority anymore. Why did I end up with something that only takes half of my interest (BSMMA)? And why did I end up here when I was...
---------------------------------------------------

The program will only retrieve the part after the user presses enter for the next paragraph.

How do I fix this code so that it will not only retrieve the user input per line but everything that the user entered for the entry tag?

Thanks for the help.

You would keep a pointer to the String that you wish to store it in and concatenate your 'new' user entry onto whatever the String's current contents are. OR you could make the function 'return' whatever the user entered as a String, and concatenate that entry with whats stored in the current String.


I haven't coded in C recently, but those are the only two ways you can do this. The pseudocode for the second way would look like this:

int main(){
String allTextEntered;
String currentEntry = interactive();
allTextEntered + currentEntry; //PSEUDOCODE - WONT WORK AS TYPED. CONCEPT WORKS.
}

String interactive(){
//code to get text from the user

return userEntry;
}

The standard input (stdin) is buffered. That's why you can write and nothing happens until you give the signal to go ahead and read. That occurs when you press ENTER/RETURN.

fgets() can only read up to n-1 of whatever size of buffer you have set apart for it. In this case WORD_COUNT. That portion of read needs to be store in some memory or next time when while() goes through, variable entry will be over written. After fgets() can not read anything else, entry will hold the last portion of input available.
If you would know before hand how much is there in the input you could create an array big enough to hold everything; by copying and concatenating each read, using strcpy() and strcat().
The risk is that more input could be entered than this array memory can hold, creating a buffer overrun.

The other alternative is learning about dynamic memory. Which it is somehow out of the reach of a quick example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.