473,748 Members | 4,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

output problem

hi,
i am facing a problem with my program output

here is the program

/*************** *************** *************** \
* CD Database *
* *
\************** *************** *************** */

#include <stdio.h>

main()

{
char title[61]; /* CD title */
char artist[61]; /* Artist Name */

printf(" Welcome to the CD database ... \n");

/* prompt the user to enter the CD title */

printf("Please enter the title of the CD ... \n");
printf("Title? ");
scanf("%[^\n]", title);

/* prompt the user to enter the artist name */
printf("Please enter the artist name ... \n");
printf("artist? ");
scanf("%s", artist);
/* CD details output */

printf("The CD details you entered are : \n");
printf(" =============== =============== ==\n");
printf("Title : %s\n", title);
printf("Artist: %s\n", artist);
printf(" =============== =============== ==\n");
printf("Thanks for using our program ...\n");

}

when i try to enter the last name of the artist after the first name
separated with space,
with using %[^\n] instead of %s in line 27 ,when i run the program it
just prompt me for the (the title) first scanf() only
and the program exit without prompting me for the second scanf()
(artist name), also issue was the same
when i tried fflush(stdin) after scanf() statements.That problem dose
not exist when using %s instead of [^\n]
but i have to enter one word (the first name only)
any opinion?

thanks in advanced

Apr 19 '07 #1
11 1930
aljaber wrote On 04/19/07 13:19,:
hi,
i am facing a problem with my program output

here is the program

/*************** *************** *************** \
* CD Database *
* *
\************** *************** *************** */

#include <stdio.h>

main()

{
char title[61]; /* CD title */
char artist[61]; /* Artist Name */

printf(" Welcome to the CD database ... \n");

/* prompt the user to enter the CD title */

printf("Please enter the title of the CD ... \n");
printf("Title? ");
scanf("%[^\n]", title);
This line reads characters from stdin and stores
them in title[], stopping when it reads a '\n'. What
you've missed is that the '\n' is not consumed: it is
still "pending" on stdin, waiting to be read by another
input function.

(By the way, you will have lots of trouble if the
user enters a title longer than 60 characters ...)
/* prompt the user to enter the artist name */
printf("Please enter the artist name ... \n");
printf("artist? ");
scanf("%s", artist);
The "%s" specifier works a little differently from
"%[...]". It starts by reading and discarding white
space characters from the input -- the first one it
reads and discards is the '\n' that stopped the first
scanf(). When it finds a non-white character, it
stores it in artist[] and continues reading and storing
characters until it finds another white space character,
when it stops. Just as with the first scanf(), the
character that stops the input remains "pending" on
stdin.

So: If the user enters "Steppenwolf\n" , this call
will discard the left-over '\n', store "Steppenwol f"
in artist[] (as before, there would be trouble if the
user entered a Really Long artist name), and will leave
the final '\n' pending on stdin, ready to be consumed
by the next input operation.

But if the user enters "Wiener Philharmoniker\ n",
this call will discard the left-over '\n', store "Wiener"
in artist[] and then stop because of the ' ' character.
The " Philharmoniker\ n" part will remain un-read. This
was (I guess) the problem you were trying to fix.
>
/* CD details output */

printf("The CD details you entered are : \n");
printf(" =============== =============== ==\n");
printf("Title : %s\n", title);
printf("Artist: %s\n", artist);
printf(" =============== =============== ==\n");
printf("Thanks for using our program ...\n");

}

