Why: Loop with printf() skips getchar() ? 
Author Message
 Why: Loop with printf() skips getchar() ?

Greetings!
After the first loop, this code increments and executes printf() 2x before
doing getchar() on each successive loop. Any ideas why?

#include <stdio.h>

main()
  {
   int i, j, ch;

      for (i=0, j=1, ch=' '; ch!='x'; i++, j++)
      {
         printf("%d + %d = %d. Enter "x" to terminate loop. \n",i, j, i+j);
         ch=getchar();
      }

   printf("\nLoop exited.");

   result=0;

   }

Thx for any ideas---
--pp



Sun, 26 Aug 2001 03:00:00 GMT  
 Why: Loop with printf() skips getchar() ?

Quote:

> After the first loop, this code increments and executes printf() 2x before
> doing getchar() on each successive loop. Any ideas why?

> #include <stdio.h>

> main()
>   {
>    int i, j, ch;

>       for (i=0, j=1, ch=' '; ch!='x'; i++, j++)
>       {
>          printf("%d + %d = %d. Enter "x" to terminate loop. \n",i, j, i+j);
>          ch=getchar();
>       }

>    printf("\nLoop exited.");

>    result=0;

>    }

After compiling it under Linux with gcc, I had the following errors:
    test.c: In function `main':
    test.c:9: parse error before `x'
    test.c:15: `result' undeclared (first use this function)
    test.c:15: (Each undeclared identifier is reported only once
    test.c:15: for each function it appears in.)
So I declared result as an int, and I added backslashes before the "x" in the
printf. And here is the result:
    (moses) [pprodhon] ~ > gcc -Wall test.c
    (moses) [pprodhon] ~ > a.out
    0 + 1 = 1. Enter "x" to terminate loop.
    x

    Loop exited.(moses) [pprodhon] ~ >
Where's the problem??? Maybe you didn't use -Wall to get all the warnings
(though these are errors, not warnings...)
Hope this helps.



Sun, 26 Aug 2001 03:00:00 GMT  
 Why: Loop with printf() skips getchar() ?
I think this is what happened. He wrote a core program, which did the loop
wrong. He made some changes and fixed the loop problem, but introduced a
compiler error (the result thing). Then he either didn't recompile, or did
recompile but got an error which he didn't notice, so he was running the
old binary.

--
Richard H

#include "sig.h"



Quote:

> > After the first loop, this code increments and executes printf() 2x
before
> > doing getchar() on each successive loop. Any ideas why?

> > #include <stdio.h>

> > main()
> >   {
> >    int i, j, ch;

> >       for (i=0, j=1, ch=' '; ch!='x'; i++, j++)
> >       {
> >          printf("%d + %d = %d. Enter "x" to terminate loop. \n",i, j,
i+j);
> >          ch=getchar();
> >       }

> >    printf("\nLoop exited.");

> >    result=0;

> >    }

> After compiling it under Linux with gcc, I had the following errors:
>     test.c: In function `main':
>     test.c:9: parse error before `x'
>     test.c:15: `result' undeclared (first use this function)
>     test.c:15: (Each undeclared identifier is reported only once
>     test.c:15: for each function it appears in.)
> So I declared result as an int, and I added backslashes before the "x" in
the
> printf. And here is the result:
>     (moses) [pprodhon] ~ > gcc -Wall test.c
>     (moses) [pprodhon] ~ > a.out
>     0 + 1 = 1. Enter "x" to terminate loop.
>     x

>     Loop exited.(moses) [pprodhon] ~ >
> Where's the problem??? Maybe you didn't use -Wall to get all the warnings
> (though these are errors, not warnings...)
> Hope this helps.



Sun, 26 Aug 2001 03:00:00 GMT  
 Why: Loop with printf() skips getchar() ?
Because you have to enter (1) a character, and (2) a linefeed, to make your
entry. Each of these characters is accepted by calls to getchar(), one after
the other.

