Newbie - scanf and while loop - correction 
Author Message
 Newbie - scanf and while loop - correction

I apologize for the earlier post where I forgot the code.

I have included a portion of my code below. The problem that is driving me
crazy is that the first time thru the loop everything works fine but the
second time thru the loop the program just goes by the scanf statement. Could
someone please enlighten me as to what I am doing wrong.

Since my original post I have come across a function called _flushall() that
seems to flush out the stdin buffer. Why don't either of my two Introduction
to C Programming books talk about flushall() or problems with buffers?

int main(void)  
{
char y;
while (1)
        {
        printf("\nDo you want to input another string? (y/n)");
        scanf("%c",&y);
        if (y=='n') break;
    y='0';
    input();
    }
return 0;

Quote:
}/* End Main */

Thanks

Robert Sproat



Mon, 21 Dec 1998 03:00:00 GMT  
 Newbie - scanf and while loop - correction


| I apologize for the earlier post where I forgot the code.
|
| I have included a portion of my code below. The problem that is driving me
| crazy is that the first time thru the loop everything works fine but the
| second time thru the loop the program just goes by the scanf statement. Could
| someone please enlighten me as to what I am doing wrong.

| Since my original post I have come across a function called _flushall() that
| seems to flush out the stdin buffer. Why don't either of my two Introduction
| to C Programming books talk about flushall() or problems with buffers?

That could be because flushall() and _flushall() are not part of C.  They
may be a specialized extension of a given implementation.  It may be to
cover up for weaknesses in their library or weak programmers.  But you
don't need them.

| int main(void)  
| {
| char y;
| while (1)
|       {
|       printf("\nDo you want to input another string? (y/n)");
|       scanf("%c",&y);
|       if (y=='n') break;
|     y='0';
|     input();
|     }
| return 0;
| }/* End Main */

Did you have to type return to get the first input to work?  Your
operating system may have built a complete line of input first,
which in your case was one letter, but the input actually has both
that letter and a newline in the buffer.  The scanf gets the letter
the first time through, but the newline is still there for the next
time.

You might try:

    int n;
    ...
    scanf("%c\n%n",&y,&n);

This will set "n" with the number of characters read from the input
stream, being 1 if the "\n" didn't match (i.e. more than one character
was typed before the end of line) and being 2 if the "\n" did match.

Alternatively, use getc() or getchar() to read from input until you
find the "\n" (or EOF) then it should be safe to take another line
of input (if not EOF ... but depending on OS, it may be anyway).

If you want "hot key" input, you will have to apply operating system
tricks to get that to work, and of course that's OS dependent and you
need to pick the correct OS related newsgroup.

--
Phil Howard KA9WGN   +-------------------------------------------------------+
Linux Consultant     |  "Adolf Hitler was OK at the beginning, but then he   |
Milepost Services    |  went too far".   --Marge Schott    --World History   |



Tue, 22 Dec 1998 03:00:00 GMT  
 Newbie - scanf and while loop - correction



Quote:
>I apologize for the earlier post where I forgot the code.

>I have included a portion of my code below. The problem that is driving me
>crazy is that the first time thru the loop everything works fine but the
>second time thru the loop the program just goes by the scanf statement. Could
>someone please enlighten me as to what I am doing wrong.

>Since my original post I have come across a function called _flushall() that
>seems to flush out the stdin buffer. Why don't either of my two Introduction
>to C Programming books talk about flushall() or problems with buffers?

>int main(void)  
>{
>char y;
>while (1)
>    {
>    printf("\nDo you want to input another string? (y/n)");

You can't use printf() without a prototype.

Quote:
>    scanf("%c",&y);

You can't use scanf() without a prototype.  Also, this is
very poor practice.  Using fgets() and sscanf() are much
better.

Quote:
>    if (y=='n') break;
>    y='0';
>    input();

What is this?  There is no input() in the standard library.

Quote:
>    }
>return 0;
>}/* End Main */

In the future, try compiling your code.


Tue, 22 Dec 1998 03:00:00 GMT  
 Newbie - scanf and while loop - correction



