• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

reading from stdin

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
Yo081699
Asked:
Yo081699
  • 5
  • 4
  • 3
  • +2
1 Solution
 
jhanceCommented:
#include <conio.h>
....
char c;
....
if(__kbhit()){
 c = _getch();
}
0
 
Yo081699Author Commented:
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
 
balakrishnan_tCommented:
#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
Technology Partners: 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!

 
marcjbCommented:
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
 
jhanceCommented:
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
 
jhanceCommented:
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
 
Yo081699Author Commented:
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
 
Yo081699Author Commented:
JHANCE, please copy-paste again your answer. I'm not allowed to accept a previously rejected answer.
0
 
jhanceCommented:
#include <conio.h>
.....
char c;
.....
if(_kbhit()){
 c = _getch();
}
0
 
jhanceCommented:
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
 
jpjpjpCommented:
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
 
Yo081699Author Commented:
Ok, thanks everybody. getch() will do.
0
 
balakrishnan_tCommented:
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
 
balakrishnan_tCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now