Solved

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

Posted on 1997-09-21
11
4,108 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

689 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