Continued... C++ password help!

Discussion in 'Mac Programming' started by unknown.exe, Jan 30, 2008.

  1. macrumors member

    unknown.exe

    Joined:
    Sep 22, 2007
    Location:
    Somewhere on Earth
    #1
    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:
    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:
     
  2. macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #2
    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!
     
  3. thread starter macrumors member

    unknown.exe

    Joined:
    Sep 22, 2007
    Location:
    Somewhere on Earth
    #3
    Can you elaborate> I saee where you are getting, but I think I need a little clarification:).
     
  4. macrumors member

    Joined:
    Jan 18, 2006
    Location:
    Finland
    #4
    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
     
  5. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #5
    Holy cow. Is this C or C++, and is it Windows or Mac?
     
  6. macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
  7. macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #7
    I can't make it any simpler. Sorry.
     
  8. macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #8
    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;
    }
    
     
  9. thread starter macrumors member

    unknown.exe

    Joined:
    Sep 22, 2007
    Location:
    Somewhere on Earth
    #9
    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.
     
  10. thread starter macrumors member

    unknown.exe

    Joined:
    Sep 22, 2007
    Location:
    Somewhere on Earth
    #10
    *sigh*, the code doesn't work, but thanks anyway. It still doesn't replace my text with asteriks. Any one else have suggestions?:(
     
  11. macrumors newbie

    Joined:
    Jan 31, 2008
    #11
    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
     
  12. thread starter macrumors member

    unknown.exe

    Joined:
    Sep 22, 2007
    Location:
    Somewhere on Earth
    #12
    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:(
     
  13. thread starter macrumors member

    unknown.exe

    Joined:
    Sep 22, 2007
    Location:
    Somewhere on Earth
    #13
    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?
     
  14. macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #14
    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

    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;
    }
    
     
  15. macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #15
    2a and 2b are inside the loop, yes.


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

Share This Page