The question is not what behavior your code produces -- that is obvious. The
question is what behavior you hoped for.
--

Paul Lutus
www.arachnoid.com

Quote:

>Greetings!
>After the first loop, this code increments and executes printf() 2x before
>doing getchar() on each successive loop. Any ideas why?

>#include <stdio.h>

>main()

>   int i, j, ch;

>      for (i=0, j=1, ch=' '; ch!='x'; i++, j++)
>      {
>         printf("%d + %d = %d. Enter "x" to terminate loop. \n",i, j, i+j);
>         ch=getchar();
>      }

>   printf("\nLoop exited.");

>   result=0;

>   }

>Thx for any ideas---
>--pp



Sun, 26 Aug 2001 03:00:00 GMT  
 Why: Loop with printf() skips getchar() ?
Richard Heathfield schrieb:

Quote:

> I think this is what happened. He wrote a core program, which did the loop
> wrong. He made some changes and fixed the loop problem, but introduced a
> compiler error (the result thing). Then he either didn't recompile, or did
> recompile but got an error which he didn't notice, so he was running the
> old binary.

Interesting and complex explanation, but not quite on target :-)

You (and Zoub) both overlooked that the error is still in the
code, even after correcting the anoying typos. It becomes obvious
when you enter something different than 'x'. Anything except an empty
<Return> will give you one loop iteration per character, because
"getchar()" only read *one* character from <stdin>, but nevertheless
waits until <Return> has been pressed.

One solution might be to use "fgets()" instead of "getchar()". Another
might be to call "getchar()" in a loop, to read everything up to and
including the '\n':
   while ( (ch=getchar) != EOF && ch != '\n' && ch != 'x' );

Stephan
(initiator of the campaign against grumpiness in c.l.c)
(-: A brandnew excellent FAQ version has been released !!! :-)
(-: Get it: http://www.eskimo.com/~scs/C-faq/versions.html :-)



Sun, 26 Aug 2001 03:00:00 GMT  
 Why: Loop with printf() skips getchar() ?

Quote:

> Greetings!
> After the first loop, this code increments and executes printf() 2x before
> doing getchar() on each successive loop. Any ideas why?

You'll find my explanation and a solution in my other reply in
this thread (I replied to Richard Heathfiled who replied to Zoub).

Stephan
(initiator of the campaign against grumpiness in c.l.c)
(-: A brandnew excellent FAQ version has been released !!! :-)
(-: Get it: http://www.eskimo.com/~scs/C-faq/versions.html :-)



Sun, 26 Aug 2001 03:00:00 GMT  
 Why: Loop with printf() skips getchar() ?

00:00:26 -0600 in comp.lang.c.
Why: Loop with printf() skips getchar() ?'s a cool scene! Dig it!

Quote:
>main()

  In addition to what others have told you, you have another problem.
The main() function must return an int. Your definition of main() here
defaults to type int, since you didn't provide one explicitly.
However, the next version of the standard won't allow this, so it's
best to explicitly type this function.

int main(void)

Quote:
>  {

  And, since main() is of type int, you should return an int from
main(). 0, EXIT_SUCCESS and EXIT_FAILURE (as defined in stdlib.h) are
portable values that can be returned from main().

  return 0;

Quote:
>   }

--

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============



Sat, 01 Sep 2001 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. getchar() causes for loop to skip every other...WHY???

2. skipped printf/scanf ???

3. little puzzle with printf, scanf and getchar

4. VC6 printf bug for while(ch=getchar()) != EOF)

5. printf error with while(ch=getchar())=eof)

6. Getchar and loop

7. Q: Why does my code skip gets()

8. Why getchar () echoes to the screen?

9. when an infinite loop after printf

10. Help with printf and for loop

11. Why is the problem skipping the if else statement on the second loop?

12. replacing printf with my printf, then calling C lib's printf

 

 
Powered by phpBB® Forum Software