masking password issue

closed account (1vf9z8AR)
i wrote a program to enter password in such a way that on screen it comes to be *******
but i can't understand how to proceed further.

 
 mask(pass); inside a class 


 
  void mask(string*); defined as public inside same class


Program to mask password.
1
2
3
4
5
6
7
8
9
10
11
12
13
void user::mask(string *s)
{
        string ch=getch();
        if(ch==13)
                cout<<"\b";
        else if(ch==8)
        {
                cout<<"\0";
                break;
        }
        else
                cout<<"*";
}

What do i do now?
closed account (1vf9z8AR)
i wrote another program but it is not working.

No errors and warnings are shown

1
2
3
4
5
6
7
8
9
10
11
12
void user::mask(string s)
{
        char c;
        for(int i=0;i<100;i++)
        {
                c=getch();
                if(c=='\r')
                        break;
                std::cout<<"*";
                s=s+c;
        }
}
Pass the string by reference
 
void user::mask(string & s)


... or don't pass anything, make the string a private member variable of user.

 
void user::mask()

closed account (1vf9z8AR)
i did this but i can't input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class user
{
private:
   string name;
   string pass;
public:
                void mask()
{
        char c;
        for(int i=0;i<100;i++)
        {
                c=getch();
                if(c=='\r')
                        break;
                std::cout<<"*";
                pass=pass+c;
        }
}
        void getdata()
        {
                int flag=0;
                do
                {
                cout<<"Enter username: ";std::getline(std::cin,name);
                cout<<endl;
                cout<<"Enter password: ";mask();
                cout<<endl;
                 if((name=="myname")&&(pass=="tiger"))
                        {
                                cout<<"\n\nLogin successful"<<endl;
                                flag=1;
                        }
                else
                        cout<<"\n\nInvalid username or password\n\nHint:Your username and password is myname and tiger"<<endl<<endl;
        }while(flag==0);
        }
};
That code seems to work if the user gets name/password right on the first attempt. If they don't, any further attempts will fail because line 16 pass=pass+c; means the password keeps getting longer and longer - including all the previous failed attempts.
Last edited on
closed account (1vf9z8AR)
so what i do now? it doesn't even display my input as ***
Last edited on
Topic archived. No new replies allowed.