Retrieving a paragraph instead of a single line of text

Reply

Join Date: Sep 2008
Posts: 2
Reputation: hikaru1239 is an unknown quantity at this point 
Solved Threads: 0
hikaru1239 hikaru1239 is offline Offline
Newbie Poster

Retrieving a paragraph instead of a single line of text [urgent]

 
0
  #1
Oct 23rd, 2008
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:

  1. void interactive(){ //if argument is 0, program will enter the interactive mode.
  2.  
  3. int choice;
  4. int year, month, day;
  5. char div[40];
  6. char tags[WORD_COUNT];
  7. char entry[WORD_COUNT];
  8.  
  9. printf("\t\t....................................................\n");
  10. printf("\t\t What do you wish to do?\n");
  11. 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");
  12. scanf("%d", &choice);
  13. printf("\nYour choice is: %d\n\n", choice);
  14. inFile = fopen("journal.txt", "a");
  15.  
  16. if(choice == 1){
  17.  
  18. printf("Enter your entry's date [yy,mm,dd format]: ");
  19. scanf("%d, %d, %d", &year, &month, &day);
  20. fprintf(inFile, "------------------------------------------------------------------------------\n\n%s", div);
  21. fprintf(inFile, "Date: %d, %d, %d\n", year, month, day);
  22.  
  23.  
  24. printf("[Tags: Press ^Z to proceed] Enter your post's keywords: ");
  25.  
  26. while(fgets(tags, WORD_COUNT, stdin)){
  27.  
  28. len = strlen(tags);
  29. tags[len-1] = '\0';
  30.  
  31. }
  32.  
  33. fprintf(inFile, "\nTags: %s", tags);
  34.  
  35. printf("\n[Press ^Z to post] Your entry:\n\n");
  36.  
  37. while(fgets(entry, WORD_COUNT, stdin)){
  38.  
  39. len2 = strlen(entry);
  40. entry[len2-1] = '\0';
  41.  
  42. }
  43.  
  44. fprintf(inFile, "\n\n>START OF ENTRY\n\n\n %s \n\n\n>END OF ENTRY\n\n\n", entry);
  45. fclose(inFile);
  46.  
  47.  
  48. }
  49.  
  50. if(choice == 4){
  51.  
  52. printf("Thank you for using ............................. \n\n");
  53. printf("Program will now terminate\n");
  54. exit(0);
  55.  
  56.  
  57. }
  58.  
  59. if (choice > 4){
  60.  
  61. printf("Your choice is not in the menu. Program will now terminate\n\n");
  62.  
  63. }
  64.  
  65.  
  66. }

The problem is in this part:

  1.  
  2. while(fgets(entry, WORD_COUNT, stdin)){
  3.  
  4. len2 = strlen(entry);
  5. entry[len2-1] = '\0';
  6.  
  7. }

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.
Last edited by hikaru1239; Oct 23rd, 2008 at 1:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,563
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Retrieving a paragraph instead of a single line of text

 
0
  #2
Oct 23rd, 2008
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;
}
Last edited by BestJewSinceJC; Oct 23rd, 2008 at 7:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 176
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Retrieving a paragraph instead of a single line of text

 
0
  #3
Oct 23rd, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC