Re: i need help about Turbo C



"lovecreatesbeauty" <lovecreatesbeauty@xxxxxxxxx> writes:
Tom St Denis wrote:
You need to add a "\n" or the printf won't always show. The stream is
only guaranteed to flush if you send a newline.

Also your scanf doesn't eat the newline [that the user enters]. So
when you get to the second scanf() you get just the newline.

At the second time, every input function will be fed with the newline
left by the previous scanf.

You need to flush the input stream [hint: don't use fflush on it].

You may also want to try and use fgets() instead of scanf [hint:
buffer overflows].

What you said are really good points. Someone, e.g. Keith Thompson,
recommended to use scanf for numbers. Do you agree?

Perhaps you have me confused with someone else.

Sure, you can use scanf for numbers. Doing so doesn't present the
same overflow possibilities as an unbounded scanf("%s"). But I
usually recommend using fgets() to read a line at a time, then
sscanf() to parse the line once it's been read. (fgets() has its
problems, but they're easier to work around than scanf()'s problems.)

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <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.
.



Relevant Pages

  • Re: scanf problem
    ... because after the first scanf a newline is left ... character in the first input line is a newline. ... Any whitespace in the format string will match any amount of whitespace ...
    (comp.lang.c)
  • Re: Help! Esc button wont exit program
    ... Floating-point numbers on most computers today are binary and ... cannot exactly represent most decimal fractions. ... and if that next scanf is %c it will get only the newline; ...
    (comp.lang.c)
  • Re: Cant figure out why scanf does this
    ... trying to hault the program at the scanf statement. ... newlines or whitespace characters. ... In the above scanf call, however, there will be a remaining newline on the stdin stream and in the next call to scanf that newline will be the first character read in. ...
    (comp.lang.c)
  • Re: peek at stdin, flush stdin
    ... at least one character other than newline following the one ... other than newline, ... scanning an input stream containing "y\n" puts the ... (Here scanf() ...
    (comp.lang.c)
  • Re: i need help about Turbo C
    ... Also your scanf doesn't eat the newline. ... void delnl; ... int main{ ...
    (comp.lang.c)