RajW wrote:
Disclaimer: I'm new to C.
I'm building a simple text based program and I'm not sure I'm
designing it correctly. What I have is simple and it works but it just
seems weird. Is there a better way? I do still have some debugging
code in the subroutine to print out the key pressed.
Basically menuinput() waits for the key press, and passes it to the
switch below. What I'm confused about is, what is the best way to get
back to the menu input subroutine? At the end of the routine that was
called, should I just call menuinput() again?
Try this routine. It is portable, unlike the <snipped> code you
posted. Compile with -DTESTING (under gcc) for a test program.
/* -------------------- file menuget.h -------------------- */
/* public domain, by C.B. Falconer. Attribution appreciated */
#ifndef H_menuget_h
# define H_menuget_h
# ifdef __cplusplus
extern "C" {
# endif
#include <stdio.h>
/* ------------------ */
int skipblanks(FILE *f);
/* ------------------ */
int flushln(FILE *f);
/* ------------------ */
/* Print optional menu and get a single char response. Repeat
as needed until the response is in the acceptable string.
If the first char in acceptable is uppercase, use that as
a default response when a blank line is entered
*/
int menuget(const char *prompt, /* whatever prompt is needed */
const char *acceptable); /* list of acceptable ans */
# ifdef __cplusplus
}
# endif
#endif
/* ----------- file menuget.h ------------ */
/* -------------------- file menuget.c -------------------- */
/* public domain, by C.B. Falconer. Attribution appreciated */
#include <ctype.h>
#include <string.h>
#include "menuget.h"
/* ------------------ */
int skipblanks(FILE *f)
{
int ch;
while (' ' == (ch = getc(f))) continue;
return ch;
} /* skipblanks */
/* ------------------ */
int flushln(FILE *f)
{
int ch;
while (('\n' != (ch = getc(f))) && (EOF != ch)) continue;
return ch;
} /* flushln */
/* ------------------ */
/* Print optional menu and get a single char response. Repeat
as needed until the response is in the acceptable string.
If the first char in acceptable is uppercase, use that as
a default response when a blank line is entered
*/
int menuget(const char *prompt, /* whatever prompt is needed */
const char *acceptable) /* list of acceptable ans */
{
int ch;
do {
if (prompt) {
printf("%s", prompt); fflush(stdout);
}
/* Now get a reply */
ch = skipblanks(stdi n); /* get first non-blank char */
if ('\n' != ch) flushln(stdin); /* set up to try again */
else if (isupper(accept able[0]))
ch = acceptable[0]; /* 1st is default entry */
} while (!strchr(accept able, ch) && (EOF != ch));
return ch;
} /* menuget */
#ifdef TESTING
/* ------------------ */
int main(void)
{
int ch;
do {
ch = menuget("\nA a all cases\n"
"n nonsense\n"
"x exit : ",
"Aanx");
switch (ch) {
case 'a':
case 'A': puts("All cases"); break;
case 'n': puts("nonsense" ); break;
case 'x':
case EOF: /* ignore here */ break;
default: puts("Ooops"); break;
}
} while (('x' != ch) && (EOF != ch));
if (EOF == ch) puts("EOF encountered");
return 0;
} /* main */
#endif
/* ----------- file menuget.c ------------ */
--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>