Solved

Write me getche()

Posted on 1997-12-12
11
1,100 Views
Last Modified: 2013-12-26
I need someone to write me the function "getche()" which can be used on the Mac (i.e. input a key without having to press Return). I can't find console.h on CW. I can't find conio.h. I'm trying to learn C using Teach Yourself C by Herb Schildt and this function is used a lot.

So, please either write me "getche()" or tell me where to find/download the library which contains conio.h which contains getche().

Confused? That's why I'm offering a wad of points for this.
0
Comment
Question by:Spenny
  • 6
  • 5
11 Comments
 
LVL 1

Expert Comment

by:twall
ID: 1291719
Are you using SIOUX within CodeWarrior?  SIOUX is the standard code for porting console-based apps.

BTW, console.h and conio.h are DOS-specific.  Look for the functions provided in stdio.h instead, which *all* ANSI C implementations must implement.
0
 

Author Comment

by:Spenny
ID: 1291720
I am using SIOUX. The only thing is I don't know which function is a decent alternative, or how I can find a list of all the functions that SIOUX offers.

You're right. These functions are DOS-based, as are the books. But the books have loads of examples in them and exercises, something which the Mac books do not. And you know what they say about practice makes perfect...
0
 
LVL 1

Accepted Solution

by:
twall earned 200 total points
ID: 1291721
The MSL libraries provide console.h, which defines getch (), which will do what you want.  Include MSL RuntimePPC.Lib or the 68k equivalent.

#include <console.h>

int ch = getch ()
0
 

Author Comment

by:Spenny
ID: 1291722
Twall, this still hasn't worked for me. I can't seem to find the MSL libraries. I have included all the Runtime libraries, but to no avail. I have MPW libraries, and all sorts, but no MSL libraries with RuntimePPC.Lib. So getch() didn't work.

Believe me, no one more than me wants to solve this. Please keep helping.
0
 
LVL 1

Expert Comment

by:twall
ID: 1291723
What version of CW are you using? If it's one of the Lite versions it may not include MSL.

Ordinarily, MSL Runtime is found at
Metrowerks Codewarrior:MacOS Support:Libraries:Runtime
and console.h is at
Metrowerks Codewarrior:Metrowerks Standard Library:MSL C:MSL Mac:Public Includes


At any rate, here are some implementations you can use:
/*
*
*    int kbhit()
*
*    returns true if any keyboard key is pressed without retrieving the key
*    used for stopping a loop by pressing any key
*/
int kbhit(void)
{
      EventRecord event;  
      return EventAvail(keyDownMask,&event);
}

/*
*
*    int getch()
*
*    returns the keyboard character pressed when an ascii key is pressed  
*    used for console style menu selections for immediate actions.
*/
int getch(void)
{
   int c;
   EventRecord event;
   fflush(stdout);
         /* Allow SIOUX response for the mouse, drag, zoom, or resize. */
   while(!GetNextEvent(keyDownMask,&event)) {
            if(GetNextEvent(mDownMask|mUpMask,&event))SIOUXHandleOneEvent(&event);
   }
   c=event.message&charCodeMask;
   if(c=='.' && (event.modifiers&cmdKey))exit(1);
 
   return c;
}

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Spenny
ID: 1291724
Seeing as these functions will be used continuously by me, I decided to put them in a header file (called "headers.h") and included in each programme. Here's the syntax:

"headers.h"

// int khbit()
// returns true if any keyboard key is pressed without retrieving the key.
// Used for stopping a loop by pressing a key.

int khbit( void );
int getch( void );

int khbit( void )
{
      EventRecord event;
      return EventAvail(keyDownMask, &event);
}

// int getch()
// returns the keyboard character pressed when an ascii key is pressed.
// used for console style menu selections for immediate actions.

int getch( void )
{
      int c;
      EventRecord event;
      fflush( stdout );
      // Allow SIOUX response for the mouse, drag, zoom or resize
      while( !GetNextEvent( keyDownMask, &event) )
      {
            if( GetNextEvent( mDownMask|mUpMask, &event)) SIOUXHandleOneEvent (&event );
      }
      
      c = event.message&charCodeMask;
      if( c=='.' && (event.modifiers&cmdKey) ) exit(1);
      
      return c;
}

Then my main programme, say:

"main.c"

#include <stdio.h>
#include "headers.h"

int main( void )
{
      int i;
      char ch;
      
      for( i=1; i<10000; i++)
      {
            if( !(i%6) )
            {
                  printf( "%d. More?", i );
                  ch = getch();
                  if( ch=='n' ) break;
                  printf( "\n" );
            }
      }
      
      return 0;
}


But, on trying to compile "headers.h" it gives me the following messages:

Error   : function has no prototype
headers.h line 26   if( GetNextEvent( mDownMask|mUpMask, &event)) SIOUXHandleOneEvent (&event );

Error   : function has no prototype
headers.h line 30   if( c=='.' && (event.modifiers&cmdKey) ) exit(1);

Have I typed these correctly or is there something I'm missing?

0
 

Author Comment

by:Spenny
ID: 1291725
Oops, the tabs didn't come out.
0
 
LVL 1

Expert Comment

by:twall
ID: 1291726
1) Don't put code in a header file.  That's bad practice.  Make a header which contains prototypes for the functions, then put the functions in a separate source file and add it to your project, or add the functions to an existing source file.  

2) Error: function has no prototype
If this is complaining about one of *your* functions, it means you need to declare the function before using it, i.e. put a prototype for it in a header or somewhere in the code stream *before* it is used.

If this is complaining about some other function, then it means you need to #include the header which contains a prototype for that fucntion.


0
 

Author Comment

by:Spenny
ID: 1291727
Twall, the errors given were in the following functions:

exit(1)

SIOUXHandleOneEvent( &event ).

Even when I included these functions in the main programme.

BTW, I'm using CW Gold 8.
0
 
LVL 1

Expert Comment

by:twall
ID: 1291728
The SIOUXHandleOneEvent declaration is in SIOUX.h, while the definition is in SIOUX.c.  (declaration is a prototype, while definition is the actual code)

exit() is declared in stdlib.h

Unfortunately, CW is not very good at looking things up like this.  It's a lot easier to search headers and lookup documentation on a UNIX system.
0
 

Author Comment

by:Spenny
ID: 1291729
It worked! Thanks for your time and help, Twall.

Look out for more things in the future, I'm sure. For now, that's really solved a huge problem. The programmes now WORK! (With getchar() they did strange things).
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

708 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