Password Protection Problem

Hi, I'm a C++ beginner making a password protection program, and it works almost perfectly. The only problem is, if a user types their password wrong, it says invalid (obviously), but if they try and re-enter the correct password, it continues to say invalid, I suspect my program doesn't clear the value of the char after an unsuccessful attempt, so all it does is keep comparing the same char to the correct password. How do I fix this? My code is below. Thanks
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
63
void Auth()
{
     ifstream Passfile("password.txt", ios::in);
     Passfile>>inpass;
     decrypt ( inpass );
     system("cls");
     cout<<"PASSWORD: ";
     
         do   //Loop until 'Enter' is pressed
         {
         c = getch();
         switch(c)
            {
            case 0: //Catches f1-f12
               {
               getch();     
               break;
               }
               case 0xE0: //Catches arrow keys, end, home, page up/down, etc.
                    {
                         getch();
                          break;
                          }
            case '\b':
               {
               if(pass.size() != 0)  //If the password string contains data, erase last character
                  {
                  cout << "\b \b";
                  pass.erase(pass.size() - 1, 1);
                  }
               break;       
               }  
              
            default:
            {
               if(isalnum(c) || ispunct(c))
                  {
                                      
                  pass += c;
                  cout << "*";           
                  }
                  
               break;     
               }
            };
         }  
         
         
      while(c != '\r');
      
   cout<<"\n";
     Passfile.close();
     if(pass==inpass)
     {
                     Select();
                     }
     else
     {
         cout<<"Wrong Password, please try again.";
         Sleep(500);
         main();
         }
}
Last edited on
from what i have got pass is of type string.If so

1) It should be initialised as pass="";
2)You are checking password in Auth() so why you go to main() for reentering password?Also reset pass=""; in Auth();
3)don't decrypt original password.encrypt entered password and check it with encrypted original password.
Hi, I did all those changes and it still doesn't work, the code is c = getch();, all I need to do is clear the input stream after using getch(), I'm sure of it. How do I do that?
Last edited on
SOLVED:

I simply added pass.clear(); to void Auth() so it now reads:

1
2
3
4
5
     pass.clear();
     ifstream Passfile("password.txt", ios::in);
     Passfile>>inpass;
     system("cls");
     cout<<"PASSWORD: ";
Last edited on
Topic archived. No new replies allowed.