C++ C++;用户登录程序只读取文件的第一行 void abc::loginUser() { abc x; 流文件; char-usr[25]; 字符串pwd=“”,行; int-ch; open(“user.txt”),ios::in; cou
C++;用户登录程序只读取文件的第一行C++ C++;用户登录程序只读取文件的第一行 void abc::loginUser() { abc x; 流文件; char-usr[25]; 字符串pwd=“”,行; int-ch; open(“user.txt”),ios::in; cou,c++,file,authentication,C++,File,Authentication,C++;用户登录程序只读取文件的第一行 void abc::loginUser() { abc x; 流文件; char-usr[25]; 字符串pwd=“”,行; int-ch; open(“user.txt”),ios::in; cout user.password; if(strcmp(usr,user.username)==0) { if(pwd!=用户密码) { file.close(); CUT现在是一个极好的机会,让你学会如何使用调试器。使用调试器可以一次运行一行程
void abc::loginUser()
{
abc x;
流文件;
char-usr[25];
字符串pwd=“”,行;
int-ch;
open(“user.txt”),ios::in;
cout user.password;
if(strcmp(usr,user.username)==0)
{
if(pwd!=用户密码)
{
file.close();
CUT现在是一个极好的机会,让你学会如何使用调试器。使用调试器可以一次运行一行程序,监视所有变量的值,因为它们改变了,并且观察程序的逻辑执行流程。知道如何有效地使用调试器是每个C++开发人员的必备技能,没有例外。ns.这是一个非常简单的错误,您应该能够使用调试器快速找到它:为什么您的程序只从文件中读取一行,然后立即关闭它。祝您好运,学习如何使用调试器。我认为问题在于循环中的每个路径都调用file.close()
第一次运行后while退出。这就解决了它!我认为因为file.close在if条件内,它不会运行。谢谢!
void abc::loginUser()
{
abc x;
fstream file;
char usr[25];
string pwd = "",line;
int ch;
file.open("user.txt"),ios::in;
cout << "User Login" << endl;
cout << "Username: ";
gets(usr);
cout << "Password: ";
file.seekg(0);
while(ch = getch())
{
if(ch == 13)
{
while(file)
{
file >> user.username >> user.password;
if(strcmp(usr,user.username)==0)
{
if(pwd != user.password)
{
file.close();
cout << "\nLogin Failed";
getch();
system("cls");
x.login();
}
else if(pwd == user.password)
{
file.close();
cout << "\nLogin Successful";
getch();
system("cls");
x.menuUser();
}
}
else if(strcmp(usr,user.username)!=0)
{
file.close();
cout << "\nUser Not Registered";
getch();
system("cls");
x.login();
}
else
{
file.close();
cout << "\nLogin Failed";
getch();
system("cls");
x.login();
}
}
}
else if(ch == 8)
{
if(pwd.length() > 0)
{
cout<<"\b \b";
pwd.erase(pwd.length()-1);
}
}
else
{
cout << "*";
pwd += ch;
}
}
}