I have this c++ program to accept the username and the password(which is masked) and compare them to preset values. If they are same, the program closes, otherwise it loops back to the beginning.
I have had some difficulty with the password input, especially in mimicking a normal input. So far I have managed to put the Enter and Backspace functionality, but I just cant seem to get the arrow keys right.
With the code below, the program does work partly right, and of course, it moves the cursor back two times when the left key is pressed. But in the process, it also replaces the current letter under which it is with the 'alpha' symbol and the previous letter with 'K'. (K and the alpha symbol next to each other seems to be the value which my compiler, dev c++ associates with the left arrow key)
Also, when I remove the extra '\b', the cursor just doesnt move at all, and also replaces the previous letter with 'K'. I don't know what to do now, so I ask you. Thanks in advance!
#include <stdlib.h>
#include <conio.h>
#include <iostream.h>
#include <windows.h>
using namespace std;
int main()
{
int i=0;string u;char parr[i+1],ch;
while (1)
{
system("cls");
cout<<"Enter username."<<endl;
cin>>u;
system("cls");
cout<<"Enter password."<<endl;
i=0;
while (1)
{
ch=getch();
if (ch=='\r') break;
if (GetAsyncKeyState(VK_LEFT)) cout<<'\b'<<'\b';
if (ch=='\b')
{
cout<<'\b';
while (i!=0) {--i;}
ch='\0';
parr[i]='\0';
cout<<' '<<'\b';
continue;
}
parr[i]=ch;
ch='*';
cout<<ch;
++i;
}
parr[i]='\0';
string p="password";
if (u=="username" && parr==p)
{
system("cls");
cout<<"Welcome!";
break;
}
else
{
system("cls");
cout<<"Username and password entered does not match! Please try again.";
}
getch();
}
getch();
}