如果条目与变量不匹配,如何删除行
How to erase line if entry it doesn't match variable
我正在尝试制作一个必须使用密码登录的程序。如果密码不正确,它应该删除该行,以便您重试。相反,它只是换行。关于如何使这项工作有任何想法吗?
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');
}
}
可以做到。
但是,我不知道这样做的便携方式。
原因是 stdin
的回显是 console property,而不是 C++ 标准的一部分。
有一个库旨在解决该问题:ncurses
除此之外,您的循环可以大大简化。 (参见:ideone)
如果有便携式解决方案,我真的很想知道。我花了大约 5 个小时环顾四周,这是我得到的最好的。
我正在尝试制作一个必须使用密码登录的程序。如果密码不正确,它应该删除该行,以便您重试。相反,它只是换行。关于如何使这项工作有任何想法吗?
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');
}
}
可以做到。
但是,我不知道这样做的便携方式。
原因是 stdin
的回显是 console property,而不是 C++ 标准的一部分。
有一个库旨在解决该问题:ncurses
除此之外,您的循环可以大大简化。 (参见:ideone)
如果有便携式解决方案,我真的很想知道。我花了大约 5 个小时环顾四周,这是我得到的最好的。