TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: CHRISTOPHER BUTLER
from: KEN WAUGH
date: 1997-07-06 18:45:00
subject: Re: Help me!

 CB> I've got a program here, and it compiled fine a while ago, then I left
 CB> it for a bit... I've come back to it, and it crashes when it tries to
 CB> do a getch()! 
 CB> A bit of code that it crashes on:
 CB> char i;
 CB> if (inp(0x64) & 1)  // same as kbhit()
 CB>     i = getch();
 CB> else
 CB>     i = 0;
 CB> If I press a key then, the DOS box (or whole computer in DOS mode) just
 CB> crashes...
I assume by DOS box, you are referencing a window.. Being new to programming 
C, and I'm doing this on an Amiga, a lot of things are NEW/buggy to me. The 
amiga (windows/GUI interface) also has a getch() bug. It isn't so much a bug, 
as the amiga was never really designed for raw keyboard I/O... 
Anyways, maybe the note on what Amiga says about their window and getch how 
it works can help...
getch-Get a character from stdin immediately
Synopsis
        #include 
        c = getch(void);
        int c;      /* return character or code */
Description
        This function gets a single unbuffered character from stdin.  If
        the input is a console device, it will be put into RAW mode until
        a character is typed, then switched back to the normal buffered
        mode.
        This function is not available if the  _STRICT_ANSI  flag has been
        defined.
Portability
         SAS/C 
Returns
        If successful, the next input character is returned. Otherwise,
        the function returns EOF, which is defined in the file stdio.h. In
        the event of an EOF return, error information can be found in the
        external integers  errno  and  _OSERR .  Most programmers treat any
        EOF return as an indication of end-of-file.  However, if you want
        to distinguish errors from an end-of-file condition, you should
        reset the external integer  errno  before calling the function and
        then analyze its contents when you receive an EOF return.
        NOTE:  This function provides the functionality found in the
         getchar  function in most other compilers on most other platforms.
        A carriage return is not required before the getch function
        returns a character.
See Also
         errno ,  fgetc ,  fgetchar ,  fopen ,  getchar ,  _OSERR 
getchar does not work as expected.
_______________________________________________________________________
Input and output on the Amiga system are buffered.  On most other
systems,  getchar  immediately gets a character from stdin (usually the
keyboard).  However, the CON:  device on the Amiga system buffers its
input, so your program does not actually read any characters until you
do one of the following:
         - fill up the console input buffer
         - press Return
         - enter the Amiga End-of-File character, Ctrl-\.
The SAS/C Development System libraries include two functions that you
can use to deal with this problem:
   getch       gets a character from the console in  RAW  mode.  The
            character is returned as soon as the user types it.
   rawcon      turns RAW mode on and off.  rawcon(1) sets the console
            into RAW mode.  rawcon(0) restores the console to non-
            RAW mode.
When the console is in RAW mode, any characters typed by the user are
passed immediately to the application.
getch returns a single character from stdin just like getchar, but
getch gets the character in RAW mode.  If your console is not in RAW
mode, using getch is equivalent to the following sequence:
                  rawcon(1);
                  c = getchar();
                  rawcon(0);
If your console is in RAW mode, getch is equivalent to getchar . ******
rawcon-Set or unset raw console input
Synopsis
        #include 
        error = rawcon(flag);
        int error;        /* 0 on success */
        int flag;         /* non-zero for raw, 0 for non-raw */
Description
        This routine turns on and off the capability of stdin that allows
        you to get single character input without waiting for a new line
        character.
        This routine works with the  getch  and  getchar  functions.
        Normally, the Amiga console device waits until you press the Return
        key before it passes the keystrokes you enter to your program.
        Therefore, the  getchar  function, for example, will not be able to
        read a single character at a time.  Calling rawcon(1) forces the
        console device to pass each character separately.  Calling the
        rawcon(0) function restores the console to normal operations.
        This function is not available if the  _STRICT_ANSI" Link 
_STRICT_ANSI} flag has been
        defined.
Portability
         AmigaDOS 
Returns
        A nonzero return value indicates failure.
Example
        /*
         *
         * Wait for user to press any key (if possible)
         *
         */
        #include 
        #include 
        void main(void)
        {
            if (!rawcon(1))
            {
                printf("Press any key to continue\n");
                /* make sure output from printf is seen */
                fflush(stdout);
                getchar();
                rawcon(0);
            }
            else
            {
                /* unable to switch the console, wait some other way */
                printf("Sorry, rawcon() didn't work!\n");
                exit(EXIT_FAILURE);
            }
        }
See Also
         getch ,  getchar 
From what I've found, if I want getch() to work and work everytime I must 
setup the console both before and after a call to getch. 
What I finally did can be shown in this example program...
int main(int argc, char *argv[])
{
	int x;
	char c;
	
	if (argc <2)
	{
		printf("Here is the ASK programs format \n");
		printf("ASK [text]\n");
		printf("\"Text\" is displayed then the PC\n");
		printf("waits for Y or N to be pressed. Y\n");
		printf("returns an ERRORLEVEL of 1; N is 2.\n");
		exit(0);
	}
	
/* print all the arguments */
	
	for(x=1; x * Origin: Static Line C= Support 407-633-6855 (1:374/128)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.