when i try to enter the last name of the artist after the first name
separated with space,
with using %[^\n] instead of %s in line 27 ,when i run the program it
just prompt me for the (the title) first scanf() only
and the program exit without prompting me for the second scanf()
(artist name),
What does "%[^\n]" do? It reads and stores characters
until it finds a '\n', which it does not store and does
not consume. What is the first character it reads? The
'\n' that stopped the first scanf(). What does that '\n'
do to this "%[^\n]"? It causes it to stop reading, to
store a zero-length string in artist[], and to leave the
'\n' still sitting on stdin, waiting to be read.
also issue was the same
when i tried fflush(stdin) after scanf() statements.
fflush() only works on output streams (or on update
streams whose most recent operation was not input). When
you try to use it on an input stream like stdin, you get
undefined behavior. Don't Do That.
That problem dose
not exist when using %s instead of [^\n]
but i have to enter one word (the first name only)
any opinion?
You can use "%[^\n]" for both lines of input, but
you need to swallow the pending '\n'. One way to do it
is to use " %[^\n]", because a white space character in
the format string matches and consumes any arbitrary
amount of white space from the input. So if there's a
'\n' pending on stdin, the leading ' ' in the format
string will swallow it and then the "%[^\n]" will start
on the data from the next line. Note that the leading
' ' will swallow *all* white space until something non-
white appears, so if the user enters " Blank \n"
the new format will store only "Blank " in artist[].

Do *not* use "%[^\n]\n" in an attempt to get rid
of the trailing '\n'. Once again: any white space in
the format will swallow all the input white space it can
find, so the final '\n' in the format will keep reading
and reading and reading until the user enters something
non-white. Result: The user will need to enter the artist
name *before* the program prompts for it!

You could also use "%[^\n]%*c" to swallow the '\n'
and discard it: The "%*c" directive reads one character,
and the '*' says not to bother storing it anywhere.

... but all these (and more) are really just work-
arounds. scanf() is not a good tool for interactive
input, in part because it doesn't "understand " line
boundaries: Everything on stdin is just one big long
undifferentiate d stream of input as far as scanf() is
concerned. It is usually much more satsifactory to read
an entire line of input into a char array with fgets()
(*not* with gets()!), and then to extract information
from the array with sscanf() -- two s's -- or with other
string-handling functions.

--
Er*********@sun .com
Apr 19 '07 #2
On Apr 19, 8:58 pm, Eric Sosman <Eric.Sos...@su n.comwrote:
aljaberwrote On 04/19/07 13:19,:
hi,
i am facing a problem with my program output
here is the program
/*************** *************** *************** \
* CD Database *
* *
\************** *************** *************** */
#include <stdio.h>
main()
{
char title[61]; /* CD title */
char artist[61]; /* Artist Name */
printf(" Welcome to the CD database ... \n");
/* prompt the user to enter the CD title */
printf("Please enter the title of the CD ... \n");
printf("Title? ");
scanf("%[^\n]", title);

This line reads characters from stdin and stores
them in title[], stopping when it reads a '\n'. What
you've missed is that the '\n' is not consumed: it is
still "pending" on stdin, waiting to be read by another
input function.

(By the way, you will have lots of trouble if the
user enters a title longer than 60 characters ...)
/* prompt the user to enter the artist name */
printf("Please enter the artist name ... \n");
printf("artist? ");
scanf("%s", artist);

The "%s" specifier works a little differently from
"%[...]". It starts by reading and discarding white
space characters from the input -- the first one it
reads and discards is the '\n' that stopped the first
scanf(). When it finds a non-white character, it
stores it in artist[] and continues reading and storing
characters until it finds another white space character,
when it stops. Just as with the first scanf(), the
character that stops the input remains "pending" on
stdin.

So: If the user enters "Steppenwolf\n" , this call
will discard the left-over '\n', store "Steppenwol f"
in artist[] (as before, there would be trouble if the
user entered a Really Long artist name), and will leave
the final '\n' pending on stdin, ready to be consumed
by the next input operation.

But if the user enters "Wiener Philharmoniker\ n",
this call will discard the left-over '\n', store "Wiener"
in artist[] and then stop because of the ' ' character.
The " Philharmoniker\ n" part will remain un-read. This
was (I guess) the problem you were trying to fix.


/* CD details output */
printf("The CD details you entered are : \n");
printf(" =============== =============== ==\n");
printf("Title : %s\n", title);
printf("Artist: %s\n", artist);
printf(" =============== =============== ==\n");
printf("Thanks for using our program ...\n");
}
when i try to enter the last name of the artist after the first name
separated with space,
with using %[^\n] instead of %s in line 27 ,when i run the program it
just prompt me for the (the title) first scanf() only
and the program exit without prompting me for the second scanf()
(artist name),

