Thread: the typical * password thing

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    5

    the typical * password thing

    so ive only recently started trying to learn c++
    i did as many of the tutorials as i could figure out from this site, now i have been going through the forums and just trying out different code that people ask about and stuff
    so i read several threads on making a password show up as * and i tried to piece one together for myself this is what i have so far, but it isn't working quite right and i am not "learned" enough to figure out why input would be great

    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    #include <conio.h>
    
    using std::cout;
    using std::endl;
    using std::cin;
    using std::string;   
    
    int main(){
    
    	char ch;
    	string password;
    	int counter = 0;
    	int maxlength;
    	char pword[10];
    	
    		cout<<"Password: ";
    	
    	while( (ch = getch() ) != '\r' && counter < maxlength-1 ){ 
               if(ch == '\b' && counter > 0){ 
                     cout<<("\b \b"); 
                     counter--;
                     } 
               else{ 
                     pword[counter++] = ch; 
                     putchar('*'); 
                     }  
               pword[counter] = '\0';
    }
               cin>> password;
               cin.get();
               
               if (password == "1234567"){
    		      cout<<"Welcome\n";
    	       }
    
            else {
    		cout<<"Forbidden\n"; 
            }
    
               cin.get();
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i havent looked at all your code yet, just up to this first error:

    while( (ch = getch() ) != '\r' && counter < maxlength-1 )

    where is maxlength initialized/set? it isnt--give it a value.
    Last edited by nadroj; 10-26-2006 at 12:14 PM.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    Quote Originally Posted by nadroj
    i havent looked at all your code yet, just up to this first error:

    while( (ch = getch() ) != '\r' && counter < maxlength-1 )

    where is maxlength initialized/set? it isnt--give it a value.
    ok so would that be...
    Code:
    int maxlength=10;
    
    while( (ch = getch() ) != '\r' && counter < maxlength ){
    ?

    like i said i just started learning so i am not Entirely sure if that is what you ment.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    yes thats what i meant.

    after this, i was searching around i guess theres no portable way of doing this. search yourself though youll find code already made or find snippets and try and make your own. im sure itll involve things you wont know what they are or do, neither did i for some of the 'serious' ones.

    but give it a search, even on these boards. good luck and have fun

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    ok, so after this has been compiled and i run it...

    i can input 10char that appear as * the 11th char doesn't seem to do anything even as a \r.

    the 12th char acts like a regular input

    if i type in 11 characters or less...
    **********
    and press ENTER
    it just goes to the next line

    if i type in 12 characters or more...

    **********1
    and press ENTER
    it gives me the forbiddent message...

    what am i doing wrong...

    *so far i have not kept and chages to the already posted code...other than init maxlength...*

    thanks

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i know this is how it runs, i tried your code myself too.

    i dont think theres any other way to prevent characters from being echoed (displayed when typed) to the screen and saving them, other than the way i told you in previous post. youll have to search the method either here, or another search engine. good luck

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PwdFilt
    By jgelowitz in forum C Programming
    Replies: 9
    Last Post: 12-18-2008, 01:41 PM
  2. Password Problem
    By peckitt99 in forum Windows Programming
    Replies: 6
    Last Post: 11-13-2007, 03:13 AM
  3. Password
    By KJ_Magic in forum C++ Programming
    Replies: 15
    Last Post: 04-19-2003, 07:29 PM
  4. Password, color
    By leinad079 in forum C Programming
    Replies: 8
    Last Post: 03-16-2003, 05:10 PM
  5. Replies: 3
    Last Post: 12-15-2001, 01:46 PM