I've been attempting this for awhile but I keep getting lost the more I look into it.
I've been attempting to create a system which allows a user to input their sign up details, have them stored in a file, then later have a login which validates that file.
So far, I've come up with some code, but it's like it's only reading the start of the file and only validating that. I know it shouldn't work, but I don't know how to go about fixing it.
#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();
}