Limiting input

This is a discussion on Limiting input within the C Programming forums, part of the General Programming Boards category; Is it possible to limit user input to say a few number of characters, or even just 1? for example, ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    147

    Limiting input

    Is it possible to limit user input to say a few number of characters, or even just 1?

    for example, when the user enters 1 character, the program will immediately move on to a next line of code?
    Only by the cross are you saved...

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    getch() or getche().
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Banned
    Join Date
    May 2003
    Posts
    124
    This is from an old post of mine:

    It doesn't look very nice but it works fine.
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
    	int c;
    
    	c = getch();
    	printf("%c\n", c);
    	.....statements....
    	return 0;
    }
    If you want more than 1 character use a for to loop each character and print each character at the time which it was given.

  4. #4
    larko
    Guest
    for more than one character you could use:

    fgets( string, 10, stdin);

    that would accept only the first nine characters from the keyboard, and replsce the last character with a '\0'.
    Only useful if you're entering a string, other wise I'd go with the 'for' loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 12:22 AM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM

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