🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: microsoft.public.vc.language
Article #7

Re: choiceyn: cut down choice command, use of sleep, kbhit and getch

#7
From: Frank Hickman
Date: Mon, 12 Jan 2004 19:30
149 lines
5089 bytes
"frank spencer" <no_spam@all> wrote in message news:4002d4eb_5@127.0.0.1...
> Hello,
>
> I have put this short program together and would like constructive
comments
> on what improvements could be made.
>
> It works like the choice.com command on win98 with no parameters.
>
> I am particularly concerned about  the use of sleep, kbhit and getch but
any
> other errors found would be appreciated.
> It seems to work but  I'm guessing there are some naive assumptions in
> there.
>
> Thanks in advance for any help given
>
> Hal
>
/*__________________________________________________________________________
> _*/
>
/*__________________________________________________________________________
> _*/
> /*
>  * choiceyn.c - emulates Win98's choice.com (with no parameters)
>  * no warranty whatsoever given or implied.
>  *
>  * Hal Styli, 2004.
>  */
>
> #include <stdio.h>
>
> #ifdef _MSC_VER
>  #include <conio.h>
conio.h is in both MS and Borland, why have multiple lines.

>  #include <windows.h>
Not needed unless your also doing some SDK calls somewhere.

>  #define KBHIT()  _kbhit()
>  #define GETCH()  _getch()
MS has these defined without the leading _ so they already match Borland, no
need to seperate.

>  #define SLEEPX(s) Sleep((s)*1000)
Not needed, getch will wait for input.

> #elif defined( __BORLANDC__ )
>  #include <conio.h>
>  #define KBHIT()  kbhit()
>  #define GETCH()  getch()
>  #define SLEEPX(s) _sleep(s)
See comments above.

> #else
>  #pragma message( "\n\n* * * U n t e s t e d * * *" )
>  #pragma message( "This source has not been tested on this platform/with
> this compiler" )
>  #include "*** ERROR -- Untested environment ***\n\n"
This should be a #error directive not a #include one.
> #endif
>
> #define BEEP '\a'
>
/*__________________________________________________________________________
> _*/
>
/*__________________________________________________________________________
> _*/
>
> int main()
> {
>     char c;
>
>     putchar('[');
>     putchar('Y');
>     putchar(',');
>     putchar('N');
>     putchar(']');
>     putchar('?');
printf would add to much additional code for such a small utility however I
would write a function to output strings something like this...

void output( char* spString )
{
    while( *spString != '\0' )
        putchar( *spString++ );
}

so you could do this...

output( "[Y,N]? " );

makes your source more readable.

>
>     for(;;)
>     {
>       if (KBHIT())
>       {
>         c=GETCH();
>         if( (c=='Y') || (c=='y') )
>     {
>       putchar('Y');
>       putchar('\n');
>       return 1;
>     }
>  else
>  if ( (c=='N') || (c=='n') )
>     {
>       putchar('N');
>       putchar('\n');
>       return 2;
>     }
>  else
>      putchar(BEEP);
>       }
>       else
>         SLEEPX(1);
>     }
> }

I would write it something like this...

    for( ;; )
    {
        ch= getch();
        if ( ch == 'Y' || ch == 'y' || ch == 'N' || ch == 'n' )
        {
            putchar( ch );
            putchar( '\n' );
            return (ch == 'Y' || ch == 'y') ? 1 : 2;
        }
        else
        {
            // Test for special characters...
            if ( ch == '\0' || ch == 0xE0 )
                getch();    // Discard special keys (function, arrows,
etc.)...
            putchar( BEEP );
        }
    }

Just my two cents, of course when asking a question like this your bound to
get several different variations :)

--
Frank
Technical Details
Message-ID: <uIWdnZPEO6ofaJ_dRVn-ig@comcast.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!peernews-us.colt.net!newsfeed.news2me.com!small1.nntp.aus1.giganews.com!border1.nntp.aus1.giganews.com!intern1.nntp.aus1.giganews.com!nntp.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail
References: <4002d4eb_5@127.0.0.1>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f8d38,6ae23aa1a5869fb6
X-Google-Attributes: gidf8d38,public
X-Google-Thread: f7a51,6ae23aa1a5869fb6
X-Google-Attributes: gidf7a51,public
X-Google-ArrivalTime: 2004-01-12 11:29:07 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!peernews-us.colt.net!newsfeed.news2me.com!small1.nntp.aus1.giganews.com!border1.nntp.aus1.giganews.com!intern1.nntp.aus1.giganews.com!nntp.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail
NNTP-Posting-Date: Mon, 12 Jan 2004 13:29:06 -0600
From: "Frank Hickman" <fhickman_nosp@m_noblesoft.com>
Newsgroups: microsoft.public.vc.language,borland.public.cpp.language
References: <4002d4eb_5@127.0.0.1>
Subject: Re: choiceyn: cut down choice command, use of sleep, kbhit and getch
Date: Mon, 12 Jan 2004 14:30:43 -0500
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <uIWdnZPEO6ofaJ_dRVn-ig@comcast.com>
Lines: 151
NNTP-Posting-Host: 68.55.155.67
X-Trace: sv3-SKON+hSOFqAcIdAjghC3s86xBsdvdkq+dYjyHyOedQUPm7AFSwImmzAX+1/eBX2pMnoxc61cb5szuMG!MbHjSnjGRbXE/tfiYxYTM4h5eI9HFNcJ3VVU/bXArT5E7g0243XPOb8scAn+
X-Complaints-To: abuse@comcast.net
X-DMCA-Complaints-To: dmca@comcast.net
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.1