对简单的帐户检查程序有问题?程序返回编译错误,指出"no match for 'operator||' unsure of how to fix?"



我是编程新手输入多个用户名和密码,匹配它们,还允许错误输入信息的人尝试再次输入。我已经写了以下程序来这样做。如果有人还可以阐明如何使程序允许重试的用户名/密码输入,而无需结束程序,那也将不胜感激。谢谢!

#include <iostream>
#include <string>
using namespace std;
int main()
{
string username1="Varun";
string username2="Agne";
string username3="Geetha";             
string username4="Mohan"; //Usernames for accounts
string pass1="PassworD";
string pass2="PASSWORD"; //Passwords for accounts
string pass3="password";
string pass4="Password";
string Uattempt; //User input for username
string Pattempt; //User input for password

cout << "Please enter your username.n";
cin >> Uattempt;
cout << "Please enter your password n";   //Asks for username and password entry by user
cin >> Pattempt;
if(Uattempt!=username1 || username2 || username3 || username4)
{
    cout << "Access Denied. Incorrect username or password. Please retry.n";  //If username does not match possible entries, program will ask to retry
}
if(Pattempt !=(pass1||pass2)&&(pass3||pass4)
{
    cout << "Access Denied. Incorrect username or password. Please retry.n";  //If password does not match possible entries, program will ask to retry
}
if (Uattempt&&Pattempt==username1&&pass1||username2&&pass2||username3&&pass3||username4&&pass4)
{
    cout << "Access Granted. Welcome " + <<Uattempt<< + ".n";
}
else
{
    return 0;
}
}

std :: string不定义||操作员。

更改以下内容:

if(Uattempt!=username1 || username2 || username3 || username4) ...

to:

if (Uattempt!=username1 || Uattempt!=username2 || Uattempt!=username3 || Uattempt!=username4) ...

使用

if(Uattempt != username1 && Uattempt != username2 && Uattempt != username3 && Uattempt != username4)

而不是

if(Uattempt!=username1 || username2 || username3 || username4)

如果您打算获取大量的用户名和密码图:

std::map<string, string> userandpass{
  {"user1", "pass1"},
  {"user2", "pass2"}};
string Uattempt, Pattempt;
std::cout << "Please enter your username.n";
std::cin >> Uattempt;
std::cout << "Please enter your password n";   //Asks for username and password entry by user
std::cin >> Pattempt;
auto userIt = userandpass.find(Uattempt);
if (userIt == userandpass.end())
{
  std::cout << "Access Denied.";
  return;
}
string &password = userIt->second;
if (password  != Pattempt)
{
  std::cout << "Access Denied.";
  return;
}
std::cout << "Access Granted.";

最新更新