Re: How do I limit user entry choices?

From: Benjamin (benkong2_at_msn.com)
Date: 03/02/04


Date: Tue, 02 Mar 2004 00:41:34 GMT

On Sat, 28 Feb 2004 22:16:33 GMT, "Mike Wahler"
<mkwahler@mkwahler.net> wrote:

>"Benjamin" <benkong2@msn.com> wrote in message
>news:hkq140h8r6s3vetfksrvakssldvbbm3au4@4ax.com...
>> Not sure I understand do you have an example?
>
>Here is one of many possibilites:
>
>#include <ctype.h>
>#include <stdio.h>
>
>#define THIS '1'
>#define THAT '2'
>#define QUIT 'Q'
>
>void do_this(void)
>{
> puts("Doing this\n");
>}
>
>void do_that(void)
>{
> puts("Doing that\n");
>}
>
>void discard(void)
>{
> int c = 0;
>
> while((c = getchar()) != EOF && c != '\n')
> ; /* empty statement */
>}
>
>int err(int d)
>{
> puts("Invalid choice, try again\n");
>
> if(d)
> discard();
> return 0;
>}
>
>int menu()
>{
> int choice = 0;
>
> while(!choice)
> {
> printf("%c. %s\n", THIS, "This");
> printf("%c. %s\n", THAT, "That");
> printf("%c. %s\n", QUIT, "Quit");
>
> printf("Choice? ");
> fflush(stdout);
>
> if((choice = getchar()) != EOF)
> {
> if(getchar() == '\n')
> {
> choice = toupper((unsigned char)choice);
>
> switch(choice)
> {
> case '1':
> case '2':
> case QUIT:
> break;
>
> default:
> choice = err(0);
> continue;
> }
>
> }
> else
> choice = err(1);
> }
> else
> choice = err(0);
>
> }
>
> return choice;
>
>}
>
>int main()
>{
> int c = 0;
>
> while((c = menu()) != QUIT)
> {
> putchar('\n');
> switch(c)
> {
> case THIS:
> do_this();
> break;
> case THAT:
> do_that();
> break;
> default:
> puts("Logic error, should not reach here");
> break;
> }
> }
>
> puts("\nQuitting");
> return 0;
>}
>
>
>
>BTW please don't top-post. Thank you.
>
>-Mike
>
>
I tried this to see if I could make one case work. I only get the
invalid response error message. What did I do wrong?

#include <stdio.h>
#include <ctype.h>

#define British_Pound '1'
#define Canadian_Dollar '2'
#define Euros '3'
#define Yen '4'
#define Franc '5'
#define Quit 'Q'

void do_pounds(void)
{
        
        
        puts("\nthis works");
}

void discard(void)
{
        int c=0;
        
        while ((c = getchar()) !=EOF && c !='\n');
        
}

int err (int d)
{
        puts("Invalid choice, try again\n");
        
        if (d)
          discard();
        return 0;
        
}

int menu()
{
        int choice = 0;
        
        while(!choice)
        {
        
             printf("%c. %s\n",British_Pound,"British_Pound");
             
             printf("Choice? ");
             fflush(stdout);
        if ((choice - getchar()) !=EOF)
        {
        
                if(getchar() =='\n')
                {
                        choice = toupper((unsigned char) choice);
                        
                        switch(choice)
                        
                        {
                        case '1':
                        case '2':
                        case '3':
                        case Quit:
                              break;
                              
                        default:
                             choice = err(0);
                             continue;
                         }
                         
                 }
                 else
                     choice = err(1);
                     
        }
        else
             choice = err(0);
             
    }
         return choice;
         
}
int main()

{

        int c = 0;
        
        while(( c = menu()) !=Quit)
        {
        
                putchar('\n');
                switch(c)
                {
                case '1':
                puts("\nThis works also");
                break;
                default:
                puts("Logic error, should not reach here");
                
        }
        
}

        puts("\nQuitting");
        return 0;

}



Relevant Pages

  • Help in Java swings(internal Frame)
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)
  • [PATCH] get rid if __cpuinit and __cpuexit
    ... unsigned long action, void *hcpu) ... unsigned int cpu = hcpu; ... -static int __cpuinit ... __cpu_up(unsigned int cpu) ...
    (Linux-Kernel)
  • [PATCH,RFC 2.6.14 09/15] KGDB: SuperH-specific changes
    ... This adds basic support for KGDB on SuperH as well as adding some architecture ... -static int kgdb_uart_getchar ... -static void kgdb_uart_putchar ... * The command-line option can include a serial port specification ...
    (Linux-Kernel)
  • problem in java swings
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)
  • ToolTips in a View and TTN_NEEDTEXT
    ... extern int g_minWorkPeriod; ... void CDayView::CreateAllFonts ... void CDayView::DrawDayLog(CDC* pDC) ... BOOL CDayView::TimeToY ...
    (microsoft.public.vc.mfc)