password enterance in c++

Hey i am trying to build a secure console app to store all the usernames and passwords at one place and access them through a master password.
I inserted a module of code for master password enterance and in that programm the password is not getting backspaced...
Someone pls help me. this is the code i have so far.
Its actually erasing stars but not deleting the character from string password. Help me!
string password = "";
char c = ' ';
while(c != 13) //Loop until 'Enter' is pressed
{
c = getch();
password += c;
cout << "*";
}


compiler: DEV C++
Last edited on
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 password; // no need to init string with ""; that's done by constructor

    char c = ' ';
    while(c != 13) //Loop until 'Enter' is pressed
    {
        c = getch(); // from conio.h; with VC++, this must sometimes be _getch()
        if(c == 8) // backspace
        {
            if(!password.empty()) // protect against over eager backspacing 
            {
                cout << "\b \b";
                password.resize(password.length() - 1);
            }            
        }
        else if(isprint(c)) // ignore control chars (isalnum might be better?)
        {
            password += c;
            cout << "*";
        }
    }
    
    cout << endl;
    cout << "got: " << password << endl;
    cout << endl;

    return 0;
}


Notes

'\b' is the backspace char, but it doesn't erase. So you go back, write a space, then go back again (btw - if you search this forum for \b, you'll find people discussing this same problem!)

isprint
http://www.cplusplus.com/reference/clibrary/cctype/isprint/

Can also use control chars rather than numbers: \r and \b

1
2
3
4
5
6
    char c = ' ';
    while(c != '\r') // loop until 'Enter' (13) is pressed
    {
        c = getch();
        if(c == '\b') // backspace (8)
        {

Last edited on
Topic archived. No new replies allowed.