Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

unknown.exe

macrumors member
Original poster
Sep 22, 2007
67
0
Somewhere on Earth
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.

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

macrumors 68020
Apr 5, 2004
2,294
0
Huntsville, AL
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!
 

fimac

macrumors member
Jan 18, 2006
95
1
Finland
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
 

pilotError

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
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



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[]);
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

macrumors member
Original poster
Sep 22, 2007
67
0
Somewhere on Earth
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



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[]);
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.
 

asciimov

macrumors newbie
Jan 31, 2008
10
0
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

macrumors member
Original poster
Sep 22, 2007
67
0
Somewhere on Earth
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

macrumors member
Original poster
Sep 22, 2007
67
0
Somewhere on Earth
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:
Code:
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

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
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:

https://forums.macrumors.com/threads/418230/

Code:
#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;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.