我正在寻找一种方法来检查用户从其他10个字符串的数组中输入时输入的字符串(让我们将其作为字符串变量“ userinput”)。到目前为止,我有:
while (userinput.empty()) //Check for empty input
{
cout << "Please enter your identity.\n"; //Identify user
getline(cin, userinput);
do //Check to see if user is the same as the string variable "user"
{
cout << "This user is either non existent or has access privileges revoked.\n"; //Wrong username!
cout << "Please re-enter the username.\n";
getline(cin, userinput);
}
while (user != userinput);
}
但是可以看出,这仅适用于单个字符串变量“ user”。我将如何更改字符串数组?
数组本身如下:
string arr[10] = {"Test1", "Test2", "Test3", "Test4", "Test5", "Test6", "Test7", "Test8", "Test9", "Test10"};
请注意:我不打算使用密码,仅使用用户名。
将支票放在单独的功能中
bool isValidUserName(const string& input) {
for(int i = 0; i < 10; ++i) {
if(input == arr[i]) {
return true;
}
}
return false;
}
并在一段时间内将其用作条件
while (!isValidUserName(userinput));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句