如何使Do功能只重复输入密码
How to make Do function only repeat password input
我终于找到了如何制作我想要的函数,但我遇到的问题是,我希望它只重复要求输入密码,而不是同时要求输入用户名和密码。
#include "stdafx.h"
#include <iostream> // library that contains basic input output functions
#include <string> // library for c++ strings
using namespace std;
int main()
{
//Username and Password to validate credentials
const string USERNAME = "myself";
const string PASSWORD = "stanley";
const string USERNAME2 = "otherperson";
const string PASSWORD2 = "otherpassword";
//strings in which user will enter username and password
string username, password;
int passattempts = 0;
do{
// Prompting user to input username
cout << "Enter Username : ";
cin >> username;
//Checking if username length is less than 4 characters then display an error message
if (username.length() < 4)
{
cout << "Username length must be atleast 4 characters long.";
}
else //if username length is greater than 3
{
//promprting user for password
cout << "Enter Password : ";
cin >> password;
//Checking if password length is less than 6 characters then display an error message
if (password.length() < 6)
{
cout << "Password length must be atleast 6 characters long.";
}
else //if password length is greater than 5
{
//Checking if user's entered credentials are equal to actual USERNAME and PASSWORD
if (username == USERNAME && password == PASSWORD || username == USERNAME2 && password == PASSWORD2)
{
cout << "User credentials are correct!!!" << endl;
break;
}
else
{
cout << "Invalid login details" << endl;
++passattempts;
}
}
}
} while (passattempts != 3);
system("pause");
return (0);
}
然后将输入(并检查)用户名置于密码do循环之外。
如果你想让用户重新输入一个太短的用户名,你总是可以把它们放在另一个do循环中。没有规则规定你只允许一个循环:)
类似(伪代码)
do
{
prompt for username
read username
} while (username invalid)
do
{
prompt for password
read password
} while (password invalid)
关于是否告诉用户为什么他们的数据无效,有一个哲学上的争论。我没有被卷入其中,安保人员可以得到一点 强烈。
相关文章:
- Cryptopp:获取密码输入的填充字符串
- 如何在QT中为QInputDialog输入密码时设置背景非活动和灰色?
- 区分键盘记录器中的密码和其他类型的键盘输入
- 程序输入密码并解锁窗口7,8,10
- Vigenere密码的C++功能有时才起作用(适用于某些输入,跳过其他输入的班次)
- 如何修复密码条目,它一次输入两个字符
- C++简单的暴力项目密码输入方法
- 密码屏蔽(输入显示为********)
- c++密码存储.txt文件+屏蔽输入
- 如何在牡丹加密中检查输入的密码是否正确
- 如何让我的checkLength函数循环,直到用户输入超过5个字符的密码
- 当我输入密码时,第一个字母和我输入的不同
- 带有SSL的libWebsocks服务器不输入PEM密码短语
- 如何使Do功能只重复输入密码
- 防止提示输入 PEM 密码
- 如何在不提示输入密码的情况下设置计划任务
- 如何在终端上显示星号(*)形式的输入密码
- 如何多次尝试输入密码,终端在RAW模式
- 使用鞋帮不正确?直到我输入密码
- c++ shell命令提示输入密码