Solved

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

Posted on 1997-09-21
11
4,013 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cross compile release version of c++ program for linux 2 136
Dynamically allocate memory 9 63
Unresolved External Symbols 3 109
Unable to start eclipse ? 17 154
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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

831 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