: >I apologize for the earlier post where I forgot the code.
: >
: >I have included a portion of my code below. The problem that is driving me
: >crazy is that the first time thru the loop everything works fine but the
: >second time thru the loop the program just goes by the scanf statement. Could
: >someone please enlighten me as to what I am doing wrong.
: >
: >Since my original post I have come across a function called _flushall() that
: >seems to flush out the stdin buffer. Why don't either of my two Introduction
: >to C Programming books talk about flushall() or problems with buffers?
: >
: >int main(void)  
: >{
: >char y;
: >while (1)
: >  {
: >  printf("\nDo you want to input another string? (y/n)");

: You can't use printf() without a prototype.

You can use printf without a prototype because it returns an int, and
because compilers still must support K&R C. Of course it is better to use
prototypes and include stdio.h.

: >  scanf("%c",&y);
As above.

: You can't use scanf() without a prototype.  Also, this is
: very poor practice.  Using fgets() and sscanf() are much
: better.

You can use scanf() if you know how. I would not use it myself because I
just made a mistake when I was telling Robert how to use it myself.

: >  if (y=='n') break; : >    y='0';
: >    input();

: What is this?  There is no input() in the standard library.
How do you know input() is not a function written by Robert?
The only real bad thing here is that he would want to use a non-standard
function like flushall(), which incidentally you did not notice. :-)

: >    }
: >return 0;
: >}/* End Main */

: In the future, try compiling your code.

--
---------------------------------------------------------------------


Canada

No beast so fierce but knows some touch of pity
But I know none, And therefore am no beast
              Richard III., William Shakespeare
vim : the best editor
ftp.fu-berlin.de/misc/editors/vim/beta-test
---------------------------------------------------------------------



Tue, 22 Dec 1998 03:00:00 GMT  
 Newbie - scanf and while loop - correction


Quote:

>: You can't use printf() without a prototype.

>You can use printf without a prototype because it returns an int, and
>because compilers still must support K&R C.

Please don't post nonsense.  You can't use _any_ variadic function
without a prototype in scope.  printf is no exception.  Compilers are
NOT required to support K&R C code which is invoking undefined behaviour
according to the ANSI C standard.

"But it works on any compiler I've used" is not a valid argument in this
newsgroup.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Tue, 22 Dec 1998 03:00:00 GMT  
 Newbie - scanf and while loop - correction



: GE: : You can't use printf() without a prototype.
: GE:
: GE: You can use printf without a prototype because it returns an int, and
: GE: because compilers still must support K&R C. Of course it is better to use
: GE: prototypes and include stdio.h.
: GE:

: The standard specifically states that varargs functions *must* have
: prototypes in scope when they are called. So, printf needs a
: prototype.

: By the way, not all valid K&R I C is valid ANSI C. There were a few
: changes, and one has to adhere to them. For example, the following is
: perfectly valid K&R I C, but try compiling it on a truly ANSI C
: compiler!

: #include <stdio.h>
: int main() { printf("/Is everything okay??/"); return 0; }

You would expect this to generate something like unterminated string
constant. However, when I compile with gcc without the -ansi switch it
works fine. :-)

: Cheers
: Tanmoy
: --

: Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
: Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
: <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
: internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
: fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]

--
---------------------------------------------------------------------


Canada

No beast so fierce but knows some touch of pity
But I know none, And therefore am no beast
              Richard III., William Shakespeare
vim : the best editor
ftp.fu-berlin.de/misc/editors/vim/beta-test
---------------------------------------------------------------------



Tue, 22 Dec 1998 03:00:00 GMT  
 Newbie - scanf and while loop - correction



: >
: >: You can't use printf() without a prototype.
: >
: >You can use printf without a prototype because it returns an int, and
: >because compilers still must support K&R C.

: Please don't post nonsense.  You can't use _any_ variadic function
: without a prototype in scope.  printf is no exception.  Compilers are
: NOT required to support K&R C code which is invoking undefined behaviour
: according to the ANSI C standard.

: "But it works on any compiler I've used" is not a valid argument in this
: newsgroup.

I won't use that argument. In fact what I am about to say is not fact but
my personal view on this.
The prototype for printf in this case, without a prototype is
int printf()
That means that nothing should be assumed about the arguments to printf
by the compiler. Now this should work unless you do something really
boneheaded on a system where ints are 16 bit and longs are 32 bit and
pointers are 32 bit.
eg. printf("%p",0); could cause problems.
or printf("%ld",10); could also cause problems, I agree on that.
But scanf is variadic and should work unless you do not pass the address
of a variable which is wrong no matter how you look at it.
Of course I am not saying that you should not prototype or that you do
not include the appropriate headers. Also you would use varargs.h for
variadic functions in K&R C. Handling varargs the way they are now was
actually borrowed from C++. It was handled quite differently in K&R C.

: Dan
: --
: Dan Pop
: CERN, CN Division

: Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland

--
---------------------------------------------------------------------


Canada

No beast so fierce but knows some touch of pity
But I know none, And therefore am no beast
              Richard III., William Shakespeare
vim : the best editor
ftp.fu-berlin.de/misc/editors/vim/beta-test
---------------------------------------------------------------------



Tue, 22 Dec 1998 03:00:00 GMT  
 Newbie - scanf and while loop - correction


