![]() |
| ||
| Retrieving a paragraph instead of a single line of text [urgent] 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.The problem is in this part:
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. |
| ||
| Re: Retrieving a paragraph instead of a single line of text 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; } |
| ||
| Re: Retrieving a paragraph instead of a single line of text 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. |
| All times are GMT -4. The time now is 7:46 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC