Solved

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

Posted on 1997-09-21
11
3,966 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

813 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

11 Experts available now in Live!

Get 1:1 Help Now