473,378 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

allowing user to enter paragraphs

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.

--
Jesse Engle
Nov 14 '05 #1
13 4322
Jesse Engle <je****@elementalsn.com> scribbled the following:
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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"All that flower power is no match for my glower power!"
- Montgomery Burns
Nov 14 '05 #2
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.
--
Jesse Engle

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:c2**********@oravannahka.helsinki.fi...
Jesse Engle <je****@elementalsn.com> scribbled the following:
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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"All that flower power is no match for my glower power!"
- Montgomery Burns

Nov 14 '05 #3
"Jesse Engle" <je****@elementalsn.com> wrote in message
news:pR******************@newssvr31.news.prodigy.c om...
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
Nov 14 '05 #4
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
Nov 14 '05 #5
Jesse Engle wrote:
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. :-)

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #6
Mac
On Sat, 06 Mar 2004 00:14:34 +0000, Joona I Palaste wrote:
Jesse Engle <je****@elementalsn.com> scribbled the following:
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.

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

Nov 14 '05 #7
Mac
On Sat, 06 Mar 2004 01:39:50 +0100, Peter Hille wrote:
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

Nov 14 '05 #8
Mac
On Sat, 06 Mar 2004 00:39:00 +0000, Mike Wahler wrote:
"Jesse Engle" <je****@elementalsn.com> wrote in message
news:pR******************@newssvr31.news.prodigy.c om...
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.


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.
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.


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


--Mac

Nov 14 '05 #9
Mac <fo*@bar.net> scribbled the following:
On Sat, 06 Mar 2004 00:14:34 +0000, Joona I Palaste wrote:
Jesse Engle <je****@elementalsn.com> scribbled the following:
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.

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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"War! Huh! Good God, y'all! What is it good for? We asked Mayor Quimby."
- Kent Brockman
Nov 14 '05 #10
Mac wrote:
.... 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 */

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #11
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>
Nov 14 '05 #12
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 :)

Nov 14 '05 #13
Mac
On Sat, 06 Mar 2004 15:28:48 +0100, Peter Hille wrote:
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

Nov 14 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Roderick Thomas | last post by:
This problem centers around the basic character/line count problem most people have asked and have received numerous responses to. However, my question is a little more detailed and I'm hoping...
40
by: | last post by:
Could someone cite some offical rule or documentation with regard to the <P> tag? I've seen folks put it in between paragraphs... and others wrap it around a paragraph. I'd think to use it...
2
by: needhelp | last post by:
This one ought to be simple, but I have dug myself deeper and deeper, and gone farther and farther from what I wanted and I'm giving up. And my html that I've written is so bolloxed up now that...
5
by: yjc238 | last post by:
Here's what needs to be done. I have a form with email1, email2, email3, email4, and email5. I need a way to have an alert come up when more than one email field contains a value. The user can...
2
by: Dave Smithz | last post by:
Hello there, Summary: How far can you go with SQL Select queries using like clauses with wildcard characters. Can you apply anything like regular expressions? Full details: On a Intranet...
0
by: Joe Schmoe | last post by:
I am working on an ASP.NET application that generates an Excel 2003 spreadsheet on demand for the user, containing much sales information, and then editable cells for the salerep user to enter...
7
by: NDayave | last post by:
I was wondering if there was a way to enable the user to set variables that remain set until the user changes them when desired, even after the database has been closed and the computer shutdown. ...
1
nateraaaa
by: nateraaaa | last post by:
In some way or another my Visual Studio context menu has changed and will not allow me to enter comments for my changes before checking the file into sourcesafe. I used to be able to right click and...
5
by: keoo | last post by:
Im putting the part of the code which is relevant to my question......The program is opening the texfile which i already have created..Im not quite managing to make the program count the number of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.