PDA

View Full Version : Continued... C++ password help!




unknown.exe
Jan 30, 2008, 01:44 PM
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:
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.



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:

#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;;
}

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!!!!!:D
Thanks in advance!:apple:



ChrisBrightwell
Jan 30, 2008, 01:54 PM
Your logic flow is messed up.

Try this:
1. Print "enter username".
2. Begin loop,
2a. accept input.
2b. compare it to the output
3c. either go back to 2a or end the loop
4. ???
5. PROFIT!

unknown.exe
Jan 30, 2008, 02:14 PM
Your logic flow is messed up.

Try this:
1. Print "enter username".
2. Begin loop,
2a. accept input.
2b. compare it to the output
3c. either go back to 2a or end the loop
4. ???
5. PROFIT!

Can you elaborate> I saee where you are getting, but I think I need a little clarification:).

fimac
Jan 30, 2008, 02:22 PM
Google for ASCII table to find the correct character code (9 is probably not what you want!).

C provides so-called escapes, which mean you do not have to remember raw codes; these include:
\n newline (linefeed)
\r carriage return

toddburch
Jan 30, 2008, 02:39 PM
Holy cow. Is this C or C++, and is it Windows or Mac?

pilotError
Jan 30, 2008, 02:44 PM
does X-Code contain windows.h? ;) :p

ChrisBrightwell
Jan 30, 2008, 02:59 PM
Can you elaborate> I saee where you are getting, but I think I need a little clarification:).

I can't make it any simpler. Sorry.

pilotError
Jan 30, 2008, 05:02 PM
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!!!!!:D
Thanks in advance!:apple:

Try something like this...
I didn't test it, but logically its a little closer to what you need
Obviously it doesn't test for using the delete key so type carefully

You should really separate the password code into a different method (ie. getPassword() ) and return a success or failure




#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[]);
int doWork();


void main()
{
char password[20];
int retryCount=0, retVal;

do {
cout << "PLEASE TYPE IN THE PASSWORD...\nPASSWORD: ";

for (int i=0; i<20; i++)
{
password[i] = getch();
cout << "*";
if (password[i] == '\n')
break;
}

retVal = PassCheck(password);

if (retVal == 1)
{
cout << "PASSWORD VERIFIED...\n";
doWork();
}
else
if ((retryCount + 1) < 5)
cout << "PASSWORD DENIED, PLEASE TRY AGAIN...\n";

} while (++retryCount < 5);

cout << "TOO MANY RETRIES, SYSTEM LOGGING OUT...\n";
exit(1);
}


int PassCheck(char password2[])
{

if ( strcmp (password2, "xxxx") == 0)
return(1);

return(0);
}

int doWork()
{
cout << "Dude Buy a Mac!" << endl;
}

unknown.exe
Jan 30, 2008, 07:35 PM
Try something like this...
I didn't test it, but logically its a little closer to what you need
Obviously it doesn't test for using the delete key so type carefully

You should really separate the password code into a different method (ie. getPassword() ) and return a success or failure




#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[]);
int doWork();


void main()
{
char password[20];
int retryCount=0, retVal;

do {
cout << "PLEASE TYPE IN THE PASSWORD...\nPASSWORD: ";

for (int i=0; i<20; i++)
{
password[i] = getch();
cout << "*";
if (password[i] == '\n')
break;
}

retVal = PassCheck(password);

if (retVal == 1)
{
cout << "PASSWORD VERIFIED...\n";
doWork();
}
else
if ((retryCount + 1) < 5)
cout << "PASSWORD DENIED, PLEASE TRY AGAIN...\n";

} while (++retryCount < 5);

cout << "TOO MANY RETRIES, SYSTEM LOGGING OUT...\n";
exit(1);
}


int PassCheck(char password2[])
{

if ( strcmp (password2, "xxxxx") == 0)
return(1);

return(0);
}

int doWork()
{
cout << "Dude Buy a Mac!" << endl;
}

WOW!!! Thanks for this! All i asked for was a nudge, not a shove lol! But i'll try this out if it works ill erect a shrine for you in my house.. thanks!:apple:
PS: I do have a mac, but I find programming C++ easier with visual C++, but i've used xcode a lot for carbon and java.

unknown.exe
Feb 5, 2008, 12:34 PM
*sigh*, the code doesn't work, but thanks anyway. It still doesn't replace my text with asteriks. Any one else have suggestions?:(

asciimov
Feb 5, 2008, 02:07 PM
Hi Unknown,

Your looking in the wrong place to do what you want.

When you type in something your c program is not echoing your key strokes to the console, the console is.

The solution to this is to turn off echoing when your typing in your password, then back on when you exit that part of the program.

I don't know what console/shell your using, so I cant give you the specific command, but this info should be able to help you google-fu your solution.

Hope that helps,
asciimov

unknown.exe
Feb 7, 2008, 09:46 AM
Hi Unknown,

Your looking in the wrong place to do what you want.

When you type in something your c program is not echoing your key strokes to the console, the console is.

The solution to this is to turn off echoing when your typing in your password, then back on when you exit that part of the program.

I don't know what console/shell your using, so I cant give you the specific command, but this info should be able to help you google-fu your solution.

Hope that helps,
asciimov

But wouldn't I WANT to have echoing on so I can echo back an asterisk every time I type a character? This password thing has been going on for about half a year now and i'm starting to give up:(

unknown.exe
Feb 7, 2008, 09:51 AM
Your logic flow is messed up.

Try this:
1. Print "enter username".
2. Begin loop,
2a. accept input.
2b. compare it to the output
3c. either go back to 2a or end the loop
4. ???
5. PROFIT!

Chris, I might try your suggestion. But what is the ???...? Also, do in #2 do you mean:

for (int i=0; i<20; i++)
{
password[i] = getch();
cout << "*";
if (password[i] == '\n')
break;
}

or do you mean add in 2a & 2b into a loop? If so, then where would I put the code replacing the text?

pilotError
Feb 7, 2008, 10:03 AM
I'm not sure what platform your working on, but getch doesn't work the same way.

I know this code won't work on a Mac OS, but you had a windows.h header, so it should have (didn't try)

This is the more appropriate way to do it on the console

From this thread:

http://forums.macrumors.com/showthread.php?t=418230&highlight=getch


#include <iostream>
#include <termios.h>

int mygetch( );

int main (int argc, char * const argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
char ch;
ch = mygetch( );
std::cout << "You hit the " << ch << " key" << std::endl;
return 0;
}

int mygetch( ) {
struct termios oldt,
newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}

ChrisBrightwell
Feb 7, 2008, 10:56 AM
or do you mean add in 2a & 2b into a loop? If so, then where would I put the code replacing the text?

2a and 2b are inside the loop, yes.


the last two steps are old Slashdot humor. Ignore those.