I am somewhat between beginner and intermediately well-versed in c++, and I managed to write a basic login program in c++ using Dev C++, Windows 7. This program was capable of taking in inputs for the username and password and comparing them to see if they were equal. If so, it would welcome the user and terminate the program and if not, it would loop back to the beginning (infinitely).
Then I looked at some tutorials on the net to mask the password in C and tried to do so myself, in c++.
With the code that I have included below, I do not get any errors, but I do not get the desired output either. In the end, even if I enter the right 'password', I always get said to try again. Also, the program masks everything, including the 'Enter' and 'Backspace' key. Here's the code:
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int main()
{
int i=0;string u;char parr[i],ch;
while (1)
{
system("cls");
cout<<"Enter username."<<endl;
cin>>u;
system("cls");
cout<<"Enter password."<<endl;
for (i=0;i<=8;i++)
{
ch=getch();
parr[i]=ch;
ch='*';
cout<<ch;
}
string p(parr);
if (u=="username" && p=="password")
{
system("cls");
cout<<"Welcome!";
break;
}
else
{
system("cls");
cout<<"Username and password entered does not match! Please try again.";
}
getch();
}
getch();
}
Also, is there any way to make the password masked only for the characters and for howewer much the length of the password given by the user as input,i.e, not just 8 characters of the word 'password', but if the user enters "notapassword", then all of the characters must be masked, not just the first eight of them.