473,803 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getchar can't be used twice?

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

Apr 13 '06 #1
5 8758

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

Apr 13 '06 #2
Jonathan <rl******@nospa m.us> 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_Keit h) ks***@mib.org <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.
Apr 13 '06 #3

in article ln************@ nuthaus.mib.org, Keith Thompson at ks***@mib.org
wrote on 4/13/06 1:52 PM:
Jonathan <rl******@nospa m.us> 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

Apr 13 '06 #4
Jonathan <rl******@nospa m.us> writes:
in article ln************@ nuthaus.mib.org, Keith Thompson at ks***@mib.org
wrote on 4/13/06 1:52 PM:
Jonathan <rl******@nospa m.us> 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_Keit h) ks***@mib.org <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.
Apr 13 '06 #5
Jonathan wrote:


in article ln************@ nuthaus.mib.org, Keith Thompson at ks***@mib.org
wrote on 4/13/06 1:52 PM:
Jonathan <rl******@nospa m.us> 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
Apr 13 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2245
by: Cam | last post by:
Hi everyone, Before I answer to a (hopefully) helpful reply to this post, I have been rapped over the knuckles for 'top-posting' and I do not wish to be a learner poster who observes poor netiquette as I am a firm believer in consideration .... to this end, could somebody please tell me how I reply to a message without top-posting? Do I need to have my original post selected when I hit the 'reply' button? Here's my question (and...
6
1916
by: Luke Wu | last post by:
Whenever one runs across programs that use the return value from getchar() to read input, it's almost always accepted into an int-defined variable. I've read explanations on this and have always used this form. Recently, someone asked me why an int was needed, since -1 (usually EOF) can be safely represented in a signed char value. In fact, any negative number can be safely represented in a signed char value, and wouldn't conflict with...
17
4825
by: Martin Jørgensen | last post by:
Hi, Since I'm a newbie I have some small but quick (probably) stupid questions also :-) This is my "get_double" function which takes a default argument also of type double. The function returns a value of type double so I can write something like value = getdouble(0.5); in my program. --------------
20
2659
by: blufox | last post by:
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');
5
326
by: CDROOM | last post by:
i have this qussion how getchar used , where it used and when it used. please reply quickly,
25
5452
by: ehabaziz2001 | last post by:
Why I can not begin my subscript of character arrrays with 0. In this program I can not do : do { na=getchar(); i++; na=getchar(); } while (na!='\n');
14
2867
by: arnuld | last post by:
i have slightly modified the programme from section 1.5.1 which takes the input frm keyboard and then prints that to the terminal. it just does not run and i am unable to understand the error message. may you tell me what is wrong and how to make that right ? ------------------------------ INPUT --------------------- #include <stdio.h> int main() {
12
4726
by: arnuld | last post by:
#include <stdio.h> int main() { printf("%d\n", getchar() != EOF); return 0; } when i run this programme, no matter what i give it as input, output
20
636
by: Senthil-Raja | last post by:
The getchar() function is expected to fetch the next character in the input stream and return it. But, when I wrote a program using this function, it looks like the reading of the input stream happens only after I press the ENTER key. Please someone explain why this is happening. Regards, Senthil-Raja.
0
9565
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10550
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.