Hello, I am trying to create a Login system, so far it writes users correctly however it doesn't seem to be reading it correctly as it always claims the username/password is incorrect even though when I check the file it is.

Could someone help, thanks!

class TestLogin
{
    public:
    char name[16];
    char pword[10];

        void sendname();
        void sendpassword();
};

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void TestLogin::sendname()
{
    cout << "Enter your desired username(max 16 characters): ";
    cin >> name;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
};

void TestLogin::sendpassword()
{
    cout << "Enter your password(max 10 characters): ";
    cin >> pword;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
};

int ListMenu()
{
    int option;
    cout << "Welcome to the login menu..." << endl;
    cout << "\tPress '1' to login." << endl;
    cout << "\tPress '2' to register." << endl;
    cout << "Your option: ";
    cin >> option;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    return option;
};

int main()
{
    ifstream readFile ("list.txt");
    ofstream writeFile ("list.txt", ios::app);
    TestLogin charInfo;
    do
    {
        int option = ListMenu();
        cout << endl;

        if(option == 1)
        {
            string username;
            string password;
            string temp;
            string temp1;

            if(readFile.is_open())
            {
                cout << "Enter Name: ";
                cin >> username;
                cout << "Enter Password: ";
                cin >> password;
                cout << endl;
                readFile >> temp;
                cout << "scanning for username...";


                if(username == temp)
                {
                    cout << "\nusername found, checking password" << endl;

                    readFile>>temp1;
                    if(password == temp1)
                    {
                        cout << "Welcome " << username << "!" << endl;
                        break;
                    }
                }
                else
                {
                    cout << "Invalid username or password." << endl;
                }
                readFile.close();
            }
        }
        cout << endl;
        if(option == 2)
        {
            charInfo.sendname();
            cout << endl;
            charInfo.sendpassword();
            cout << endl;
            if (writeFile.is_open())
            {
                writeFile << charInfo.name << endl;
                writeFile << charInfo.pword << endl;
                writeFile.close();
            }
            else
            {
                cout << "Unable to open file";
            }
        }
    }while(1);
    return 0;
}

When you do this:
readFile >> temp;
or this:
readFile >> temp1;
you are not reading a line, that operator is reading each character until it reaches a whitespace. If there is no whitespace in your file, it will read the whole file. To read a line you have to use the function getline() or reading character by character until it reaches the '\n' character.

Hello,

Thanks for the information, how will I go about implementing the getline?

Thanks.

I assume something like: getline (readFile,line) will be needed but not sure where to put it?

Yup.. u can use it by using 'cin.getline(temp1)'... Also make sure u make 'temp1' an array of say about atleast 15 characters so that the compiler has enough space to read data into the array !!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.