获取字符串以使用密码功能

时间:2014-03-24 13:15:32

标签: c++ function

对于我糟糕的解释,我很抱歉,我对此非常陌生。我目前正在处理密码功能,但我遇到了问题。我已将帐户名设置为字符串" 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 个答案:

答案 0 :(得分:0)

  1. std::添加到字符串声明中(因此您将拥有std::string)。
  2. 在while循环之后,函数login结束,没有任何值返回。在那里添加return true。也总是在启用警告的情况下构建!