What does "%[^\n]" do? It reads and stores characters
until it finds a '\n', which it does not store and does
not consume. What is the first character it reads? The
'\n' that stopped the first scanf(). What does that '\n'
do to this "%[^\n]"? It causes it to stop reading, to
store a zero-length string in artist[], and to leave the
'\n' still sitting on stdin, waiting to be read.
also issue was the same
when i tried fflush(stdin) after scanf() statements.

fflush() only works on output streams (or on update
streams whose most recent operation was not input). When
you try to use it on an input stream like stdin, you get
undefined behavior. Don't Do That.
That problem dose
not exist when using %s instead of [^\n]
but i have to enter one word (the first name only)
any opinion?

You can use "%[^\n]" for both lines of input, but
you need to swallow the pending '\n'. One way to do it
is to use " %[^\n]", because a white space character in
the format string matches and consumes any arbitrary
amount of white space from the input. So if there's a
'\n' pending on stdin, the leading ' ' in the format
string will swallow it and then the "%[^\n]" will start
on the data from the next line. Note that the leading
' ' will swallow *all* white space until something non-
white appears, so if the user enters " Blank \n"
the new format will store only "Blank " in artist[].

Do *not* use "%[^\n]\n" in an attempt to get rid
of the trailing '\n'. Once again: any white space in
the format will swallow all the input white space it can
find, so the final '\n' in the format will keep reading
and reading and reading until the user enters something
non-white. Result: The user will need to enter the artist
name *before* the program prompts for it!

You could also use "%[^\n]%*c" to swallow the '\n'
and discard it: The "%*c" directive reads one character,
and the '*' says not to bother storing it anywhere.

... but all these (and more) are really just work-
arounds. scanf() is not a good tool for interactive
input, in part because it doesn't "understand " line
boundaries: Everything on stdin is just one big long
undifferentiate d stream of input as far as scanf() is
concerned. It is usually much more satsifactory to read
an entire line of input into a char array with fgets()
(*not* with gets()!), and then to extract information
from the array with sscanf() -- two s's -- or with other
string-handling functions.

--
Eric.Sos...@sun .com
Mr, Eric.
many thanks to you, and it is not enough.
thanks again for the time you gave to replay.

