Stopping a user from typeing.

This is a discussion on Stopping a user from typeing. within the C++ Programming forums, part of the General Programming Boards category; Ok, lets say I wrote a small program that asks the user for his/her username, etc, etc. How can I ...

  1. #1
    knave
    Guest

    Question Stopping a user from typeing.

    Ok, lets say I wrote a small program that asks the user for his/her
    username, etc, etc.

    How can I stop the user from typeing input after about 10
    characters?

    For instances, the cursor will just sit there after the user has
    entered 10 characters waiting for the enter button, and the
    user can easily correct what he or she typed by pressing back
    space etc, etc.

    Is there a way to do this in a dos like atmoshere(win32 console)?
    Programming: win32 console | Compiler: MSVC++

  2. #2
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    i suppose you could do it a long way round and every ime a character is entered increment a counter and add the char on to the end of the 'string'. Then when it reaches the limit, you could stop it moving (move it back) and stop adding on the characters
    ????
    Monday - what a way to spend a seventh of your life

  3. #3
    Unregistered
    Guest
    I know of no way to prevent user from entering erroneous data. However, there are ways to protect the program from such abuse. First you can have a policy that all input will be as strings. You would do this becaue "there is no such thing as a bad string", whereas if input is to be an int and somebody inputs a string or a double it could crash the program. You can convert the string to any type necessary once it has been input and it's value to be appropriate. You can limit the amount of data input into the string using get() or getline(). To clear the input buffer of any remaining "erroneous" input you can call the ignore() member function of the istream using an appropriately large number as the first parameter. It's not perfect, but it's the only way I know short of working directly with the istream input buffer.

  4. #4
    Unregistered
    Guest
    get() by no means protects a user from overwriting memory, so you must use fgets().

    As to the original question, I don't know...

  5. #5
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You could do some like this:

    Code:
    #include <iostream> 
    #include <conio.h> 
    
    using namespace std;
    
    int main(int argc, char* argv[]) 
    
    { 
    	char username[11]={0};
    	char temp;
    
    	for (int i=0;i<10;i++)
    	{
    		temp=getch();
    		if(temp=='\r')
    		{
    			cout << endl;
    			break;
    		}
    		else
    		{
    			cout << temp;
    			username[i]=temp;
    		}
    	}
    
    	cout << username << endl;
    	
    	return 0; 
    
    }
    You have to add some code to handle backspaces and pause if the max number of charaters is entered.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Style Points
    By jason_m in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 07:15 AM
  2. Replies: 4
    Last Post: 04-03-2008, 10:07 PM
  3. Add/Delete Remotely a user
    By Scarvenger in forum Windows Programming
    Replies: 5
    Last Post: 03-24-2008, 09:36 AM
  4. Replies: 4
    Last Post: 04-21-2004, 05:18 PM
  5. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 06:01 AM

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