How to avoid using Backspace character with push_back?

Mar 28, 2014 at 8:34pm
How to avoid using Backspace character with push_back?

I'm making a software as an ATM, so when the user try to enter the password the user only sees *******, but when trying to delete it doesn't delete a character. It just adds a new one. Here's my code:

string password2 = "";

cout << "PASSWORD: ";

ch = _getch();
while(ch != 13) //character 13 is enter
{
password2.push_back(ch);
cout << '*';
ch = _getch();
}


And also, I try to use pop_back(); but it doesn't work either. Can anybody help me please?
Mar 28, 2014 at 8:41pm
try using:
password+=char(ch)
by the way pop_back only deletes the last character and doesn't return it
http://www.cplusplus.com/reference/string/string/pop_back/
Mar 28, 2014 at 8:52pm
Ok but where can I use it?
Mar 28, 2014 at 9:01pm
if you are asking about password+=char(ch)
1
2
3
4
5
6
7
8
9
10
11
string password2 = "";

cout << "PASSWORD: ";

ch = _getch(); 
while(ch != 13) //character 13 is enter
{
     password+=char(ch)
     cout << '*';
     ch = _getch();
}

if your question is about pop_back:I don't know I've just learned about it
Last edited on Mar 28, 2014 at 9:02pm
Mar 28, 2014 at 9:21pm
Look the entire code, I'm trying to verify if the user enter the incorrect password 3 times in a row, the aacount gets blocked:

do{
password2="";
cout<<"PASSWORD: ";
ch = _getch();while(ch != 13){//character 13 is enter
password2.push_back(ch);
cout << '*';
ch = _getch();
}
if(password!=password2)
{
password2="";
j=j+1;
}
}while(password!=password2 && j!=3);
if(j==3)
{
aux3=false;
cout<<"\nYOUR ACCOUNT HAS BEEN BLOCKED.\n"<<endl;
system("PAUSE");
}
}
else
{
cout<<"\nYOUR ACCOUNT HAS BEEN BLOCKED.\n"<<endl;
aux3=false;
system("PAUSE");
}

Some booleans are for others things. Do not pay much attention to them.
Mar 28, 2014 at 10:06pm
Are you trying to make it so that when the user presses Backspace, it deletes the last character in the string as well the * character that was printed?

Basically, you check if the user entered '\b', and if so, then overwrite the last * with a space and move the cursor back one spot.
If password2.pop_back(); doesn't work for you, I would recommend updating your compiler.
(Or use password2.erase(password2.end() - 1);.)

I whipped this up really quickly, so this is probably bad code (well, at least it doesn't use <conio.h>), but it does seem to work (as long as you're running Windows, and you stick to ASCII characters only):
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <string>
#include <windows.h>

std::string getPassword();

int main()
{
    std::cout << "Enter your password: ";
    std::string password = getPassword();
    std::cout << "\nOkay, your password was: " << password;
    std::cout << "\nJust to make sure everything still works, "
        "enter something: ";
    std::string other;
    std::getline(std::cin, other);
    std::cout << "Okay, you entered: " << other;
}

void doBackspace()
{
    HANDLE hOut;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    if ((hOut = GetStdHandle(STD_OUTPUT_HANDLE)) &&
        GetConsoleScreenBufferInfo(hOut, &csbi))
    {
        COORD pos = csbi.dwCursorPosition; // Get current position
        // Get position of last character
        if (pos.X > 0)
            --pos.X;
        else if (pos.Y > 0)
        {
            --pos.Y;
            pos.X = csbi.dwSize.X - 1;
        }
        // Overwrite that character with a space and move the cursor back
        DWORD unused;
        WriteConsoleOutputCharacter(hOut, " ", 1, pos, &unused);
        SetConsoleCursorPosition(hOut, pos);
    }
}

std::string getPassword()
{
    HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
    INPUT_RECORD ir;
    DWORD unused;
    std::string ret;
    while (ReadConsoleInput(hIn, &ir, 1, &unused)) // Get key press
    {
        if (ir.EventType == KEY_EVENT)
        {
            DWORD keyState = ir.Event.KeyEvent.dwControlKeyState;
            if (ir.Event.KeyEvent.bKeyDown == TRUE &&
               (keyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED |
                            LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) == 0)
            {
                char ch = ir.Event.KeyEvent.uChar.AsciiChar;
                if (std::isprint(ch))
                {
                    ret += ch;
                    std::cout << '*' << std::flush;
                }
                else if (ch == '\b' && !ret.empty()) // Backspace
                {
                    ret.pop_back();
                    doBackspace();
                }
                else if (ch == '\r' || ch == '\n') // Newline -- end of input
                {
                    std::cout << std::endl;
                    break;
                }
            }
        }
    }
    return ret;
}

Some sample output:
Enter your password: ****************

Okay, your password was: Password123!@#$%
Just to make sure everything still works, enter something: test
Okay, you entered: test
Last edited on Mar 28, 2014 at 10:16pm
Mar 28, 2014 at 10:28pm
Thanks to all you guys, I'll keep trying.
Topic archived. No new replies allowed.