'How to erase line if entry it doesn't match variable
I am trying to make a program where you have to sign in with a password. If the password is incorrect it is supposed to erase the line so you can try again. Instead it just makes new lines. Any ideas on how to make this work?
cout << "--Bank Account Database--\n";
string password = "wfadmin";
string pwentry;
int entryCount = 0;
while (pwentry != password) {
if (entryCount == 0) {
string login = "\n\n\nEnter your password: ";
cout << login;
getline(cin, pwentry);
cout << string(login.length(), '\b');
entryCount++;
}
else {
string login = "\nIncorrect. Try again.\n\nEnter your password: ";
cout << login;
getline(cin, pwentry);
cout << string(login.length(), '\b');
}
}
Solution 1:[1]
It can be done.
However, I do not know of a portable way of doing it.
The reason for that is that echoing of stdin
is a console property, and not part of the C++ standard.
There is a library designed to address that problem: ncurses
Beyond that, your loop can be simplified quite a bit. (See: ideone)
If there is a portable solution, I'm really interested to know. I spent about 5 hours looking around, and this was the best I got.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | viraltaco_ |