Program to input Password. PLEASE HELP.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

Thread: Program to input Password. PLEASE HELP.

  1. #1
    Join Date
    Nov 2012

    Program to input Password. PLEASE HELP.

    void main()
    int i=0,flag=1;
    cout<<"Enter the password.";
    char Pass[8],pass[]={"PASSWORD"};
    while ( (i<8) && (flag==1) )
    if(Pass[i]=='\b') {i-=1;cout<<"\b";}
    if(Pass[i]=='\r') {flag=0;cout<<"\b";}
    cout<<"\nAccess Granted!";
    else cout<<"\nDenied.";
    When I input the password, the Backspace and the Ebter keys are not working as they should. Please help.
    Also, I want to know if I can press enter only once to input the password, not twice.

    Thanks a ton!! Really Appreciate the help.
    Compiler is Turbo C++ 3.0 on Windows 7.(I am a new student)

  2. #2
    Join Date
    Dec 2012

    Re: Program to input Password. PLEASE HELP.

    There are several things wrong with this program.

    You are using Turbo c++ 3.0. This is NOT ANSI c++ compliant. You would be better off using the free Microsoft c++ express complier under Windows 7.

    1) You are not null terminating Pass and not allocating memory for a final 0. The definition for Pass to allow for a maximum of 8 character passwords should be

    char	Pass[9] = {0};
    2) There is no test for backspace beyond the beginning of the input. If you enter 3 characters and then 4 backspaces, i will then be negative.

    3) When you process a backspace, \b will move the cursor to the left but will not erase the '*' on the screen. Backspace the cursor, output a ' ' then backspace the cursor again.

    4) The input stops after 8 characters have been entered. This may be as required but if your password changes to say 'passwd' then a final enter will be required which makes the program inconsistent. It would be better to require a final enter even if the password is 8 characters.

    5) Try to avoid embedded numbers such as 8 in a program. It is better to define a constant at the start of the program. If later your max password length changes to say 10 then it only requires a single change in the program.

    6) You test for '\b' and then also test for '\n' after. If you find a '\b' there is no point also testing for a '\n'. Also even if you find a '\b' or a '\n' you still output a '*' and increment i which is not wanted - especially as you have just decremented i if a '\b' is input!

    7) main should return an int under ANSI c++

    8) you have no comments in your program saying what is happening and the purpose of the variables and your code isn't indented to make it easier to read and understand.

    I've changed the program to incorrporate the above points as below. Hope this helps.

    #include <iostream>
    #include <conio.h>
    using namespace std;
    int main()
    const int MAXSIZE = 8;			//Maximum number of characters for entered password
    int	i = 0;				//Number of characters entered and position to insert next char in Pass
    bool	gotcr = false;			//Has a CR been entered? true if entered, false if have not
    char	ch,				//Entered char
    	Pass[MAXSIZE + 1] = {0},	//Entered password. Need to be one larger than max numner of chars for terminating null
    	pass[] = {"PASSWORD"};		//Password to check against
    	cout << "Enter the password :";
    	//Get characters from the keyboard until a CR entered
    	while (gotcr == false) {
    		switch (ch = getch()) {
    			//Got backspace
    			case '\b':
    				//Only process backspace if characters already entered
    				if (i > 0) {
    					Pass[i] = 0;	//Terminate input string
    					i -= 1;
    					cout << '\b' << ' ' << '\b';	//Overwrite * on screen
    			//Got a CR
    			case '\r':
    				//Terminate loop
    				gotcr = true;
    			//Got another char not \b or \n
    				//Put char into buffer if size of buffer not exceeded
    				if (i < MAXSIZE) {
    					Pass[i] = ch;
    					cout << '*';
    				} else {
    					//Output an alert (bell) if more characters entered than allowed
    					cout << '\a';
    	//Is entered pasword the one required
    	if(strcmpi(Pass, pass) == 0) {
    		cout << "\nAccess Granted!";
    	} else {
    		cout << "\nDenied.";
    	return 0;

Tags for this Thread

Posting Permissions

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

Azure Activities Information Page

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


HTML5 Development Center