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.