Input help

This is a discussion on Input help within the C Programming forums, part of the General Programming Boards category; how can i get c to only accept 1 character for an input. i have a menu system 1-5 using ...

  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, 12:47 PM
  2. continues input
    By Matty_Alan in forum C Programming
    Replies: 2
    Last Post: 06-22-2007, 11:04 PM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-30-2007, 12:16 AM
  4. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 04:54 AM
  5. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 02:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21