Thread: Limiting user input

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    4

    Limiting user input

    I am writing a simple program in C that allows the user to view the contents of a txt file.

    I want to resrict the input the user can make... e.g only keys 1-5 and nothing else....


    How is this possible?

    Thanks

    Tom

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    That will be OS specific.
    For Windows/DOS, use getch().
    Read this thread for a simple example of using getch() to accomplish custom input.

    gg

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I want to resrict the input the user can make... e.g only keys 1-5 and nothing else....
    You can easily restrict the input that your program accepts, but actually restricting the user from giving you more than you want is not a trivial task, and there's no way to do it within the confines of standard C.

    The former is simple:
    Code:
    int inrange ( int val, int low, int high )
    {
      return val >= low && high >= val;
    }
    
    ...
    
    /* Get input from user */
    if ( inrange ( input, 1, 5 ) ) {
      /* It's good */
    }
    else
      /* Handle erroneous input */
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    4
    thanks all

    Tom

  5. #5
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Ye could also try:
    Code:
      scanf("%[1-5]", &reciving array);

  6. #6
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    out of curiosity, could something like this do?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
       int range;
       char buf[10], *p;
    
       for(;;)
       {
          printf("Enter your number: ");
          fgets(buf, sizeof(buf), stdin);
    
          if((range = (int)strtol(buf, &p, 10)) >=1 && range <= 5)
              break;
    
          printf("\nYou are not authorized for that input.  Try again\n");
      }
    
      printf("\noption = %d", range);
    
      while(getchar() != '\n');
    
      return 0;
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Code:
    return val >= low && high >= val;
    this is pretty cryptic is it written out like this?
    Code:
    if(val>=low && high >= val){
          return val;
    }
    I was just wondering I have never seen something like that.
    My brother says that it returns either 1 or 0. Just wondering.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>this is pretty cryptic is it written out like this?
    That line of code simply(!) performs two tests, and the result is returned in the form of 1 or 0. Simply put, it returns 1 (true) if the number is within a given range, 0 (false) if not.

    Your suggestion of writing "return val;" brings a new meaning to the code, that doesn't give the same result.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >this is pretty cryptic
    Only if you aren't used to seeing somebody return the result of a boolean expression. You can lengthen it to something (debatably) more transparent like so:
    Code:
    int inrange ( int val, int low, int high )
    {
      if ( val >= low && high >= val )
        return 1;
      else
        return 0;
    }
    >My brother says that it returns either 1 or 0.
    Your brother would be correct. Any boolean expression returns 1 for true and 0 for false.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  3. Ending user input with # character
    By jowatkins in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2004, 10:41 AM
  4. Limiting scanf input
    By SMurf in forum C Programming
    Replies: 0
    Last Post: 11-21-2002, 06:39 AM