Quote:

>: By the way, not all valid K&R I C is valid ANSI C. There were a few
>: changes, and one has to adhere to them. For example, the following is
>: perfectly valid K&R I C, but try compiling it on a truly ANSI C
>: compiler!                                          ^^^^^^^^^^^^

>: #include <stdio.h>
>: int main() { printf("/Is everything okay??/"); return 0; }

>You would expect this to generate something like unterminated string
>constant. However, when I compile with gcc without the -ansi switch it
>works fine. :-)

Of course it works.  With no additional options, gcc compiles a language
named GNU C, which is neither a subset nor a superset of ANSI C.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Tue, 22 Dec 1998 03:00:00 GMT  
 Newbie - scanf and while loop - correction


Quote:



>: >
>: >: You can't use printf() without a prototype.
>: >
>: >You can use printf without a prototype because it returns an int, and
>: >because compilers still must support K&R C.

>: Please don't post nonsense.  You can't use _any_ variadic function
>: without a prototype in scope.  printf is no exception.  Compilers are
>: NOT required to support K&R C code which is invoking undefined behaviour

                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Quote:
>: according to the ANSI C standard.

>: "But it works on any compiler I've used" is not a valid argument in this
>: newsgroup.

>I won't use that argument. In fact what I am about to say is not fact but
>my personal view on this.
>The prototype for printf in this case, without a prototype is
>int printf()

This should read: "The default declaration in this case, without a
prototype in scope, is
int printf();"

Not every declaration is a prototype.

Quote:
>That means that nothing should be assumed about the arguments to printf
>by the compiler.

Wrong.  The compiler assumes that printf is a function with an unspecified,
but _constant_ number of arguments.

Quote:
>Now this should work unless you do something really
>boneheaded on a system where ints are 16 bit and longs are 32 bit and
>pointers are 32 bit.

This won't work on _any_ system where the compiler uses different argument
passing conventions for variadic functions vs ordinary functions.  This is
precisely why variadic functions CANNOT be called without a prototype in
scope.

Quote:
>eg. printf("%p",0); could cause problems.
>or printf("%ld",10); could also cause problems, I agree on that.

Both of these could ALWAYS cause problems, no matter which headers are
included or what are the sizes of various types, because the types of
the arguments don't match the format strings.  Which means UNDEFINED
BEHAVIOUR.

Quote:
>But scanf is variadic and should work unless you do not pass the address
>of a variable which is wrong no matter how you look at it.

Once you invoke undefined behaviour, the expression "should work" is plain
stupid.  And using scanf without a prototype in scope invokes undefined
behaviour.  What is so difficult to understand?

Quote:
>Of course I am not saying that you should not prototype or that you do
>not include the appropriate headers. Also you would use varargs.h for
>variadic functions in K&R C.

<varargs.h> is NOT a K&R C feature.

Quote:
>Handling varargs the way they are now was
>actually borrowed from C++. It was handled quite differently in K&R C.

It wasn't handled at all in K&R C.  K&R1 explicitly states that there is
no mechanism for user-defined variadic functions.  <varargs.h> is an
extension to K&R C, which was not universally supported.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Wed, 23 Dec 1998 03:00:00 GMT  
 Newbie - scanf and while loop - correction

Quote:



> : >You can use printf without a prototype because it returns an int, and
> : >because compilers still must support K&R C.
> : Please don't post nonsense.  You can't use _any_ variadic function
> : without a prototype in scope.  printf is no exception.  Compilers are
> : NOT required to support K&R C code which is invoking undefined behaviour
> : according to the ANSI C standard.
> : "But it works on any compiler I've used" is not a valid argument in this
> : newsgroup.
> I won't use that argument. In fact what I am about to say is not fact but
> my personal view on this. [...various arguments for not using variadic
> function prototypes...]

If we were discussing something other than a well-defined aspect of the
C Language, personal views may be appropriate. In this case, one simply
cannot argue or dispute the ISO/IEC 9899-1990 document. Both Mr. Pop
and Mr. Bhattacharya have already made the point -- but to support what
they correctly contend, simply refer to Section 7.1.7 "Use of library
functions" and this key sentence: "If a function that accepts a
variable number of arguments is not declared (explicitly or by
including its associated header), the behavior is undefined."

That is now (at least) the third time the point has been made in this
thread alone. Surely that is proof enough to bring closure to any
ill-advised debating otherwise.

--
=============================================================================

      Linux for fun, M$ for $$$...and the NFL for what really counts!
=============================================================================



Wed, 23 Dec 1998 03:00:00 GMT  
 Newbie - scanf and while loop - correction

