获取在密码函数中工作的字符串
getting a string to work in password 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;
}
- 将
std::
添加到您的字符串声明中(因此您将拥有std::string
)。 - while循环结束后,函数
login
没有返回任何值。加入return true
。构建时也总是启用警告!
相关文章:
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 在 c++ 中对字符串进行操作的关系运算符无法按预期工作
- C++ "noskipws"没有按预期工作,如何正确允许字符串中的空格?
- C++20字符串文本模板参数工作示例
- 将双精度值转换为 char 变量时字符串流如何工作
- 为什么在C++中使用关系运算符创建的模板函数不能对字符串正常工作?
- 为什么 C 样式字符串的工作空指针检查不?
- 比较字符串的 GetLine 工作一次,然后比较之后不起作用
- 在 stl 中的字符串中擦除关键字的工作?
- scanf() 语句中"%*[^n]"的格式字符串指示什么?分配抑制器 (*) 和否定扫描集 ([^) 如何协同工作?
- 使用 for 循环打印字符串数组无法正常工作 (C++)
- 字符串文字到 char 数组的转换如何在C++中实际工作
- 如何让我的构造函数和函数工作,以便我的 main() 能够同时显示字符串和 int 数据
- 如何使用字符串更改此插入功能以在链接列表中工作
- 如何与C 字符串中的非ASCII字符一起工作
- 读取进程内存无法正常工作,使用 UTF16 字符串
- strcpy() 和 strcat() 在 Turbo C++ 中无法正常工作(从字符串 litteral 初始化数组时
- C-字符串的获取线函数无法正常工作
- 这个右移是如何工作的:字符串流>>无符号的int>>无符号的int?
- 字符串的二进制搜索无法正常工作