Apr 19 '07 #3
In article <1177009117.354 179@news1nwk>
Eric Sosman <Er*********@Su n.COMwrote:
>What does [scanf's] "%[^\n]" do? It reads and stores characters
until it finds a '\n', which it does not store and does
not consume. What is the first character it reads? The
'\n' that stopped the first scanf(). What does that '\n'
do to this "%[^\n]"? It causes it to stop reading, to
store a zero-length string in artist[] ...
Small but important correction: when the directive fails, as
in this case, scanf() terminates, leaving artist[] unchanged.

The return value from scanf() tells you how many assignments
were succesfully completed. For instance:

n = scanf("%d%d%d", &e, &f, &g);

attempts to read three "int"s (%d) and store them into e, f, and
g respectively; but if only one "int" is available before scanf()
runs into trouble, only e will receive a value: f and g will remain
unchanged. The scanf() call will return 1, setting n to 1.

If scanf() returns 0, it must have run into a matching failure
before doing any assignments. If scanf() returns EOF, it ran out
of input (due to either EOF *or* error) before doing any assignments.

Since scanf() cannot, in general, tell you how far it got (there
are some exceptions using "%n" directives but they are limited),
the scanf() function should almost never be used. Really, seriously:
beginners should never call scanf(). Using sscanf() is OK, but
using scanf() directly is not.
You could also use "%[^\n]%*c" to swallow the '\n'
and discard it: The "%*c" directive reads one character,
and the '*' says not to bother storing it anywhere.
(This, however, runs into other problems.)
... but all these (and more) are really just work-
arounds. scanf() is not a good tool for interactive
input ...
Indeed.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Apr 20 '07 #4
Chris Torek wrote:
In article <1177009117.354 179@news1nwk>
Eric Sosman <Er*********@Su n.COMwrote:
>What does [scanf's] "%[^\n]" do? It reads and stores characters
until it finds a '\n', which it does not store and does
not consume. What is the first character it reads? The
'\n' that stopped the first scanf(). What does that '\n'
do to this "%[^\n]"? It causes it to stop reading, to
store a zero-length string in artist[] ...

Small but important correction: when the directive fails, as
in this case, scanf() terminates, leaving artist[] unchanged.
Ah! Good catch; thank you. "%[...]" succeeds only if it
can match a non-empty sequence, so when '\n' is the next input
character "%[^\n]" fails and does nothing instead of matching
a zero-length string and doing something with it. My mistake.

--
Eric Sosman
es*****@acm-dot-org.invalid
Apr 20 '07 #5
On Thu, 19 Apr 2007 22:19:57 -0400, Eric Sosman
<es*****@acm-dot-org.invalidwrot e:
>Chris Torek wrote:
>In article <1177009117.354 179@news1nwk>
Eric Sosman <Er*********@Su n.COMwrote:
>>What does [scanf's] "%[^\n]" do? It reads and stores characters
until it finds a '\n', which it does not store and does
not consume. What is the first character it reads? The
'\n' that stopped the first scanf(). What does that '\n'
do to this "%[^\n]"? It causes it to stop reading, to
store a zero-length string in artist[] ...

Small but important correction: when the directive fails, as
in this case, scanf() terminates, leaving artist[] unchanged.

Ah! Good catch; thank you. "%[...]" succeeds only if it
can match a non-empty sequence, so when '\n' is the next input
character "%[^\n]" fails and does nothing instead of matching
a zero-length string and doing something with it. My mistake.
per me sembrate due secchioni!
Apr 21 '07 #6
Chris Torek wrote:
Since scanf() cannot, in general, tell you how far it got (there
are some exceptions using "%n" directives but they are limited),
the scanf() function should almost never be used. Really, seriously:
beginners should never call scanf().
If they never call scanf(), they never acquire experience in using it.
I'd advise: Use scanf() whenever you like, and learn from your errors.
That way, someday they'll no longer be beginners.
--
DPS
Apr 26 '07 #7
Dietmar Schindler <DS***@Arcor.De writes:
Chris Torek wrote:
>Since scanf() cannot, in general, tell you how far it got (there
are some exceptions using "%n" directives but they are limited),
the scanf() function should almost never be used. Really, seriously:
beginners should never call scanf().

If they never call scanf(), they never acquire experience in using it.
And that's a problem because ...?
I'd advise: Use scanf() whenever you like, and learn from your errors.
That way, someday they'll no longer be beginners.
Perhaps. There's certainly nothing wrong with learning how scanf()
works. But once you've learned how it works, the best conclusion to
draw from the experience is probably just to avoid it. (sscanf() is
another matter.)

Note also that scanf(), sscanf(), and fscanf() all invoke undefined
behavior if they attempt to read a numeric value and the input
specifies a value that can't be represented in the specified type.
None of them provides a way to anticipate or handle numeric overflow.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 26 '07 #8
On Thu, 26 Apr 2007 15:41:34 +0200, in comp.lang.c , Dietmar Schindler
<DS***@Arcor.De wrote:
>I'd advise: Use scanf() whenever you like, and learn from your errors.
Hm, the "give the kids a gun to play with, whats the worst that could
happen?" approach. Cool.
>That way, someday they'll no longer be beginners.
Or, quite possibly, employed... :-(

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Apr 26 '07 #9
Chris Torek wrote:
>
>Since scanf() cannot, in general, tell you how far it got (there
are some exceptions using "%n" directives but they are limited),
the scanf() function should almost never be used. Really,
seriously: beginners should never call scanf().
Piggybacking. Why do you say that? It returns the number of
conversions made, and a failed conversion prevents advancing to the
next.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Apr 27 '07 #10

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

Similar topics

5
23364
by: Jay Chan | last post by:
I am trying to use a command line program to run a stored procedure that generates output in a comma-delimitted format. Somehow, ISQL or OSQL always wrap the lines at 256 characters. I believe this has something to do with the column width switch (-w). But enlarging the column width to 800 characters max still doesn't help. The following is a stored procedure that is essentially doing what my stored procedure is doing: create procedure...
1
5581
by: Lisa | last post by:
I need to apply the HTML formatting tags and the French accented characters in a XML document. The XML is generated from a database that has HTML tags and French accented characters in the records. I have specified <xsl:output method="html"/> and encoding="iso-8859-1". When I apply the xsl:value-of and set the disable-output-escaping to "yes", the HTML formatting tags are displayed correctly, but the French accented characters are...
11
1904
by: Etienne Charland | last post by:
Hi, I have a solution containing 6 C# projects; 1 WinForms project and 5 class libraries. I didn't have any problems until recently. I added a new project containing reports. I am using ActiveReports.Net. Now, whenever I make a change to a report in the class library and recompile, I get this error: "Could not copy temporary files to the output directory.". I have to close VS.Net and reopen it in order to recompile, this is very cumbersome....
3
2325
by: Dalan | last post by:
Perhaps someone has experienced this problem. I developed an Access 97 Runtime database on a Windows 98 SE machine. I have three reports that can be optionally copied to a floppy disk or hard drive through use of a macro: Output To, Report, the name of the Report, Rich Text Format, Output location and file name, and Auto Start - No. The process has worked flawlessly when running the macro on Windows 98 SE under Access 97 SR2b on the host...
5
2171
by: Tom Lam lemontea | last post by:
Hi all, This is my very first post here, I've seriously tried some programming on C, and shown below is my very first program(So you can expect it to be very messy) that I wrote after I've learned the basics. However, the output function I wrote seems to repeat unneedingly for 2 times. My trial on solving it myself have failed. Anyone willing to point out where the problem lies will be greatly appreciated. Thanks. **** Source code ****
4
8330
by: Mountain Bikn' Guy | last post by:
I am having serious problems with the following IDE bug: Could not write to output file 'x.dll' -- 'The process cannot access the file because it is being used by another process. ' and BUG: "Could Not Copy Temporary Files to the Output Directory" Error Message When You Build a Solution That Contains Multiple Projects I have tried all the solutions in Microsoft Knowledge Base Article - 313512.
5
1740
by: Brad | last post by:
I created a base page class which sets a response filter and the filter injects additional html into the response output stream. The filter works fine and everything works as expected except for the following quirk: When I navigate my browser to another url (a link in the page, a browser favorite...it doesn't mater) and then use the browsers (IE 6) Back or Forward buttons to go back to my filtered page the additional html I had added...
6
4915
by: Alec MacLean | last post by:
Hi, I've created a small application for our company extranet (staff bulletins) that outputs a list of links to PDF's that are stored in a SQL table. The user clicks a link and the PDF is loaded into a new browser window. This works as expected on the test PC (using forms authentication, but no SSL) using IE. It also works as expected on the production server when using FireFox. The production server environment is using forms...
4
9183
by: Jon | last post by:
Hi, I used XslCompiledTransform with the following Xsl file. The <xsl:text disable-output-escaping="yes"does not work when using XslCompiledTransform to do the trnasform (namely the output contain < not <), while it works when using MSXML2 to do the transform. Does anyone have the same problem and how to make the escape work? Thanks. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
8
2702
by: Alec MacLean | last post by:
Hi, I'm using the DAAB Ent Lib (Jan 2006) for .NET 2.0, with VS 2005 Pro. My project is a Web app project (using the WAP add in). Background: I'm creating a survey system for our company, for which invites will target selected personnel among our customers via email. Each email will provide a custom hyperlink for each respondent using a SQL generated GUID value in the querystring. The GUID will be checked for validity before the user...
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.