c++中的一个系统,用于创建一个文件,存储用户登录,然后稍后验证该登录



我已经尝试了一段时间,但我越看越迷路。

我一直在尝试创建一个系统,允许用户输入他们的注册详细信息,将它们存储在一个文件中,然后稍后有一个登录验证该文件。

到目前为止,我已经编写了一些代码,但它好像只读取文件的开头并只验证它。我知道它不应该工作,但我不知道如何着手解决它。
#include <iostream>
#include <string>
#include <fstream>
#include <string.h>
using namespace std;
class file {
private:
string Name, password;
string ID;
public:
void Display();
int Menu();
void addtofile();
void VerifyPass();
};
int main()
{
file Obj;
Obj.Menu();
Obj.Display();
Obj.addtofile();
Obj.VerifyPass();
}
int file::Menu() // The 
{
int Option = 0;
attemp:

system("cls");
cout << "1: Sign u " << endl;
cout << "2: Login: " << endl;
cout << "3: Exit" << endl;
cout << "Select Option: " << endl;
cin >> Option;
switch(Option)
{
case 1: 
addtofile();
break;
case 2:
VerifyPass();
break;
case 3:
exit(0);
break;
default:
cout << "Wrong Input, attempt again";
goto attemp;
}
return 0;
}
void file::addtofile() // Sign up pag:
{
// system("cls");
ofstream File("Test.txt"); 
cout << "Sign Up: n n n";
cout << "Enter Name: ";
cin >> Name;
File << Name << endl;
cout << "Enter Password: ";
cin >> password;
File << password << endl;

cout << "Enter ID: ";
cin >> ID;
File << ID << "n";
File.close();
// cout << "Finished" << endl;
// system("cls"); //
Menu();
}
void file::VerifyPass() // For verifying the user has inputted the correct information
{
string line1;
string PasswordInput2;
string NameInput2;
string IDInput2;
bool IsNamevalid = false;
bool IsIDvalid = false;
bool IsPassvalid = true;
do {
ifstream input("Test.txt", ios::app); // Iosapp is from my understanding, a pointer which allows the program to navigate a file.
system("cls");
cout << "Login: n";
cout << "Enter Name: " << endl;
cin >> NameInput2;
cout << "Enter Password: " << endl;
cin >> PasswordInput2;
cout << "Enter ID: " << endl;
cin >> IDInput2;
while (!input.eof())
{ // eof is end of file
getline(input, line1);
if (NameInput2 == line1)
{
IsNamevalid = true;
}
if (PasswordInput2 == line1)
{
IsNamevalid = true;
}
if (IDInput2 == line1)
{
IsIDvalid = true;
}
input.close();
}
if (IsNamevalid == false)
{
cout << "Wrong Name , please attempt again" << endl;
input.close();
system("pause");
}
if (IsPassvalid == false)
{
cout << "Wrong Password, Please attempt again" << endl;
input.close();
system("pause");
}
if (IsIDvalid == false)
{
cout << "Invalid ID, please attempt again" << endl;
system("pause");
}
else
{
"Login Successful";
}
} while (IsNamevalid, IsPassvalid, IsIDvalid == false);
system("pause");
}
void file::Display() // just displays what the user inputted for the sign up page
{
system("cls");
string text;
ifstream Readfile("Test.txt");
while (getline(Readfile, text))
{
cout << text << endl;
}
Readfile.close();
cout << "Click to continue to Menu" << endl;
system("pause");
Menu();
} 

你的代码有很多问题。

  • main()不应该调用addtofile()VerifyPass(),因为它们是在Menu()内部调用的。

  • Menu()应该使用do..while循环而不是goto

  • VerifyPass()在循环中使用!eof()是错误的(Display()循环正确)。

  • addtofile()在打开文件时没有指定任何标志,因此它擦除现有数据。您需要指定ios::app标志保护任何现有数据。在ifstream上使用ios::app,正如您目前在VerifyPass()中所做的那样,是错误的。ios::app是一个只输出的标志,所以它应该在addtofile()中的ofstream上使用。

  • addtofile()为每个条目添加3行新行,但VerifyPass()一次只读入并验证1行。您需要一次读取3行,并将它们作为一个整体进行验证。

话虽如此,请尝试这样做:

#include <iostream>
#include <string>
#include <fstream>
#include <limits>
#include <cstdlib>
using namespace std;
class file {
public:
void Display();
void Menu();
void AddToFile();
void VerifyPass();
};
int main()
{
file Obj;
do
{
Obj.Menu();
Obj.Display();
}
while (true);
}
void file::Menu()
{
int Option = 0;
system("cls");
do
{
cout << "1: Sign u " << endl;
cout << "2: Login: " << endl;
cout << "3: Exit" << endl;
cout << "Select Option: " << endl;
cin >> Option;
cin.ignore(numeric_limits<streamsize>::max(), 'n');
switch (Option)
{
case 1: 
AddToFile();
return;
case 2:
VerifyPass();
return;
case 3:
exit(0);
return;
default:
cout << "Wrong Input, try again" << endl;
break;
}
}
while (true);
}
void file::AddToFile()
{
ofstream File("Test.txt", ios::ate);
if (!File.is_open())
{
cout << "Can't open/create file" << endl;
system("pause");
return;
}
string Name, Password, ID;
cout << "Sign Up: n n n";
cout << "Enter Name: ";
getline(cin, Name);
File << Name << 'n';
cout << "Enter Password: ";
getline(cin, Password);
File << Password << 'n';

cout << "Enter ID: ";
getline(cin, ID);
File << ID << 'n';
File.close();
cout << "Finished" << endl;
system("pause");
}
void file::VerifyPass()
{
string line1, line2, line3;
string PasswordInput;
string NameInput;
string IDInput;
bool IsNamevalid = false;
bool IsIDvalid = false;
bool IsPassvalid = false;
ifstream input("Test.txt");
if (!input.is_open())
{
cout << "Can't open file" << endl;
system("pause");
return;
}
system("cls");
cout << "Login: n";
cout << "Enter Name: " << endl;
getline(cin, NameInput);
cout << "Enter Password: " << endl;
getline(cin, PasswordInput);
cout << "Enter ID: " << endl;
getline(cin, IDInput);
while (getline(input, line1) &&
getline(input, line2) &&
getline(input, line3))
{
if (NameInput == line1)
{
IsNamevalid = true;
if (PasswordInput == line2)
{
IsPassvalid = true;
if (IDInput == line3)
{
IsIDvalid = true;
break;
}
}
}
}
input.close();
if (!IsNamevalid)
{
cout << "Wrong Name, please try again" << endl;
}
else if (!IsPassvalid)
{
cout << "Wrong Password, please try again" << endl;
}
else if (!IsIDvalid)
{
cout << "Invalid ID, please try again" << endl;
}
else
{
cout << "Login Successful";
}
system("pause");
}
void file::Display()
{
system("cls");
string text;
ifstream Readfile("Test.txt");
while (getline(Readfile, text)) {
cout << text << endl;
}
Readfile.close();
system("pause");
}

相关内容

最新更新