Re: How do I limit user entry choices?
From: Benjamin (benkong2_at_msn.com)
Date: 03/02/04
- Next message: Buck Rogers: "[C] Working with disk files. *long post warning*"
- Previous message: jeffc: "Re: Program crashing."
- Next in thread: Francis Glassborow: "Re: How do I limit user entry choices?"
- Reply: Francis Glassborow: "Re: How do I limit user entry choices?"
- Reply: Barry Schwarz: "Re: How do I limit user entry choices?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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;
}
- Next message: Buck Rogers: "[C] Working with disk files. *long post warning*"
- Previous message: jeffc: "Re: Program crashing."
- Next in thread: Francis Glassborow: "Re: How do I limit user entry choices?"
- Reply: Francis Glassborow: "Re: How do I limit user entry choices?"
- Reply: Barry Schwarz: "Re: How do I limit user entry choices?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|