获取在密码函数中工作的字符串

getting a string to work in password function

本文关键字:工作 字符串 函数 密码 获取      更新时间:2023-10-16

很抱歉我解释得不好,我是新手。我目前正在工作的密码功能,但我有问题。我将帐户名设置为字符串"john",并将帐户密码设置为int 1111。密码工作正常,但字符串导致错误。当我将"const string name = "john"更改为随机整数时,代码工作正常。

我希望有人能指出我错在哪里?

bool login() {
   const int password = 1111; 
   int passwordAttempt;
   const string name = "john";
   string nameAttempt;
   int attempts = 0;
   std::cout << "Please Enter Password:" << std::endl; //first attempt at account number & password
   std::cin >> passwordAttempt;
   std::cout << "Enter Name:"<< std::endl;
   std::cin >> nameAttempt;
if (passwordAttempt == password && nameAttempt == name) {
   return true;
   } 
else 
while (passwordAttempt!=password || nameAttempt!=name) {
     if(attempts++ ==2)//.. a loop for two more attempts
     {
       std::cout << "You have entered the wrong details three times. The application will be terminated now" << std::endl;
       return false;
       break;
     }
       std::cout<<"Incorrect password. Try again"<<std::endl;
       std::cout<< "" <<std::endl;
       std::cout << "Please Enter Password:" << std::endl;
       std::cin >> passwordAttempt;
       std::cout << "Enter Name:"<< std::endl;
       std::cin >>nameAttempt;
     }
     }


using namespace std;
int main() {
bool loggedIn = login();
if (loggedIn){
cout << "Logged In" << endl;
}
else if (!loggedIn){
cout << "Not Logged" << endl;
}
system("pause");
return 0;
}
  1. std::添加到您的字符串声明中(因此您将拥有std::string)。
  2. while循环结束后,函数login没有返回任何值。加入return true。构建时也总是启用警告!