获取一个字符串在密码功能中工作 [英] getting a string to work in password function
本文介绍了获取一个字符串在密码功能中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
很抱歉我的可怜的解释我很新的这个。我目前正在开发一个密码功能,但我有问题。我设置帐户名称为字符串john和帐户密码为int 1111.密码工作正常,但字符串导致错误。当我把const string name =john改成一个随机整数代码工作正常。
Hi apologies for my poor explanation I'm very new to this. I am currently working on a password function but I'm having problems. I have set the account name to string "john" and the account password to int 1111. The password works fine but the string is causing the error. When I change "const string name = "john"" into a random integer the code works fine.
我希望有人能找到我错误的地方。
I was hoping someone could spot where I'm going wrong?
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;
}
推荐答案
- 将
std ::
添加到您的字符串声明(因此您将拥有std :: string
)。 - 在while循环结束后,函数
login
没有任何值返回。添加return true
。也总是建立警告并启用!
- Add
std::
to your string declarations (so you will havestd::string
). - After while loop there's end of function
login
without any value return. Addreturn true
there. Also always build with warnings enabled!
这篇关于获取一个字符串在密码功能中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文