Password

the code is not complete please complete it it doesn't store backspace as a character but it doesn't erase the word either
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
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
int main(){
   string pass ="";
   char ch;
   cout << "Enter pass\n";
   ch = _getch();
   while(ch != 13){//character 13 is enter
      pass.push_back(ch);
      cout << '*';
	  if(ch == 8)   // 8 is the ascii code of backspace
          {
              if(pass.length() > 0) 
              { 
                   pass.erase(pass.length() - 1, 1); 
 
                   //Erase the last Character in password signs
				   cout << '\b' << " " << '\b'; 
 
               }
             
          }
      ch = _getch();

   }
   if(pass == "sarmad"){
      cout << "\nAccess granted :P\n";
   }else{
      cout << "\nAccess aborted...\n";
   }
   _getch();
}
the problem is that you insert the backspace and then you remove it. In other words: line 11/12 must be in the else branch of the if(ch == 8) on line 13. That way you don't insert the backspace but remove the last inserted char.
Topic archived. No new replies allowed.