masking and attempts on password

Ive been working on a program that can mask the password and has 3 attempts.
so far i can't do both at the same time.
here is my work.
any idea how can i do both?

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream> 
#include<string.h> 
#include<conio.h>
using namespace std; 
int main()
{ 
     string _pass;
     char input;
     string yesno;
     int i=0;
     int j=0;
     char c;
     system("color 7d");  
     
     do
     {
                   cout<<"\n Welcome, Please Enter Your Password to continue"<<endl;
                  for(i=0;i<5;++i)
                  {
                                  
                                  cout<<"\n\t Password: ";
                                  while (input != 13) 
                                  { 
                                        input = (char)getch(); 
                                        if(input == 13) 
                                        break; 
                                        j++; 
                                        _pass += input; 
                                        cout << "*";
                                  }
                                  
                                  if(_pass=="haha")
                                  {
                                                   cout<<"\n\n\t\t"<<"Welcome, Sir "<<endl;
                                                   system("PAUSE");
                                                   system("cls");
                                                   break;
                                  }
                                  else if(i>2)
                                  return false;
                                  
                                  
                  }
                  
                  cout <<"Try Again?. <y/n>  \n\t\t";
                  cin>>yesno;
                  }
    while(yesno=="y");
    {
                     cout<<"EXITING PROGRAM!"<<endl;
                     cin.get();
                     system("pause");
                     return 0;
    }
     system ("PAUSE"); 
     return 0; }

this is dev c++
and im a beginner
Last edited on
The problem is that after 1 try when user presses enter 13 gets stored in input and the next time it does not go into the while.
So just put input = 0; before the while loop or just change the condition in the while to always true like while(true) or while(1), i think that should work.
Last edited on
The main issue was that you not resetting _pass, by calling _pass.clear()

Andy

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream> 
#include<string> // #0 missing header
#include<string.h> 
#include<conio.h>
using namespace std; 
int main()
{ 
     string _pass;
     //char input = '\0'; // #2 warning C4700: uninitialized local variable 'input' used
     string yesno;
     int i=0;
     int j=0;
     //char c; // #1 warning C4101: 'c' : unreferenced local variable
     system("color 7d");  
     
     do
     {
                   cout<<"\n Welcome, Please Enter Your Password to continue"<<endl;
                  for(i=0;i<3;++i) // #5 was 5
                  {
                                  _pass.clear(); // #3 reset password
                                  cout<<"\n\t Password: ";
                                  char input = '\0'; // #4 move inside loop

                                  while (input != 13) 
                                  { 
                                        input = (char)getch(); 
                                        if(input == 13) 
                                        break; 
                                        j++; 
                                        _pass += input; 
                                        cout << "*";
                                  }
                                  
                                  if(_pass=="haha")
                                  {
                                                   cout<<"\n\n\t\t"<<"Welcome, Sir "<<endl;
                                                   system("PAUSE");
                                                   system("cls");
                                                   break;
                                  }
                                  else // #6 reformat
                                  {
                                                   if(i==2) // #7 was >2
                                                         return false;
                                  }
                  }
                  
                  cout <<"Try Again?. <y/n>  \n\t\t";
                  cin>>yesno;
                  }
    while(yesno=="y");
    {
                     cout<<"EXITING PROGRAM!"<<endl;
                     cin.get();
                     system("pause");
                     return 0;
    }
     // #8 warning C4702: unreachable code
     //system ("PAUSE"); 
     //return 0;
}
Last edited on
So to me it looks like you're trying to get a character and then quickly clear the screen.
Personally I'd probably just turn the color to the command prompt off (so it's black text on black).

Oh if you're afraid of highlighting revealing the text, then I'd look for a way to block input going into the console and gather my letters via some other root, and output a * ever time this is successful.
1
2
GetKeyState(int key);
GetAsyncKeyState(int key);

These may be able to help with getting keys via "the other root", however some work is needed because you have to find shift and space etc yourself...

I'm sure there's other things, if you can't figure out a way around a problem, maybe you're looking at the wrong problem? (hope that helps in future)
Topic archived. No new replies allowed.