Password Mask with cin-VBForums
Results 1 to 11 of 11

Thread: Password Mask with cin

  1. #1

    Thread Starter
    Addicted Member bataeu's Avatar
    Join Date
    Nov 2000
    Location
    Walla Walla, Washington
    Posts
    144

    Password Mask with cin

    Is there anyway to make it so when a user types at the cin prompt it shows "*" instead of what they are typing. You know for a password
    Thanks in advance
    C, C++ and none of that MCF crap either!

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    never used this but possibly like this:

    Code:
    int key;
    while(key!=enter) // I don't know the value
    {
         key = getch();
         cout<<"*";
         //add key to your string
    }
    getch() is in conio.h


  3. #3
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    wouldnt that show up as

    m*y*p*a*s*s*w*o*r*d?
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  4. #4
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    eh....not sure......let me try it out

  5. #5
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    no, because there is no cin statement, nothing is being outputted but the asterisk. getch() doesn't print out what you entered.

  6. #6

    Thread Starter
    Addicted Member bataeu's Avatar
    Join Date
    Nov 2000
    Location
    Walla Walla, Washington
    Posts
    144
    Cool thanks Steve,
    The only problem is that it doesnt print to the screen when you are in the loop, it waits until afterwerds. Also it wont print out the cout i have above the loop.
    Uhm, damn i am a n00b, also I am not sure how to put the key value into a char array. I tried useing strcat but it complains about it not being the right type.

    Sorry btw I program in VB and have been trying to move on to C but it is no easy task in my opinion. Thanks for your help.
    C, C++ and none of that MCF crap either!

  7. #7

    Thread Starter
    Addicted Member bataeu's Avatar
    Join Date
    Nov 2000
    Location
    Walla Walla, Washington
    Posts
    144
    also how can i remove a character if they hit backspace. Well if the character is backspace, the ascii i can figure out, how do i remove the last character from a char array?
    Thanks
    C, C++ and none of that MCF crap either!

  8. #8
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    To remove the last character from a string, you can just replace it with a '\0' null terminator character. As in ASCII zero.
    Harry.

    "From one thing, know ten thousand things."

  9. #9

    Thread Starter
    Addicted Member bataeu's Avatar
    Join Date
    Nov 2000
    Location
    Walla Walla, Washington
    Posts
    144
    uhm exactly how would i go about that, and what would it be doing? Sorry i am VERY new to c++.
    Thanks
    C, C++ and none of that MCF crap either!

  10. #10
    Member
    Join Date
    Feb 2001
    Posts
    57
    Try this code. I think it does what you want. The only problem is that the backspace routine works really slow. Anyone have a better idea than using system("cls")?

    PHP Code:
    char password[256] = "";  // 256 chars for the password
        
    char tempchar[2] = "";  // Temporary character array

        
    char key;  // Holds the key number

        
    while(key != 13 /*Enter*/
        {
             
    key getch();  // Grab a character

             
    if (key == /*Backspace*/) {
                    
    password[strlen(password)-1] = 0// Erase the last character
                    
    system("cls");  // Clear the screen (a bit slow though)
                    
    for (int i=0i<strlen(password); i++)
                        
    cout << "*"// Throw "*"'s on the screen
                    
    flush(cout);  // Flush output to the screen
                    
    continue; // Start loop over again
             
    }

             
    cout << "*";  // Print a "*"
             
    flush(cout);  // Flush output to the screen
             
             
    tempchar[0] = key// Copy the char to an array
             
    tempchar[1] = 0// with a null at the end
             
    strcat(passwordtempchar);  // Append the character to the password array
        
    }

        
    cout << endl << password << endl;  // Print the inputed password 

  11. #11

    Thread Starter
    Addicted Member bataeu's Avatar
    Join Date
    Nov 2000
    Location
    Walla Walla, Washington
    Posts
    144
    odd output

    User Name:josh
    Password:*ddddddddjoshn

    when i run my program i use cin.getline to get the value for the username then i use the idea posted above for getting the password. It wasnt working right for me so i added a cout << password in the loop to see what the password looked like. This is the output i got when i typed in a username of josh and typed just n at the password promt.

    here is my code if it makes it easyer

    Code:
    #include <iostream>
    #include <windows.h>
    #include <string>
    #include <conio.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {	
    	//If they passed at least one parameter run the main code
    	if (argc > 1)
    	{
    		//Initialize the variables that we will need
    		char username[8];
    		char password[8];
    		char together[17];
    		char tempchar[2];
    		int count = 1;
    	
    		int key;
    		
    		//Ask for the username and place it in the variable username
    		cout << "User Name:";
    		cin.getline(username,255,'\n');
    		
    		cout << "Password:";
    		
    		//Loop untill ENTER is pressed.
    		while(key!=13)
    		{
    			//Set key to the key that was pressed
    			key = getch();
    			
    			if (key == 8)//Backspace was pressed
    			{
    				password[strlen(password)-1] = 0; // Erase the last character
                }
    			else
    			{
    					
    				if (count <= 8)//If it is less than 8 add to the array still
    				{
    
    					//Output an * instead of what was typed
    					cout << "*";
    
    					//Increment count by one each time throught the loop so if it gets above 8 
    					//dont add any more Chars to password or anymore "*" to the console.
    					count = (count + 1);
    
    					//This will place each key into the password variable
    					tempchar[0] = key; // Copy the char to an array
             			tempchar[1] = 0; // with a null at the end
             			strcat(password, tempchar);  // Append the character to the password array
    					
    					///////////////////////////////////////////////////////////////////
    					//DEBUG////////////////////////////////////////////////////////////
    					///////////////////////////////////////////////////////////////////
    					cout << password;
    
    				}
    
    			}
    
    		}
    
    		cout << "\n";
    		
    		//Copy username + space + password to the variable together
    		strcpy(together, username);
    		strcat(together, " ");
    		strcat(together, password);
    		
    		//run the batch file with the first argument as the name of the file.
    		ShellExecute(NULL, "open", "test", together, NULL, SW_SHOWNORMAL);
    
    		return 0;
    	}
    	else
    	{
    
    		cout << "input.exe takes one paramiter, the name \nof a file that it will pass two parameters to.\n";
    		cout << "it will ask for a User Name and Password \nand then pass them to the file named.\n";
    		cout << "\n";
    		cout << "Josh Powers\n";
    
    		return 0;
    	}
    		
    }
    C, C++ and none of that MCF crap either!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.