Solved

Write me getche()

Posted on 1997-12-12
11
1,106 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 

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
 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sumNumber challenge 16 122
notReplace  challenge 53 128
Updating statistics with error notification email in SQL server 4 95
Is there a simple front-end menu system. 9 89
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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