Thread: Input help

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    6

    Input help

    how can i get c to only accept 1 character for an input. i have a menu system 1-5 using a switch statement but when i put in 12345 or someting similiar it still accepts the first number as 1 and executes the number 1 menu.im pretty new to c so any help is much appreciated.thx

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    look into getchar()
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      printf("Menu 1\n");
      printf("Menu 2\n");
      printf("Menu 3\n");
      printf("Menu 4\n");
      printf("Menu 5\n");
      
      switch(getchar())
      {
        case '1':
          //...
          break;
          
        default: 
          //...
          break;
      }  
    
      return 0;
    }
    Last edited by sand_man; 11-30-2004 at 06:39 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem grabbing k/b input
    By falcon9 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 11:47 AM
  2. continues input
    By Matty_Alan in forum C Programming
    Replies: 2
    Last Post: 06-22-2007, 10:04 PM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  4. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  5. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM