getchar can't be used twice?

J

Jonathan

Hi--

I have the following code:


#include <stdio.h>

char a,b;

int main()
{

printf("Which character is greater?\n");
printf("Type a single character:");
a=getchar();
printf("Type another character:");
b=getchar();

printf("The first character is %c. The second character is %c.\n", a,
b);

return 0;
}


But when run, it takes only the input for the first character, then
continues without pausing for the next input:


[Session started at 2006-04-13 13:31:15 -0700.]
Which character is greater?
Type a single character:5
Type another character:The first character is 5. The second character is
..


What am I doing wrong?

Best,
Jonathan
 
B

Bill Pursell

Jonathan said:
I have the following code:

#include <stdio.h>
char a,b;
int main()
{
printf("Which character is greater?\n");
printf("Type a single character:");
a=getchar();
printf("Type another character:");
b=getchar();

printf("The first character is %c. The second character is %c.\n", a,
b);

return 0;
}


But when run, it takes only the input for the first character, then
continues without pausing for the next input:

If the user types '5' and hits return, the first getchar() consumes the
'5', and the 2nd consumes the '\n'. A simple solution is to put an
extra getchar() right after the a=getchar() line to consume the '\n'.
 
K

Keith Thompson

Jonathan said:
I have the following code:


#include <stdio.h>

char a,b;

int main()
{

printf("Which character is greater?\n");
printf("Type a single character:");
a=getchar();
printf("Type another character:");
b=getchar();

printf("The first character is %c. The second character is %c.\n", a,
b);

return 0;
}

getchar() returns int, not char, so it can distinguish EOF from any
valud input character value. It doesn't matter much for this simple
program, but you should get into the habit of using it correctly.
But when run, it takes only the input for the first character, then
continues without pausing for the next input:


[Session started at 2006-04-13 13:31:15 -0700.]
Which character is greater?
Type a single character:5
Type another character:The first character is 5. The second character is
.


What am I doing wrong?

In response to the first prompt, you typed two characters, not one:
the '5' and a new-line. You assigned the value '5' to a, and the
value '\n' to b.

Try running the program again, typing "56" followed by a new-line;
you'll get '5' in a and '6' in b.

Read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
 
J

Jonathan

Jonathan said:
I have the following code:


#include <stdio.h>

char a,b;

int main()
{

printf("Which character is greater?\n");
printf("Type a single character:");
a=getchar();
printf("Type another character:");
b=getchar();

printf("The first character is %c. The second character is %c.\n", a,
b);

return 0;
}

getchar() returns int, not char, so it can distinguish EOF from any
valud input character value. It doesn't matter much for this simple
program, but you should get into the habit of using it correctly.
But when run, it takes only the input for the first character, then
continues without pausing for the next input:


[Session started at 2006-04-13 13:31:15 -0700.]
Which character is greater?
Type a single character:5
Type another character:The first character is 5. The second character is
.


What am I doing wrong?

In response to the first prompt, you typed two characters, not one:
the '5' and a new-line. You assigned the value '5' to a, and the
value '\n' to b.

Try running the program again, typing "56" followed by a new-line;
you'll get '5' in a and '6' in b.

Read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.


Yike. I tried reading that section and still don't understand this. If it's
the case that getchar is getting the \n as an input, wouldn't that mean that
"a" would be assigned to \n and "b" to 5, not the other way around?

In other words, why is it taking the \n after the a var is input? I don't
see where it's getting that from.

Best,
Jonathan
 
K

Keith Thompson

Jonathan said:
wrote on 4/13/06 1:52 PM:
Jonathan said:
I have the following code:


#include <stdio.h>

char a,b;

int main()
{

printf("Which character is greater?\n");
printf("Type a single character:");
a=getchar();
printf("Type another character:");
b=getchar();

printf("The first character is %c. The second character is %c.\n", a,
b);

return 0;
}

getchar() returns int, not char, so it can distinguish EOF from any
valud input character value. It doesn't matter much for this simple
program, but you should get into the habit of using it correctly.
But when run, it takes only the input for the first character, then
continues without pausing for the next input:


[Session started at 2006-04-13 13:31:15 -0700.]
Which character is greater?
Type a single character:5
Type another character:The first character is 5. The second character is
.


What am I doing wrong?

In response to the first prompt, you typed two characters, not one:
the '5' and a new-line. You assigned the value '5' to a, and the
value '\n' to b.

Try running the program again, typing "56" followed by a new-line;
you'll get '5' in a and '6' in b.

Read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.


Yike. I tried reading that section and still don't understand this. If it's
the case that getchar is getting the \n as an input, wouldn't that mean that
"a" would be assigned to \n and "b" to 5, not the other way around?

In other words, why is it taking the \n after the a var is input? I don't
see where it's getting that from.

They're just assigned in the order in which you entered them.

When you ran the program, you entered two characters, '5' and a
new-line. Your program called getchar() twice. The first call
returned the first character you entered, '5'; the second returned the
second character you entered, '\n'.

The confusing thing is that input is line-buffered. getchar() doesn't
(normally) read directly from the keyboard; instead, it works on top
of some implementation-specific lower level code. The low level code
reads an entire line of input, storing the '5' and '\n' characters
together in an internal buffer. getchar() grabs successive characters
from that buffer, waiting for more input when the buffer is empty.

If it weren't for this input buffering (and there are ways to turn it
off on some systems), typing the '5' would cause getchar() to return
the value '5' immediately, and you'd see the second prompt *before*
you hit <enter>. The prompts would be interleaved with your input
differently, but you'd still get the same results for the same input;
typing "5<ENTER>" would still store '5' in a and '\n' in b.
 
F

Flash Gordon

Jonathan said:
Jonathan said:
I have the following code:


#include <stdio.h>

char a,b;

int main()
{

printf("Which character is greater?\n");
printf("Type a single character:");
a=getchar();
printf("Type another character:");
b=getchar();

printf("The first character is %c. The second character is %c.\n", a,
b);

return 0;
}
getchar() returns int, not char, so it can distinguish EOF from any
valud input character value. It doesn't matter much for this simple
program, but you should get into the habit of using it correctly.
But when run, it takes only the input for the first character, then
continues without pausing for the next input:


[Session started at 2006-04-13 13:31:15 -0700.]
Which character is greater?
Type a single character:5
Type another character:The first character is 5. The second character is
.


What am I doing wrong?
In response to the first prompt, you typed two characters, not one:
the '5' and a new-line. You assigned the value '5' to a, and the
value '\n' to b.

Try running the program again, typing "56" followed by a new-line;
you'll get '5' in a and '6' in b.

Read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.


Yike. I tried reading that section and still don't understand this. If it's
the case that getchar is getting the \n as an input, wouldn't that mean that
"a" would be assigned to \n and "b" to 5, not the other way around?

In other words, why is it taking the \n after the a var is input? I don't
see where it's getting that from.

What order are you pressing keys in? Do you press return first then 5 or
5 first then return? Do you think getchar should return characters in
the order you enter them or in a different order?
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top