This was posted as a reply on a different thread of mine, but i needed a clean thread so here you go:
I am trying to write a program in C++ that will ask for a password, and the password you type is REPLACED BY ASTERIKS AS YOU TYPE!!! Ex:
PLEASE TYPE THE PASSWORD...
PASSWORD: ********
Read the following please:
Ok i've eddited this code a bit and I have ended up with this:
This code should do the following:
-Ask you for the password
-Replace password you type with asterisks (*) as you type
-Check if the password is correct
-If it is incorrect, see if the incorrect pass has been typed 5 times (if so then it exits, if not then it will ask for the pass again but i will add that later; for now it just exits if the pass is bad).
The code compiles and runs without errors but when it runs it shows a blank compiler screen until you type something... then EVERYTHING you press on the keyboard turns into asteriks. It does this up to 19 asteriks, where the program ends because the variable for password allows 19 characters and a null.
Can someone please help me with this code?!?!?!? I've been working on it for months!!!
I WILL WORSHIP WHOEVER CAN HELP ME MAKE THIS WORK!!!!!
Thanks in advance!
I am trying to write a program in C++ that will ask for a password, and the password you type is REPLACED BY ASTERIKS AS YOU TYPE!!! Ex:
PLEASE TYPE THE PASSWORD...
PASSWORD: ********
Read the following please:
The forum does that...
The other way to do it would be to get the characters and either echo a character back or don't echo at all.
Code:char password[20]; // Accept up to 20 char password for (i=0; i<20; i++) { password[i] = getch(); cout << "*"; if (ch[i]==13) // should this be a 10? I forget my CR's from my LF's... checkPassword(); }
Ok i've eddited this code a bit and I have ended up with this:
Code:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <conio.h>
int PassCheck(char password2[]);
void main()
{
char password[20];
for (int i=0; i<20; i++)
{
password[i] = getch();
cerr << "*";
if(password[i] == 9)
PassCheck(password);
}
}
int PassCheck(char password2[])
{
int check;
int numtries = 0;
cerr << "PLEASE TYPE IN THE PASSWORD...\nPASSWORD: ";
cin >> password2;
cerr << "LOGGING IN";
Sleep(1000);
cerr << ".";
Sleep(1000);
cerr << ".";
Sleep(1000);
cerr << ".\n";
Sleep(1000);
strcmp (password2, "xxxx");
check = 1;
if (check == 1)
{
cerr << "PASSWORD VERIFIED...\n";
cout << "goto op here lol";
}
else
{
numtries = numtries + 1;
if (numtries == 5)
{
cerr << "SYSTEM LOGGING OUT...\n";
return 0;
}
}
cerr << "PASSWORD DENIED, PLEASE TRY AGAIN...\n";
return 0;;
}
-Ask you for the password
-Replace password you type with asterisks (*) as you type
-Check if the password is correct
-If it is incorrect, see if the incorrect pass has been typed 5 times (if so then it exits, if not then it will ask for the pass again but i will add that later; for now it just exits if the pass is bad).
The code compiles and runs without errors but when it runs it shows a blank compiler screen until you type something... then EVERYTHING you press on the keyboard turns into asteriks. It does this up to 19 asteriks, where the program ends because the variable for password allows 19 characters and a null.
Can someone please help me with this code?!?!?!? I've been working on it for months!!!
I WILL WORSHIP WHOEVER CAN HELP ME MAKE THIS WORK!!!!!
Thanks in advance!