#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
class UserBase{
public:
void GetUsername(string GetName){
MyUserName = GetName;
}
void GetPassword(string GetPassword){
GetPassword = MyPassword;
}
private:
string MyUserName;
string MyPassword;
};
int main(){
UserBase Input;
string MyName;
string MyPassword;
Input.GetUsername("test");
Input.GetPassword("noob");
cout << "enter your username, please." << endl;
cin >> MyName;
if (MyName == Input.GetUsername){
cout << "enter your password.." << endl;
cin >> MyPassword;
if (MyPassword == Input.GetPassword){
cout << "login was successfull" << endl;
Sleep(5000);
}
}
return 0; // if 0 then its a success
}
//所以基本上我试图创建用户名和登录密码应用程序。我是c ++的新手,我只编写了3周的时间,我只是在玩弄。无论什么时候我试图运行它说有错误,但它没有显示我在哪里,没有红色的任何东西。我使用vs 2013.
答案 0 :(得分:0)
在这里,我纠正了它。很多错误。
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
class UserBase{
public:
string GetUsername(){
return MyUserName;
}
string GetPassword(){
return MyPassword;
}
void setUsername(string name){
MyUserName = name;
}
void setPassword(string password){
MyPassword = password;
}
private:
string MyUserName;
string MyPassword;
};
int main(){
UserBase Input;
string MyName;
string MyPassword;
Input.setUsername("test");
Input.setPassword("noob");
cout << "enter your username, please." << endl;
cin >> MyName;
if (MyName == Input.GetUsername()){
cout << "enter your password.." << endl;
cin >> MyPassword;
if (MyPassword == Input.GetPassword()){
cout << "login was successfull" << endl;
Sleep(5000);
}
}
return 0; // if 0 then its a success
}
你没有真正的getPassword,getUsername,你只有setter(这被称为get ...这令人困惑!!)。你还打电话给#34; getPassword&#34; (在错误的上下文中)语法错误。
getPassword; //wrong
getPassword(); //correct
答案 1 :(得分:0)
你有
playerViewContract.initWithCourseDetails(element);
但if (MyName == Input.GetUsername)
首先不是有效的方法调用,无论如何,GetUsername
会返回GetUsername()
,您希望与字符串void
进行比较。另外,因为类中的两个字符串是私有访问,所以在主函数中,您无法在类之外的所有字符串中访问它们。如果您添加了一个像MyName
这样的getter方法,它返回了正确的字符串,那么您可以将其与此进行比较。