Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > getchar can't be used twice?

Reply
Thread Tools

getchar can't be used twice?

 
 
Jonathan
Guest
Posts: n/a
 
      04-13-2006
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

 
Reply With Quote
 
 
 
 
Bill Pursell
Guest
Posts: n/a
 
      04-13-2006

Jonathan wrote:

> 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'.

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      04-13-2006
Jonathan <(E-Mail Removed)> writes:
> 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/>.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Jonathan
Guest
Posts: n/a
 
      04-13-2006



in article (E-Mail Removed), Keith Thompson at (E-Mail Removed)
wrote on 4/13/06 1:52 PM:

> Jonathan <(E-Mail Removed)> writes:
>> 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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-13-2006
Jonathan <(E-Mail Removed)> writes:
> in article (E-Mail Removed), Keith Thompson at (E-Mail Removed)
> wrote on 4/13/06 1:52 PM:
>> Jonathan <(E-Mail Removed)> writes:
>>> 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.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      04-13-2006
Jonathan wrote:
>
>
> in article (E-Mail Removed), Keith Thompson at (E-Mail Removed)
> wrote on 4/13/06 1:52 PM:
>
>> Jonathan <(E-Mail Removed)> writes:
>>> 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?
--
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
scanf/getchar sequence problem clusardi2k@aol.com C++ 21 04-20-2005 09:50 AM
getchar vs. cin Kristo C++ 1 03-24-2005 08:25 PM
Use of getchar Cam C++ 3 05-30-2004 11:39 AM
Re: getchar returns int, assign to array of char? Ben Fitzgerald C Programming 1 06-28-2003 02:24 AM
Re: getchar returns int, assign to array of char? Ben Fitzgerald C Programming 9 06-27-2003 07:04 PM



Advertisments