[英]When I try to compile this C++ program, it gives me an error
This is a program in which someone inputs a password and gets three tries to guess the password. 这是一个程序,其中有人输入密码,然后尝试进行三次猜测。 When I compile it, I get multiple errors, one of which includes line 13, where it basically says that it cannot find a similar function included in the
Password_Program
class. 当我编译它时,我遇到多个错误,其中一个错误包括第13行,该错误基本上说找不到在
Password_Program
类中包含的类似函数。
#include <iostream>
using namespace std;
class Password_Program
{
private:
string password;
public:
Password_Program();
void login();
string passAttempt;
};
Password_Program::Password_Program()
{
cin >> password;
}
Password_Program::login()
{
for (int i = 0; i < 3; i++)
{
cin >> passAttempt;
if (passAttempt == password)
{
cout << "Success!" << endl;
break;
}
else if (i >= 3) { cout << "Try again later" << endl;
break; }
else { cout << "Sorry, try again" << endl; }
}
}
int main() {
Password_Program myPassword;
myPassword.login;
return 0;
}
Your definition of the login()
method is missing a return type. 您对
login()
方法的定义缺少返回类型。 It should be: 它应该是:
void Password_Program::login()
{
...
}
You've also forgotten brackets when calling it: 在调用它时,您也忘记了括号:
myPassword.login();
You forgot the return type in the definition of Password_Program::login()
. 您忘记了
Password_Program::login()
定义中的返回类型。
Change 更改
Password_Program::login() {....}
to 至
void Password_Program::login() {....}
2 things are there 有2件东西
Here is the right code. 这是正确的代码。
#include <iostream>
using namespace std;
class Password_Program
{
private:
string password;
public:
Password_Program();
void login();
string passAttempt;
};
Password_Program::Password_Program()
{
cin >> password;
}
void Password_Program::login()
{
for (int i = 0; i < 3; i++)
{
cin >> passAttempt;
if (passAttempt == password)
{
cout << "Success!" << endl;
break;
}
else if (i >= 3) { cout << "Try again later" << endl;
break; }
else { cout << "Sorry, try again" << endl; }
}
}
int main() {
Password_Program myPassword;
myPassword.login();
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.