Solved

reading from stdin

Posted on 2000-03-24
14
241 Views
Last Modified: 2011-09-20
I wanted to retrieve a single character from stdin. (ie. the user strikes only one key and the function returns immediatly)
But I can't remember of any function that does it.
Is there any way to prevent the user from typing as much characters as he wants.
(I'm writing a console application in VC++. I have to use C only.)
0
Comment
Question by:Yo081699
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +2
14 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 2652712
#include <conio.h>
....
char c;
....
if(__kbhit()){
 c = _getch();
}
0
 

Author Comment

by:Yo081699
ID: 2653113
I don't like this solution because it relies on the console. Is it possible to achieve this using only the standard library? (which would make my code portable to any environment.)
0
 

Expert Comment

by:balakrishnan_t
ID: 2653138
#include "conio.h"
main()
{
 getch();
}

I hope this will satisfy ur need. wait for an keystroke...and return as soon as any key is pressed.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:marcjb
ID: 2653566
There is no way to do this using the C Standard.  getch() is not part of the Standard, but is offered on many systems and therefore may offer the portability you seek.

If you want to stick to the Standard, ask for user input, and require it to be ended with a CRLF (hitting enter/return).  Just make sure you have an array big enough to hold extra characters.

Hope this helps,

marc
0
 
LVL 32

Expert Comment

by:jhance
ID: 2653804
balakrishnan_t,

Thank you for copying my answer and posting it as your own!  

This is NOT how things are done here on Experts Exchange.
0
 
LVL 32

Expert Comment

by:jhance
ID: 2653820
Yo,

You asked:

"retrieve a single character from stdin"

STDIN implies console!

You also said:

"console application in VC++"

VC++ IMPLIES Microsoft WINDOWS!!!

If you want to do this from a Windows application which doesn't have a console, let "expert" balakrishnan_t answer your question.
0
 

Author Comment

by:Yo081699
ID: 2654132
balakrishnan_t, jhance is right! You didn't say anything more than him.
As it seems that it's the only solution available and that "many implementations provide getch" I'll take jhance answer.
0
 

Author Comment

by:Yo081699
ID: 2654152
JHANCE, please copy-paste again your answer. I'm not allowed to accept a previously rejected answer.
0
 
LVL 32

Accepted Solution

by:
jhance earned 50 total points
ID: 2654236
#include <conio.h>
.....
char c;
.....
if(_kbhit()){
 c = _getch();
}
0
 
LVL 32

Expert Comment

by:jhance
ID: 2654242
Yo,

Thanks for noticing that clown's tactics.

balakrishnan_t,

If you want to become a "real" expert, please come up with your own answers.  Copying someone elses answer to a given question, whether posted as an answer or a comment is very bad form indeed.
0
 

Expert Comment

by:jpjpjp
ID: 2654976
Actually, what bala said is a bit different, ie didnt put the if (kbhit())...

If getch() returns right away, no need to check if the kb was hit...

note that, u dont include a system file with "", but with <>...

So, you could use 2 ways, ie:

if (kbhit())
{
   c = getchar();
}

or

c = getch(); /* that is, if it returns right away.. */

I briefly looked at the man page of getch on unix, and i dont really like that function (not MT safe, doesnt like ESC key, etc..), so i would use the if(kbhit()) with the getchar()..

note that kbhit() doesnt exist on unix, but u can use it on windoze or dos..
0
 

Author Comment

by:Yo081699
ID: 2655374
Ok, thanks everybody. getch() will do.
0
 

Expert Comment

by:balakrishnan_t
ID: 2656831
jhance,

      I didn't copy ur answer... As  jpjpjp said, i thought my answer is a different from urs. If u don't know the different , please keep quiet, don't put unnecessary blaming on theres. Actually i feel that my answer works on under all platform while urs causes some problem under unix as jpjpjp said...So please mind ur words...

     
0
 

Expert Comment

by:balakrishnan_t
ID: 2656832
jhance,

      I didn't copy ur answer... As  jpjpjp said, i thought my answer is a different from urs. If u don't know the different , please keep quiet, don't put unnecessary blaming on theres. Actually i feel that my answer works on under all platform while urs causes some problem under unix as jpjpjp said...So please mind ur words...

     
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question