我正在寻找一种方法来检查用户输入的字符串,当提示(让我们把它看作是一个字符串变量“userinput”)从其他10个字符串的数组中。到目前为止,我有:检查字符串是否与字符串数组中的任何字符串匹配?
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);
}
可以看出,虽然这只适用于单个字符串变量“用户”。我将如何改变这个字符串数组?
数组本身是:
string arr[10] = {"Test1", "Test2", "Test3", "Test4", "Test5", "Test6", "Test7", "Test8", "Test9", "Test10"};
请注意:我不打算使用的密码,只有用户名。
写一个单独的函数返回一个'bool',即在一个循环中检查'userinput'参数通过该阵列和去在你的'while()'中使用这个作为条件。 –
嗨,我对C++没有经验,你能否扩展你的建议? – Talisman
我写了一个答案。我希望你明白这个主意。 –