Backspace and enter cannot be used

Hi, good day.
I am making a password log in interface.
I make my input appear as *

The problem that I face is that when I press either backspace or enter, they are also counted as a character until the array is fulfilled.
Why is this so? Did any part of it goes wrong?

Hope someone can help me out please. Thank you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
  char keypwd;
	printf ("\nShutdown commencing.\n");
	 {while (1)
	 { printf ("Enter administration number to shutdown."); 
	     for(i=0;i<7;i++)
		{ keypwd=getch();
		  flushall();
		  ipt_pwd[i]=keypwd;
		  printf("*");
		}

	      if (strcmp (ipt_pwd, present_pwd)==0)
		{ printf ("\nLogin as ivan. ");
		  printf ("\nInitializing shutdown.\n\n");
		  Sleep (3500);
		  exit(1);
		 }

	       else { if (pwd_c<2) 
                      {printf ("\nWrong password. Please reenter.\n");
                       pwd_c=pwd_c+1; 
                       continue;
                      }
		      else if (pwd_c=2) 
                      {printf ("\n\nWrong password. Shutdown cancelled.");
                       Sleep (3500);
           	       system ("cls"); break;}
		      }
		    } 

}
http://msdn.microsoft.com/en-us/library/078sfkak%28v=vs.80%29.aspx

"When reading a function key or an arrow key, the function must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code."



See also: http://www.cplusplus.com/forum/general/4237/
Last edited on
Thanks for the reply.
What does it means by
_getch = returns 0 or 0xE0
_getwch = returns the actual key code
?
It means you test the value returned by getch (or _getch). If it is 0 or 0xE0 then you must call the function a second time to find which key was actually pressed.

The "w" version is a separate version of the function which you need only if dealing with wide characters.
http://www.cplusplus.com/reference/cwchar/
Last edited on
But how can I relate the backspace and enter key with this?

Is it like this?

getch()=password[i];
getwch()=getch;

if (getch='\b')
{password[i]=password[i]-1;}
No. In your program, you only need getch(). Forget about getwch(), that's just a distraction.

1
2
3
4
5
6
7
8
keypwd = getch();
if (keypwd == 0 || keypwd == 0xE0)
{
    keypwd = getch();
    // You will probably need additional code here 
    // to carry out the required action depending 
    // upon which special key was pressed.
}

Owhhh... I see... I will try out with this.

thanks for opening my eyes. :)
Topic archived. No new replies allowed.