Home > front end >  A system in C to create a file, store users login, then validate that login later
A system in C to create a file, store users login, then validate that login later

Time:07-04

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();
} 

CodePudding user response:

There are a lot of problems with your code.

  • main() should not be calling addtofile() and VerifyPass() since those are called inside of Menu().

  • Menu() should use a do..while loop instead of goto.

  • VerifyPass() using !eof() in a loop is wrong (Display() loops correctly).

  • addtofile() is not specifying any flags when opening the file, so it wipes out the existing data. You need to specify the ios::app flag to preserve any existing data. Using ios::app on an ifstream, as you are currently doing in VerifyPass(), is wrong. ios::app is an output-only flag, so it should be used on the ofstream in addtofile() instead.

  • addtofile() adds 3 new lines to the file per entry, but VerifyPass() only reads in and validates 1 line at a time. You need to read in 3 lines at a time and validate them together as a whole.

With that said, try something more like this instead:

#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");
}
  •  Tags:  
  • c
  • Related