LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-06-2005, 03:58 PM   #1
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Rep: Reputation: 15
Character Menu Function


I'm trying to write a reusable, keyword, reusable, function that I could use that will detect for valid input of a menu that only uses characters.

For instance:

If the menu choices are S, G, and Q, I would write a menu function that will accept valid input as only: S, s, G, g, and Q, q.

I wanted to write a function similar to:

Code:
int GetValidInt(int min, int max) { /* is set greater than max so the loop will be entered*/ int input = max + 1; /* Loop assures valid input */ while( input < min || input > max ) { printf("Please enter an integer between"); printf(" %d and %d.\n", min, max); scanf("%d", &input); } return input; }
However, the S, G, and Q are not next to each other. Is there a way to do this without simply writing something like if menuchoice != S, G, Q, etc...

Maybe with ascii values???

Someone recommended using an array of characters? How exactly would I do this?

Thanks.

Mistro116
 
Old 11-06-2005, 11:06 PM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
May I suggest that you put some newlines in your code-block. The reply button falls of the screen (and that hurts )
Code:
int GetValidInt(int min, int max) {
/* is set greater than max so the loop will be entered*/
int input = max + 1; /* Loop assures valid input */
while( input < min || input > max ) {
  printf("Please enter an integer between");
  printf(" %d and %d.\n", min, max);
  scanf("%d", &input);
}
return input;
Now I can read it as well
With regards to the question:
Code:
/*
 * prompt user for input and validate
 * in:   prompt
 *        valid characters
 * return:
 *        validated input
*/
char GetValidChar(char *prompt, char *validchars) {
char input;
int valid=0;
while(1) {
  // prompt user for input
  printf("%s:",prompt);
  ch=get_input();  // use your input function here

  // check input here
  for(cnt=0;cnt<strlen(validchars);cnt++)
  {
    if(input==validchars[cnt])
    {
       valid=1;
       break;
    }
  }

  if(valid)
  {
    return input;
  }
  return 0;
}
Call with something like
Code:
ch=GetValidChar("valid input is AaBcdQq","AaBcdQq");
To make the function universal (and in the good *nix tradition), it does differentiate between lowercase and uppercase.
So in your example you have to pass S, s, G, g etc in validchars.

Last edited by Wim Sturkenboom; 11-06-2005 at 11:12 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling another function from a function using GTK geminigal Programming 4 07-11-2005 03:15 PM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM
function won ' t return character array word Linh Programming 1 07-31-2003 06:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration