473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

getchar() function

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 12471
mattmao
121 100+
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
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 100+
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
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...
12
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...
1
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...
6
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...
11
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...
11
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()...
25
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
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...

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.