Write me getche()

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.
SpennyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

twallCommented:
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
SpennyAuthor Commented:
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
twallCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

SpennyAuthor Commented:
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
twallCommented:
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
SpennyAuthor Commented:
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
SpennyAuthor Commented:
Oops, the tabs didn't come out.
0
twallCommented:
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
SpennyAuthor Commented:
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
twallCommented:
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
SpennyAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.