我正在尝试创建一个简单的程序,请求用户注册并将记录保存到文件中,如果用户已经注册,则可以通过其用户名和密码登录。但是问题是我如何验证records.txt文件中是否已经存在用户名和密码?他们是正确的吗?因此用户可以登录。我已经尝试了很多方法,但是我无法使其正常工作。我是一名大学生。
这是我的代码。
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string uName, pass, name, email, gender;
string logname, logpass;
string valname, valpass;
int phone;
ofstream output;
output.open("record.txt");
cout<<"Enter username: "<<endl;
cin>>uName;
cout<<"Enter password: "<<endl;
cin>>pass;
cout<<"Enter name: "<<endl;
cin>>name;
cout<<"Enter email: "<<endl;
cin>>email;
cout<<"Enter gender: "<<endl;
cin>>gender;
cout<<"Enter phone number: "<<endl;
cin>>phone;
output<<uName<<" "<<pass<<" "<<name<<" "<<email<<" "<<gender<<" "<<phone<<endl;
output.close();
cout<<"Registration complete!"<<endl;
// login
ifstream input("record.txt");
cout<<"Enter username:"<<endl;
cin>>logname;
cout<<"Enter password:"<<endl;
cin>>logpass;
if((input>>uName)==logname && (input>>pass)==logpass) {
//input>>uName trying to get to the file and
//read the already entered uName and match with the current entered name by user, same with pass
cout>>"Login successful"<<endl;
}
else {
cout<<"Login failed!"<<endl;
}
input.close();
cout<<"Done!"<<endl;
return 0;
}
编辑:我无法登录。这是编译日志:
超出字数限制。
编辑2:如果删除我的登录代码,这是我的record.txt文件:
john666 qwertyui John [email protected] Male 682739658
任何帮助将不胜感激。谢谢
编辑3:非常感谢THANKYOU在这里的工作代码(稍有变化):
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string uName, pass, name, email, gender;
string logname, logpass;
string valname, valpass;
int phone;
int choice;
cout<<"Enter choice:"<<endl;
cout<<"1.Register\t\t2.Login"<<endl;
cin>>choice;
if (choice==1) {
ofstream output;
output.open("record.txt");
cout<<"Enter username: "<<endl;
cin>>uName;
cout<<"Enter password: "<<endl;
cin>>pass;
cout<<"Enter name: "<<endl;
cin>>name;
cout<<"Enter email: "<<endl;
cin>>email;
cout<<"Enter gender: "<<endl;
cin>>gender;
cout<<"Enter phone number: "<<endl;
cin>>phone;
output<<uName<<" "<<pass<<" "<<name<<" "<<email<<" "<<gender<<" "<<phone<<endl;
output.close();
cout<<"Registration complete!"<<endl;
}
else if(choice==2){
// login
ifstream input("record.txt");
if(!input) {
cout<<"Error creating file!";
}
cout<<"Enter username:"<<endl;
cin>>logname;
cout<<"Enter password:"<<endl;
cin>>logpass;
if (input >> uName && input >> pass && // check if reading was successfull
uName == logname && pass == logpass) { //input??uName trying to get to the file and read the already entered uName and match with the current entered name by user, same with pass
cout<<"Login successful"<<endl;
}
else {
cout<<"Login failed!"<<endl;
}
input.close();
cout<<"Done!"<<endl;
}
else {
cout<<"Please enter a valid choice!"<<endl;
}
return 0;
}
您的主要问题是这条线
if((input>>uName)==logname && (input>>pass)==logpass)
您混合使用了两种设计样式。如您在documentation中看到的,std::basic_istream::operator>>
的所有重载都返回basic_istream &
。
这意味着input>>uName
返回对input
的引用,您无法将其与std::string
进行比较。正确的模式如下:
if (input >> uName && input >> pass && // check if reading was successfull
uName == logname && pass == logpass) // check if the read data matches the input
cout>>"Login successful"<<endl;
这只是一个错字,必须是
cout<<"Login successful"<<endl;
ifstream input("record.txt");
if(!input) { /* error occurred while opening */ }