allowing user to enter paragraphs

J

Jesse Engle

how can i allow a user of my program to enter paragraphs seperated by line
spaces using gets() or a similar function?

i want the user to be able to enter as many lines as they want, while able
to press <enter> to make a new line, and when they are done they type
".<ENTER>" and it stops inserting the message.
 
J

Joona I Palaste

Jesse Engle said:
how can i allow a user of my program to enter paragraphs seperated by line
spaces using gets() or a similar function?
i want the user to be able to enter as many lines as they want, while able
to press <enter> to make a new line, and when they are done they type
".<ENTER>" and it stops inserting the message.

Use fgets() in a loop, and when you only get "\n", stop the program.
 
J

Jesse Engle

that's the thing. i want it to continue if the user presses <enter>, which
signals a new line, and keep recieving what the user types.

for example, microsoft word doesn't close itself when a user presses
<enter>. it keeps on going and getting whatever the user types.

i need that sort of functionality.
 
M

Mike Wahler

Jesse Engle said:
that's the thing. i want it to continue if the user presses <enter>,

Presumably preceeded by other nonnewline characters.
which
signals a new line, and keep recieving what the user types.

If input == text + newline, continue.
If input == newline only, stop.
for example, microsoft word doesn't close itself when a user presses
<enter>. it keeps on going and getting whatever the user types.

That's not a good comparison, since MS Word uses a GUI interface,
and a menu option (or the built-in Windows interface) to quit.

If you'd like for the user to be able to use a 'blank' line
to delimit multiple paragraphs being input, then just extend
the above to using two (or however many you like) 'standalone'
newlines for the 'quit' signal.
i need that sort of functionality.

And you can get it using 'fgets()' in a loop as Joona suggests.

-Mike
 
P

Peter Hille

Am Sat, 06 Mar 2004 00:22:45 +0000 schrieb Jesse Engle:

Hi Jesse,
that's the thing. i want it to continue if the user presses <enter>, which
signals a new line, and keep recieving what the user types.

for example, microsoft word doesn't close itself when a user presses
<enter>. it keeps on going and getting whatever the user types.

i need that sort of functionality.

the following (untested) code should fit your needs:

char buf[1025]; /* input buffer */

while (fgets (buf, 1024, stdin)
&& !(!strncmp (buf, ".", 1) && strlen (buf) == 2))
{
/* do whatever you want with the input buffer here */
}

the strlen(buf) is 2 because we have one byte for the '.' and one for the
'\n' when the user pressed enter.

Hope that helps

Peter
 
R

Richard Heathfield

Jesse said:
that's the thing. i want it to continue if the user presses <enter>, which
signals a new line, and keep recieving what the user types.

for example, microsoft word doesn't close itself when a user presses
<enter>.

It doesn't? You must have a different version to the ones I've used. :)
 
M

Mac

Use fgets() in a loop, and when you only get "\n", stop the program.


Joona, I think you missed the period all by itself on the line just before
the <ENTER>. I think the OP wants to use a line consisting of a period only
as a sentinel line to terminate the line entry phase of the program.

--Mac
 
M

Mac

Am Sat, 06 Mar 2004 00:22:45 +0000 schrieb Jesse Engle:

Hi Jesse,
that's the thing. i want it to continue if the user presses <enter>,
which signals a new line, and keep recieving what the user types.

for example, microsoft word doesn't close itself when a user presses
<enter>. it keeps on going and getting whatever the user types.

i need that sort of functionality.

the following (untested) code should fit your needs:

char buf[1025]; /* input buffer */

while (fgets (buf, 1024, stdin)
&& !(!strncmp (buf, ".", 1) && strlen (buf) == 2))

it might be simpler to just do this:
while (fgets (buf, 1024, stdin) && (buf[0] != '.' || buf[1] != '\0'))
{
/* do whatever you want with the input buffer here */
}

the strlen(buf) is 2 because we have one byte for the '.' and one for
the '\n' when the user pressed enter.

Hope that helps

Peter

--Mac
 
M

Mac

Presumably preceeded by other nonnewline characters.


If input == text + newline, continue.
If input == newline only, stop.

