Solved

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

Posted on 1997-09-21
11
3,943 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
ID: 1170134
#include <conio.h>

_getch(), _putch(), _kbhit()

0
 

Author Comment

by:aranusa5
ID: 1170135
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
ID: 1170136
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
ID: 1170137
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
ID: 1170138
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 23

Expert Comment

by:chensu
ID: 1170139
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
ID: 1170140
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
ID: 1170141
Can you try compiling the example KBHIT.C?

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

Expert Comment

by:chensu
ID: 1170142
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
ID: 1170143
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
ID: 1170144
Now you'd better grade the answer so that it can be moved to <PAQ> from the <Locked> status. Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

863 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

23 Experts available now in Live!

Get 1:1 Help Now