Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • 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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

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