getchar() behaviour on gcc 4.1

B

blufox

This is a simple code snippet i used ...
#include <stdio.h>
int main(void)
{
int ch;
do {
printf("still yes...\n");
printf("\nenter choice :: ");
ch = getchar();
}while(ch == 'y');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?
 
F

Flash Gordon

blufox said:
This is a simple code snippet i used ...
#include <stdio.h>
int main(void)
{
int ch;
do {
printf("still yes...\n");
printf("\nenter choice :: ");

fflush(stdout);
/* Otherwise there is no guarantee the output will be displayed before
waiting for input. Never heard of line buffering? */
ch = getchar();
}while(ch == 'y');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?

Yes. How many keys did you press at the first prompt? I'll bet it was
more than one...

Generally it is better to use fgets (not gets, NEVER gets) to read a
complete line and then process it. It is far simpler to get it right
that way.
 
R

ram

your input 'y' is equal to the condition i.e the condition becomes true
,so it will again ask u the input.please note the only when the
condition fails the loop will terminate.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

blufox said:
This is a simple code snippet i used ...
#include <stdio.h>
int main(void)
{
int ch;
do {
printf("still yes...\n");
printf("\nenter choice :: ");
ch = getchar();
}while(ch == 'y');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?
Consider what happens when you inputted y
You hit y , then you likely hit the enter key. The first getchar
call reads the 'y'. The next might read the enter key-hit as a '\n'
 
B

blufox

- Show quoted text -
blufox said:
This is a simple code snippet i used ...
#include <stdio.h>
int main(void)
{
int ch;
do {
printf("still yes...\n");
printf("\nenter choice :: ");
ch = getchar();
}while(ch == 'y');
return 0;
}
Output ::
[rautela@blufox c]$ ./a.out
still yes...
enter choice :: y
still yes...
[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?

Consider what happens when you inputted y
You hit y , then you likely hit the enter key. The first getchar
call reads the 'y'. The next might read the enter key-hit as a '\n'

got it. Will try to fix it.
Thank you
 
D

Duncan Muirhead

This is a simple code snippet i used ...
#include <stdio.h>
int main(void)
{
int ch;
do {
printf("still yes...\n");
printf("\nenter choice :: ");
ch = getchar();
}while(ch == 'y');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?
if you add "printf( "--- %d --- \n",ch);" just before the
end of the loop you'll see whats happening:
When you typed 'y' you also hit enter. The second call to getchar()
returns 10, ie '\n' and the loop exits.
Duncan
 
P

Pedro Graca

blufox said:
This is a simple code snippet i used ...
#include <stdio.h>
int main(void)
{
int ch;
do {
printf("still yes...\n");
printf("\nenter choice :: ");

fflush(stdout); /* so that you are guaranteed
to see the output before
getchar() executes */
ch = getchar();
}while(ch == 'y');

/* Try this */
printf("do loop exited with ch == '%c' (value == %d).\n", ch, ch);
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?

Did you just press 'y' ... or was it 'y'<ENTER>?
 
C

Chang Feng

It's right.
You can print out the value of ch after calling getchar.
The code like this:
int main(int argc, char* argv[])
{
int ch;
do
{
printf("still yes...\n");
printf("\nenter choice :: ");
ch = getchar();
printf("ch='%c'\n",ch);
}while(ch == 'y');
return 0;

}

and the output will be:
still yes...

enter choice :: y
ch='y'
still yes...

enter choice :: ch='
'

Nils O. Selåsdal said:
blufox said:
This is a simple code snippet i used ...
#include <stdio.h>
int main(void)
{
int ch;
do {
printf("still yes...\n");
printf("\nenter choice :: ");
ch = getchar();
}while(ch == 'y');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?
Consider what happens when you inputted y
You hit y , then you likely hit the enter key. The first getchar
call reads the 'y'. The next might read the enter key-hit as a '\n'
 
I

Ivanna Pee

blufox said:
This is a simple code snippet i used ...
#include <stdio.h>
int main(void)
{
int ch;
do {
printf("still yes...\n");
printf("\nenter choice :: ");
ch = getchar();
}while(ch == 'y');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?

Describe to your self in English(or your native language) what it is
you are trying to accomplish.

Then describe to your self in English(or your native language) what it
is the C code is in fact accomplishing.

You shall see that the C logic prevails.
 
I

Ivanna Pee

Nils said:
blufox said:
This is a simple code snippet i used ...
#include <stdio.h>
int main(void)
{
int ch;
do {
printf("still yes...\n");
printf("\nenter choice :: ");
ch = getchar();
}while(ch == 'y');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?
Consider what happens when you inputted y
You hit y , then you likely hit the enter key. The first getchar
call reads the 'y'. The next might read the enter key-hit as a '\n'

You are a detriment to this group.
 
I

Ivanna Pee

Flash said:
blufox said:
This is a simple code snippet i used ...
#include <stdio.h>
int main(void)
{
int ch;
do {
printf("still yes...\n");
printf("\nenter choice :: ");

fflush(stdout);
/* Otherwise there is no guarantee the output will be displayed before
waiting for input. Never heard of line buffering? */
ch = getchar();
}while(ch == 'y');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?

Yes. How many keys did you press at the first prompt? I'll bet it was
more than one...

Generally it is better to use fgets (not gets, NEVER gets) to read a
complete line and then process it. It is far simpler to get it right
that way.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

You are a detriment to this group.
 
C

CBFalconer

ram said:
your input 'y' is equal to the condition i.e the condition becomes true
,so it will again ask u the input.please note the only when the
condition fails the loop will terminate.

Who is 'your', who is 'u'. What loop? Without proper context your
article is meaningless. Google is NOT usenet, it is only a poor
excuse for an interface to usenet. There is no reason to assume
that your reader has ever seen, or will ever see, any other
messages. Thus an article has to stand by itself. You do this by
including context, snipped to material germane to your reply. For
means of doing this, even on google, see my sig. below. Please
also read the reference URLs.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
C

CBFalconer

Flash said:
blufox said:
This is a simple code snippet i used ...
#include <stdio.h>
int main(void)
{
int ch;
do {
printf("still yes...\n");
printf("\nenter choice :: ");

fflush(stdout);
/* Otherwise there is no guarantee the output will be displayed
before waiting for input. Never heard of line buffering? */
ch = getchar();
}while(ch == 'y');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?

Yes. How many keys did you press at the first prompt? I'll bet it was
more than one...

Generally it is better to use fgets (not gets, NEVER gets) to read a
complete line and then process it. It is far simpler to get it right
that way.

Using fgets or the equivalent requires a buffer, and checking for
line completion etc. A more positive way is to insist on an
'enter' as a response to the wait, and handle it with a call to:

flushln(stdin);

in place of the getchar() call. The flushln routine will be useful
in many places, and should be:

int flushln(FILE *f) {
int ch;
while ((EOF != (ch = getc(f))) && (ch != '\n')) continue;
return ch;
}

No buffer, or other storage is required. Now, if you want a
specific single non-blank character response, you can use:

int getreply(FILE *f) {
int ch;
while (' ' == (ch = getc(f))) continue;
if ('\n' != ch) flushln(f);
return ch;
}

Note that all these things can return EOF. A few such routines in
your toolbox go a long ways to keeping interactive input clean.
The routines are more useful when the input file is parametized.
Note that getc(stdin) is equivalent to getchar().

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
R

Robert Gamble

Nils said:
Ivanna said:
Nils said:
blufox wrote:
This is a simple code snippet i used ...
#include <stdio.h>
int main(void)
{
int ch;
do {
printf("still yes...\n");
printf("\nenter choice :: ");
ch = getchar();
}while(ch == 'y');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?
Consider what happens when you inputted y
You hit y , then you likely hit the enter key. The first getchar
call reads the 'y'. The next might read the enter key-hit as a '\n'

In another post to this thread you said;
"Describe to your self in English(or your native language) what it is
you are trying to accomplish."
Perhaps you should take your own advice, and try to explain the reasoning
behind:
You are a detriment to this group.

Based on his other posts, he appears to be a troll who takes objection
to people providing useful information to posters in the form of direct
answers preferring instead that people provide "hints" that still
require the questioner to figure out the problem themselves. It would
probably be best to ignore his posts.

Robert Gamble
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Ivanna said:
Nils said:
blufox said:
This is a simple code snippet i used ...
#include <stdio.h>
int main(void)
{
int ch;
do {
printf("still yes...\n");
printf("\nenter choice :: ");
ch = getchar();
}while(ch == 'y');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?
Consider what happens when you inputted y
You hit y , then you likely hit the enter key. The first getchar
call reads the 'y'. The next might read the enter key-hit as a '\n'

In another post to this thread you said;
"Describe to your self in English(or your native language) what it is
you are trying to accomplish."
Perhaps you should take your own advice, and try to explain the reasoning
behind:
 
V

Vladimir S. Oka

Robert Gamble opined:
Nils said:
Ivanna said:
Nils O. Selåsdal wrote:
blufox wrote:
This is a simple code snippet i used ...
#include <stdio.h>
int main(void)
{
int ch;
do {
printf("still yes...\n");
printf("\nenter choice :: ");
ch = getchar();
}while(ch == 'y');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?
Consider what happens when you inputted y
You hit y , then you likely hit the enter key. The first getchar
call reads the 'y'. The next might read the enter key-hit as a
'\n'

In another post to this thread you said;
"Describe to your self in English(or your native language) what it
is you are trying to accomplish."
Perhaps you should take your own advice, and try to explain the
reasoning behind:
You are a detriment to this group.

Based on his other posts, he appears to be a troll who takes
objection to people providing useful information to posters in the
form of direct answers preferring instead that people provide "hints"
that still
require the questioner to figure out the problem themselves. It
would probably be best to ignore his posts.

In my native language (after spelling is abstracted) he'd be a she...
It'd be the first she-troll I encountered.
 
K

Keith Thompson

Vladimir S. Oka said:
Robert Gamble opined: [...]
Based on his other posts, he appears to be a troll who takes
objection to people providing useful information to posters in the
form of direct answers preferring instead that people provide "hints"
that still
require the questioner to figure out the problem themselves. It
would probably be best to ignore his posts.

In my native language (after spelling is abstracted) he'd be a she...
It'd be the first she-troll I encountered.

Not that it matters, but "Ivanna Pee" is almost certainly not this
person's real name; the person behind the alias could as easily be
male as female. ("Ivanna" --> "I wanna" --> "I want to"; "pee" is
slang for "urinate".)
 
V

Vladimir S. Oka

Keith Thompson opined:
Vladimir S. Oka said:
Robert Gamble opined: [...]
Based on his other posts, he appears to be a troll who takes
objection to people providing useful information to posters in the
form of direct answers preferring instead that people provide
"hints" that still
require the questioner to figure out the problem themselves. It
would probably be best to ignore his posts.

In my native language (after spelling is abstracted) he'd be a
she... It'd be the first she-troll I encountered.

Not that it matters, but "Ivanna Pee" is almost certainly not this
person's real name; the person behind the alias could as easily be
male as female. ("Ivanna" --> "I wanna" --> "I want to"; "pee" is
slang for "urinate".)

Ah, now it becomes clear. I should have got it earlier...

PS
I made sure limericks stay out of my sig. They seem to have got around
my request not to include offensive fortunes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top