473,834 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getchar() function

35 New Member
Hello,
I have a code which uses getchar().
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main()
  3. {
  4.         char ch;
  5.         ch = getchar();
  6.         if(ch == 'Y')
  7.         {
  8.                 printf("Beautiful World\n");
  9.         }
  10.         else
  11.         {
  12.                 printf("Cruel World\n");
  13.         }
  14.         ch = getchar();
  15.         if(ch == 'N')
  16.         {
  17.                 printf("Second getchar\n");
  18.         }
  19.         else
  20.         {
  21.                 printf("Second getchar else part\n");
  22.         }
  23.         return 0;
  24. }

and the o/p is
Beautiful World
Second getchar else part

the second getchar() is not waiting to enter the character from the user....
Oct 25 '07 #1
3 12508
mattmao
121 New Member
Hi, you need to clear keyboard buffer before asking the user to inter the second char, for example:

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main()
  3. {
  4. char ch;
  5. ch = getchar();
  6. if(ch == 'Y')
  7. {
  8. printf("Beautiful World\n");
  9. }
  10. else
  11. {
  12. printf("Cruel World\n");
  13. }
  14. while(getchar()!='\n');
  15. ch = getchar();
  16. if(ch == 'N')
  17. {
  18. printf("Second getchar\n");
  19. }
  20. else
  21. {
  22. printf("Second getchar else part\n");
  23. }
  24. return 0;
  25. }

Note this line of code:
while(getchar() !='\n');

It does this job for you. Now I hope this code works as you intended to.
Oct 25 '07 #2
mahiapkum
35 New Member
could u pls explain the line "while(getchar( )!='\n');" the code is working fine but i didnt understand how the above line of code works.

Hi, you need to clear keyboard buffer before asking the user to inter the second char, for example:

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main()
  3. {
  4. char ch;
  5. ch = getchar();
  6. if(ch == 'Y')
  7. {
  8. printf("Beautiful World\n");
  9. }
  10. else
  11. {
  12. printf("Cruel World\n");
  13. }
  14. while(getchar()!='\n');
  15. ch = getchar();
  16. if(ch == 'N')
  17. {
  18. printf("Second getchar\n");
  19. }
  20. else
  21. {
  22. printf("Second getchar else part\n");
  23. }
  24. return 0;
  25. }

Note this line of code:
while(getchar() !='\n');

It does this job for you. Now I hope this code works as you intended to.
Oct 25 '07 #3
rhitam30111985
112 New Member
could u pls explain the line "while(getchar( )!='\n');" the code is working fine but i didnt understand how the above line of code works.
program wont get out of the while loop until user presses the 'enter' key which is nothing but the newline character('\n') ..once press enter.. it will take the next character.
Oct 25 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2250
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...
12
4099
by: Emmanuel Delahaye | last post by:
Hi there, It is commonly accepted that a call to the getchar() function suspends the execution of the current program. I have not found any description of this behaviour in the standard (I may have missed it). I was wondering wether it was just a 'common' behaviour of most systems, and wether the hit of the ENTER key was mandatory. Thanks for having read me.
1
3725
by: White Spirit | last post by:
I'm trying to use getchar() to read alphanumeric data as follows:- char input; /* Take a string of input and remove all spaces therein */ int j = 0; while ((input = getchar()) != '\n') { if (!isspace(input)) j++;
6
5288
by: Alan | last post by:
I am using Standard C compiled with GCC under Linux Fedora Core 4 When I run this program and enter a character at the prompt, I have to press the ENTER key as well. This gives me 2 input characters - 'a' and '\n' (Hex 61 and 0a) It seems as though the getchar() function needs ENTER to terminate reading stdin. I am trying to get the program to respond when I press one key only (ie
11
2507
by: shekhardeodhar | last post by:
The program compiles properly (most of it is from k&r) but the second function (here character_count) gives wrong answer. Can someone please explain why ? #include<stdio.h> #define IN 1 #define OUT 0 int word_count();
11
4486
by: Michael Brennan | last post by:
If a call to getchar() returns EOF, is the behavior defined if another getchar() call is made? Will it return EOF again? The reason I'm asking: if I have a function that uses getchar() repeatedly, and it returns EOF after some bytes, do I need the function to handle the EOF right there, or, can I just wait until the next time my function executes, and handle it there, if EOF will be returned by the first call to getchar().
25
5456
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');
9
5154
by: primeSo | last post by:
// FIRST int main(void){ int c, i = 0; char cArray; while( (c = getchar()) != '\n' && c != EOF){ cArray = c; i ++; }
22
3615
by: arnuld | last post by:
Mostly when I want to take input from stdin I use getchar() but I get this from man page itself: "If the integer value returned by getchar() is stored into a variable of type char and then compared against the integer constant EOF, the comparison may never succeed, because sign-extension of a variable of type char on widening to integer is implementation-defined" while( EOF != (ch = getchar()) ) ....
0
9799
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10793
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
10219
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
9331
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
7758
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
5627
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5794
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4427
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
3978
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.