skipped printf/scanf ??? 
Author Message
 skipped printf/scanf ???

I continue to run into the following problem;

I need to get user input multiple times, once in the beginning of the
code and again after a do/while/for loop and MY method and thinking
doesn't work as expected.

what usually happens(with my P... poor code) is the user is
prompted(printf/scanf) for input, the code continues into the
do/while/for loop and the section of code that prompts the user is
bypassed (as if a value has already been passed to it's scanf) and any
expressions/functions/tests are performed (the ones within the loop)
once, where at that point the user is finally prompted for input.
What am I missing ? Here's a simple .c the shows what I'm talking about.
Why is the (second(/*remarked*/))printf/scanf skipped the first time
thru ?
...and/or what's a better technique for multiple prompting ?

yes, I'm new to this.

#include <stdio.h>

static int count = 0;



Fri, 19 Jan 2001 03:00:00 GMT  
 skipped printf/scanf ???
I think you made on of the most known mistakes. Try to always put a
getchar(); after your scanf to get the return when the user presses the
keyboard key. Otherwise the input will be scanned with scanf, but the return
will be scanned with whatever other scan function that follows somewhere in
your code and that function will be over.

--

http://www.sin.khk.be/~emulov/index_english.htm
computer-internet-programming-HTML-programupload
--
READ THIS: Anything I say I say for myself, I DON'T
want ANY nagging afterwards!!!!


:I continue to run into the following problem;
:
:I need to get user input multiple times, once in the beginning of the
:code and again after a do/while/for loop and MY method and thinking
:doesn't work as expected.
:
:what usually happens(with my P... poor code) is the user is
:prompted(printf/scanf) for input, the code continues into the
:do/while/for loop and the section of code that prompts the user is
:bypassed (as if a value has already been passed to it's scanf) and any
:expressions/functions/tests are performed (the ones within the loop)
:once, where at that point the user is finally prompted for input.
:What am I missing ? Here's a simple .c the shows what I'm talking about.
:Why is the (second(/*remarked*/))printf/scanf skipped the first time
:thru ?
:...and/or what's a better technique for multiple prompting ?
:
:yes, I'm new to this.
:
:#include <stdio.h>
:
:static int count = 0;



Sat, 20 Jan 2001 03:00:00 GMT  
 skipped printf/scanf ???

Quote:

> I think you made on of the most known mistakes. Try to always put a
> getchar(); after your scanf to get the return when the user presses the
> keyboard key. Otherwise the input will be scanned with scanf, but the return
> will be scanned with whatever other scan function that follows somewhere in
> your code and that function will be over.

This sortof hints into the right direction, but the reason giving
is a bit unclear ;-)

Usually it is best to simply *not* use "scanf()" at all, because
it is a very tricky and difficult function. It has *lots* of
hidden traps and gotchas and they are deceptively non-obvious.
It apears to be easy to use, but it is *not* !

What seems to be happening in this specific case is that "scanf()"
will typically *not* read the "\n" at the end of user input when
used trivially, like eg. "scanf("%d",&i);". The next call to
another input funtion will find this "\n" that "scanf()" did not
read, and will use it as the current user input. Functions like
"getchar()" or "fgets()" will return immediately.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sat, 20 Jan 2001 03:00:00 GMT  
 skipped printf/scanf ???

Quote:

>I think you made on of the most known mistakes. Try to always put a
>getchar(); after your scanf to get the return when the user presses the
>keyboard key.

A generally better approach is a combination of fgets() and sscanf().

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


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



Sat, 20 Jan 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. scanf() skipped

2. Why: Loop with printf() skips getchar() ?

3. questions about sprintf, sscanf, printf, scanf, fscanf, fprintf!

4. Simple scanf/printf question

5. tiny question on format of scanf and printf

6. Examples of format strings for scanf/printf ?

7. scanf/printf conversions

8. pointers in scanf and printf

9. Beginner question - printf and scanf

10. printf() and scanf()

11. casting printf/scanf

12. Q:printf() and scanf()....

 

 
Powered by phpBB® Forum Software