Thank you everyone for your help. I just want to make it clear that I just
gave you the portion of my code where I thought the problem was (to keep the
posting small). In original code I did include stdio.h and input() was one of
my own functions. I have learned a lesson, next time I will post everything
relevant.



Quote:



writes:
>> : >You can use printf without a prototype because it returns an int, and
>> : >because compilers still must support K&R C.

>> : Please don't post nonsense.  You can't use _any_ variadic function
>> : without a prototype in scope.  printf is no exception.  Compilers are
>> : NOT required to support K&R C code which is invoking undefined behaviour
>> : according to the ANSI C standard.

>> : "But it works on any compiler I've used" is not a valid argument in this
>> : newsgroup.

>> I won't use that argument. In fact what I am about to say is not fact but
>> my personal view on this. [...various arguments for not using variadic
>> function prototypes...]

>If we were discussing something other than a well-defined aspect of the
>C Language, personal views may be appropriate. In this case, one simply
>cannot argue or dispute the ISO/IEC 9899-1990 document. Both Mr. Pop
>and Mr. Bhattacharya have already made the point -- but to support what
>they correctly contend, simply refer to Section 7.1.7 "Use of library
>functions" and this key sentence: "If a function that accepts a
>variable number of arguments is not declared (explicitly or by
>including its associated header), the behavior is undefined."

>That is now (at least) the third time the point has been made in this
>thread alone. Surely that is proof enough to bring closure to any
>ill-advised debating otherwise.



Fri, 25 Dec 1998 03:00:00 GMT  
 Newbie - scanf and while loop - correction



Quote:


> writes:


>: >
>: >: You can't use printf() without a prototype.
>: >
>: >You can use printf without a prototype because it returns an int, and
>: >because compilers still must support K&R C.

>: Please don't post nonsense.  You can't use _any_ variadic function
>: without a prototype in scope.  printf is no exception.  Compilers are
>: NOT required to support K&R C code which is invoking undefined behaviour
>: according to the ANSI C standard.

>: "But it works on any compiler I've used" is not a valid argument in this
>: newsgroup.

>I won't use that argument. In fact what I am about to say is not fact but
>my personal view on this.
>The prototype for printf in this case, without a prototype is
>int printf()

Minor nit - that is just a declaration. A prototype is where you
specify the types of the arguments or ...  The simplest prototyped
declaration for printf is:

int printf(const char *, ...);

Quote:
>That means that nothing should be assumed about the arguments to printf
>by the compiler.

No, int printf() declares printf as having an unknown but *fixed* number
of arguments. That is not compatible with its true definition.

The standard states that calling a function defined with a prototype
containing ... (such as the definition of printf in the standard) without
an appropriate prototype in scope at the point of call, results in undefined
behaviour.

Quote:
>Now this should work unless you do something really
>boneheaded on a system where ints are 16 bit and longs are 32 bit and
>pointers are 32 bit.

The problem is that the compiler can use a totally different calling
convention for fixed argument and variable argument functions. This
makes sense because if the compiler knows the argument number is fixed
(and hence known when compiling the called function) it can often generate
more efficient code. It is for similar reasons that, say, Pascal compilers
use different calling conventions because the function/procedure arguments
are fixed and they choose the more efficient calling mechanism.

The rules in the C language are clearly intended to permit a C compiler to
assime that all functions have fixed arguments except where it sees an
explicit prototype with a ...

Quote:
>eg. printf("%p",0); could cause problems.
>or printf("%ld",10); could also cause problems, I agree on that.

In those cases you are simply passing an argument of the wrong type.
In the first case you are calling the function using the wrong function
type. All result in undefined behaviour.

Quote:
>But scanf is variadic and should work unless you do not pass the address
>of a variable which is wrong no matter how you look at it.
>Of course I am not saying that you should not prototype or that you do
>not include the appropriate headers. Also you would use varargs.h for
>variadic functions in K&R C. Handling varargs the way they are now was
>actually borrowed from C++. It was handled quite differently in K&R C.

K&R1 has no support for variadic functions, notably not varargs.h

--
-----------------------------------------


-----------------------------------------



Sat, 02 Jan 1999 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Newbie - while loop & scanf problems

2. (Newbie) My Loop Isn't Looping - Aargh!

3. very simple newbie question with correction

4. While loop, scanf problem

5. do scanf() while loop problems

6. for loop with scanf,instead of 20repeats ->17repeats

7. terminating scanf in a loop?

8. : Read in n arguments with scanf without for loop

9. scanf at end of do-while loop

10. SCANF() NEWBIE

11. Newbie: Trouble with scanf - HELP!!!

12. Newbie with scanf question

 

 
Powered by phpBB® Forum Software