That's not what the OP wants. The OP wants to allow the user to repeatedly
enter lines, including blank lines, as long as desired. When the user is
done entering lines, he or she enters a sentinel value consisting of a
period followed by a newline. The program should interpret this sentinel
value as a signal to end line entry.
That's not a good comparison, since MS Word uses a GUI interface,
and a menu option (or the built-in Windows interface) to quit.

If you'd like for the user to be able to use a 'blank' line
to delimit multiple paragraphs being input, then just extend
the above to using two (or however many you like) 'standalone'
newlines for the 'quit' signal.


And you can get it using 'fgets()' in a loop as Joona suggests.

Yes, except the OP needs to detect the sentinel line with a period, not a
blank line, as Joona suggested.

--Mac
 
J

Joona I Palaste

Mac said:
Use fgets() in a loop, and when you only get "\n", stop the program.
[/QUOTE]
Joona, I think you missed the period all by itself on the line just before
the <ENTER>. I think the OP wants to use a line consisting of a period only
as a sentinel line to terminate the line entry phase of the program.

Yes I did miss that. Thanks for the correction.
 
C

CBFalconer

Mac said:
.... snip ...

That's not what the OP wants. The OP wants to allow the user to
repeatedly enter lines, including blank lines, as long as desired.
When the user is done entering lines, he or she enters a sentinel
value consisting of a period followed by a newline. The program
should interpret this sentinel value as a signal to end line entry.

He wants an input routine such as my ggets (available on my site,
download section) which inputs complete lines. Then he can
simplify the input system to:

char *ln;

....
while (0 == ggets(&ln))
if (('.' == *ln) && (1 == strlen(ln))) break;
else {
/* do something with ln */
/* possibly free(ln) if no longer needed */
}
/* All input finished */
/* There may be stored ln values to free */
 
P

Peter Hille

Am Fri, 05 Mar 2004 22:53:19 -0800 schrieb Mac:

char buf[1025]; /* input buffer */

while (fgets (buf, 1024, stdin)
&& !(!strncmp (buf, ".", 1) && strlen (buf) == 2))

it might be simpler to just do this:
while (fgets (buf, 1024, stdin) && (buf[0] != '.' || buf[1] != '\0'))
You're right, thanks for the hint... I had spent the whole night in front
of my PC so i was partly brain-dead when i wrote that ;-) There is one
small correction I want to do:

while (fgets (buf, 1024, stdin) && (buf[0] != '.' || buf[1] != '\n'))

buf[1] isn't \0, it's \n because the newline at the end of input is also
appended to the string by fgets, but thanks anyways :)

Peter

P.S.: If the OP (or anyone else of course) is interested in a small demo
program showing this: <http://das-system.ath.cx/~peter/fgets.c>
 
M

macluvitch

Hello
__The 1st technic begin by allocating memory for a buffer of a given size eg 1024 and during the program resize it with realloc (or similar functions)
__the 2nd one I suggest you a common one : a linked list.

Sincerely mac :)
 
M

Mac

Am Fri, 05 Mar 2004 22:53:19 -0800 schrieb Mac:

char buf[1025]; /* input buffer */

while (fgets (buf, 1024, stdin)
&& !(!strncmp (buf, ".", 1) && strlen (buf) == 2))

it might be simpler to just do this:
while (fgets (buf, 1024, stdin) && (buf[0] != '.' || buf[1] != '\0'))
You're right, thanks for the hint... I had spent the whole night in
front of my PC so i was partly brain-dead when i wrote that ;-) There is
one small correction I want to do:

while (fgets (buf, 1024, stdin) && (buf[0] != '.' || buf[1] != '\n'))

buf[1] isn't \0, it's \n because the newline at the end of input is also
appended to the string by fgets, but thanks anyways :)

Oh yes. I apologize. And I don't think your original code is bad. You
could argue that it is clearer to someone accustomed to the standard
library. Also, it is more ammenable to potential future changes. (e.g., if
the spec. changed and the sentinel line became something more complicated)
I just thought it would be nice to avoid the function calls for this simple
case, and not confuse the OP if he/she (I think Jesse is a man's name, but
just in case it isn't...) doesn't know what the standard library functions
do.

Peter

P.S.: If the OP (or anyone else of course) is interested in a small demo
program showing this: <http://das-system.ath.cx/~peter/fgets.c>

--Mac
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top