Solved

Getch(), Putch(), and Kbhit()

Posted on 1997-09-21
11
3,907 Views
Last Modified: 2008-03-04
I have successfully run a Turbo C++ program from Greg Perry's book "Turbo C++ By Example" called "The Mailing List Application."  It can be found in Appendix F.  It includes this function at the end:  

char get_answer(void)
  {
  char ans;

  ans = getch();

  while (kbhit()

    getch();

  putch(ans);

  return ans;
  }

When I try to run the same program in Microsoft Visual C++,
in console mode, of course, I get messages saying that getch, putch, and kbhit are undeclared identifiers and that the terms do not evaluate to functions.

I guess that means that the terms are compiler-specific functions for Borland's product.

My question:  What does Microsoft have that I can use to replace them?  I'm a beginner and don't know MFC at all.

Thanks.

Jack
0
Comment
Question by:aranusa5
  • 6
  • 5
11 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
Comment Utility
#include <conio.h>

_getch(), _putch(), _kbhit()

0
 

Author Comment

by:aranusa5
Comment Utility
I need further clarification.  The program already had the #include <conio.h> line.  Am I also supposed to add the line _getch(), _putch(), kbhit() with the other header files.  Perhaps you could show me what you mean.
0
 

Author Comment

by:aranusa5
Comment Utility
I need further clarification.  The program already had the #include <conio.h> line.  Am I also supposed to add the line _getch(), _putch(), _kbhit() with the other header files.  Perhaps you could show me what you mean.
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
Just replace getch with _getch, etc. You can open conio.h to see these 3 function prototypes.

#include <conio.h>

char get_answer(void)
                     {
                     char ans;

                     ans = _getch();

                     while (_kbhit())

                       _getch();

                     _putch(ans);

                     return ans;
                     }
0
 

Author Comment

by:aranusa5
Comment Utility
Chensu -
 I followed your instructions to the letter.  Same results!

  error C2065: '_getch': undeclared identifier
  error C2064: term does not evaluate to a function
  error C2064: term does not evaluate to a function
  error C2065: '_kbhit': undeclared identifier
  error C2064: term does not evaluate to a function
  error C2064: term does not evaluate to a function
  error C2065: '_putch': undeclared identifier
  error C2064: term does not evaluate to a function

  CL returned error code 2

  MAILING.EXE - 8 error(s), 0 warning(s)

What am I doing wrong?

Jack

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 23

Expert Comment

by:chensu
Comment Utility
What version Visual C++ are you using? You may open the file conio.h to check the function prototypes.

The following is an example from the documentation.

Example

/* KBHIT.C: This program loops until the user
 * presses a key. If _kbhit returns nonzero, a
 * keystroke is waiting in the buffer. The program
 * can call _getch or _getche to get the keystroke.
 */

#include <conio.h>
#include <stdio.h>

void main( void )
{
   /* Display message until key is pressed. */
   while( !_kbhit() )
      _cputs( "Hit me!! " );

   /* Use _getch to throw key away. */
   printf( "\nKey struck was '%c'\n", _getch() );
   _getch();
}

0
 

Author Comment

by:aranusa5
Comment Utility
Chensu -

I'm using Microsoft Visual C++, Version 1.52, (for developing 16-bit applications on Windows 3.1).  It's on a disk by itself and came packaged with another disk, Microsoft Visual C++, Version 4.0, Standard Edition.  I didn't even try my program with the 4.0 because of all the other overhead I hear that goes with it.

The program itself was originally compiled on my Turbo C++ Student Version for DOS (on another machine).  I copied the CPP file from there and put it in my new computer's hard drive and was trying to run it thru the 1.5 version.

I opened the documentation covering the conio.h file in order to look at the prototypes and I see the same thing you sent in your last comment.

Still no luck.  Any other suggestions?

Jack

0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
Can you try compiling the example KBHIT.C?

Can you show some parts of your code?
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
Now I know the reason. Go to menu Options->Project->Project Type, it should be set to MS-DOS application.
0
 

Author Comment

by:aranusa5
Comment Utility
Chensu-

That did it!  You are great!  Everything works the way it was supposed to!  

Many thanks for staying with me on this.  I was so worried that you would give up.  You must have loads of patience!

I will be looking forward to you answering my next question!

Best regards,

Jack  


0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
Now you'd better grade the answer so that it can be moved to <PAQ> from the <Locked> status. Thank you.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now