Thread: Passwording problem

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Question Passwording problem

    Ok, I am trying to make a very basic password block on my program but I cant seem to figure out how to turn input characters into asteriks onscreen. Dont know if you need it but this is my source code.
    Code:
    //#include <def_colors.h> //Used for text coloring
    #include <windows.h> //For windows functions
    #include <iostream> //Used for multiple functions
    
    using namespace std;
    
    //Variables are below
    //Program init passwords
    string p;
    string pass = "programming";
    char load[] = "PLEASE...WAIT........";
    //Regular Variables continued
    
    //following actual code
    int main()
    {
    	int a;
    	for ( a=0;a<21;a++ ) {
    		Sleep(200);
    		//text_lightred;
    		cout << load[a] << "";
    	}
    	a = 0;
    	system("cls");
    	//text_grey;
    	cout << "!!!LOADING COMPLETE!!!" << endl;
    	Sleep(150);
    	cout << "Press Enter to Access Login Screen" << endl;
    	cin.get();
    	//CODE SPACE
    	system("cls");
    	//text_yellow;
    	cout << "ACCESS PASSWORD REQUIRED TO PROCEED" << endl;
    	//text_darkgrey;
    	cout << "Password: ";
    	cin >> p;
    	if ( p == pass ) {
    		system("cls");
    		//text_green;
    		cout << "ACCESS GRANTED" << endl;
    		//text_darkgrey;
    	}
    	else {
    	//text_red;
    	cout << "ACCESS DENIED" << endl;
    	}
    }
    I commented out my text coloring just incase someone pastes this. Thank You in advance for any assistance you provide

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Use _getch() to read in chars one at a time, they will not be echoed to the screen, i.e.

    Code:
    char c;
    char buffer[64];
    
    ZeroMemory(buffer,64);
    
    for (int i = 0; i < 64; i++) {
        c = _getch();
    
        // Break out on ctrl-c
        if (c == 3) GenerateConsoleCtrlEvent(CTRL_C_EVENT,0);
    
        // copy into buffer
        buffer[i] = c;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    cool thanks

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Read this..

    Besides you are making pointlessly unstandard and unportable code. Why use Sleep() over sleep() ? I do not remember the specifications of each but I believe they are quite similar while sleep() is standard and Sleep() is not. The same applies for using ZeroMemory instead of a for() loop or using memset().

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    well... truth the matter is that he shouldn't worry much with that when in fact he's using the windows header.

    Portability is not why system() is evil. Portability is something one wants or not. And when one doesn't want it, one shouldn't care about it. The evil in system() is what can be done with it:

    Create a batch file named pause.bat that wipes all your disk contents, place it on the same folder as your C++ program and run your little password program. Tada! your disk will be wiped clean. And your little brother can be the culprit.

    Get used to have a console window opened. and use it to test your programs. There will not be the need to use system("pause") anymore.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I use non-standard code, because any program I make is going to be and will always be for windows only, I have no intention of ever porting my programs to other OS's. Why is it that everyone says system("function") is a bad thing. It hasnt caused any problems for me? Also the main reason I post on C++ board and not windows board is because the stuff in windows board is mostly about the win32 API, so here I get mostly answers I can use without using any API.

  7. #7
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I'm pretty sure the system() thing is in the FAQ (how do I run a program from another program I think the article's called).

    I agree that no, if portability is never going to be an issue for you, don't worry about it. Funniliy enough though, I do. I use Linux a little bit and I'm probably fooling myself into thinking I might actually port something to it one day. Either that or sticking to portability as much as possible just helps me sleep at night and I'm monsterously weird.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    One thing about IfYouSaySo's code is that you'll have to put in some stuff into the loop to change the array accordingly when backspace has been pressed or something. I made one like this:

    Code:
    void hide_typing (char WORD_TO_HIDE[300], bool hide)
    {
                    char key;
    	for (int length=0; length<300; length++)
    	{
    		key = getch();			
    		if ( (char)key == 13 )	// Return		
    		{
    			break;
    		}
    		else if ( (char)key == 8 )	// Backspace
    		{
    			if ( length == 0 )
    			{
    				cout<< NULL;
    			}							
    			else
    			length--;					
    			WORD_TO_HIDE[length]=NULL;	
    			cout<< (char)8	//backspace			
    				<< (char)0	// nothing			
    				<< (char)8;	// back again			
    			length--;					
    		}
    		else
    		{
    			WORD_TO_HIDE[length]=key;	
    
    			switch (hide)
    			{
    			case 0:
    				cout<< (char)15;//outputs stars
    				break;
    
    			case 1:
    				cout<< key;//outputs key
    				break;
    			}
    
    		}
    	}
    	cout<< "\n";						 
    }
    All teh (char)NUMBER's should be changed to VK_REFERECE's to make it look better, but sure, it works. You could change the function to a string easily enough too.

    Code:
    char thing[300];
    hide_typing (thing, false );//Shows Text
    hide_typing (thing, true);//HidesText
    EDIT: WORD_TO_HIDE is kinda deceptive because I (for some unknown reason) choose to put it in all caps ... it shouldn't be. That's generally a lighthouse for marcos and proprossor stuff, so change that if you're going to use this
    Last edited by twomers; 06-23-2006 at 02:54 AM. Reason: Commenting

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    ......

    Why a security password if it has been hardcoded into the program? It just makes no sense at all... Damn, make a hash, or public key, or something Hardcoded passwords in compiled binaries... get real.
    Last edited by jafet; 06-24-2006 at 10:30 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    35
    Quote Originally Posted by jafet
    ......

    Why a security password if it has been hardcoded into the program? It just makes no sense at all... Damn, make a hash, or public key, or something Hardcoded passwords in compiled binaries... get real.

    When's the last time you've been with a woman?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM