CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Login System

  1. #1
    Join Date
    May 2010
    Posts
    19

    Login System

    Alright, I have a code that now works.
    But like always I have a couple problems I'm still seeking help with on it.

    1. Every time I want to login I have to register a new account. And after I do that and login, If I try to login again it says "Invalid username or password." It's like the file is deleting any information that is sent to it.

    So I'm trying to find a way to permanently store data once I have registered an account so that anytime I run the program I can always just login with an account I already made.

    Here is my header code:
    Code:
    #ifndef CHARACTERINFO_H_INCLUDED
    #define CHARACTERINFO_H_INCLUDED
    
    class Character
    {
        public:
        char name[16];
        char pword[10];
    
            void sendNAME();
            void sendPWORD();
    };
    
    #endif // CHARACTERINFO_H_INCLUDED

    Here is my source code:

    Code:
    #include <iostream>
    #include <fstream>
    #include "CharacterInfo.h"
    using namespace std;
    
    void Character::sendNAME()
    {
        cout << "Enter your desired username(max 16 characters): ";
        cin >> name;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    };
    
    void Character::sendPWORD()
    {
        cout << "Enter your password(max 10 characters): ";
        cin >> pword;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    };
    
    int startMenu()
    {
        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 ("example.txt");
        ofstream writeFile ("example.txt");
        Character charInfo;
        do
        {
            int option = startMenu();
            cout << endl;
    
            if(option == 1)
            {
                string username;
                string password;
    
                cout << "Enter Name: ";
                cin >> username;
                cout << "Enter Password: ";
                cin >> password;
                cout << endl;
                string temp;
                while(readFile>>temp)
                {
                    if(temp == charInfo.name)
                    {
                        readFile>>temp;
                        if(temp == charInfo.pword)
                        {
                            cout << "Welcome " << username << "!" <<endl;
                            break;
                        }
    
                    }
                    else
                    {
                        cout << "Invalid username or password." << endl;
                    }
                }
            }
            cout << endl;
            if(option == 2)
            {
                charInfo.sendNAME();
                cout << endl;
                charInfo.sendPWORD();
                cout << endl;
                if (writeFile.is_open())
                {
                    writeFile << charInfo.name << ' ' << charInfo.pword << endl;
                    writeFile << charInfo.pword;
                    writeFile.close();
                }
                else
                {
                    cout << "Unable to open file";
                }
            }
        }while(1);
        return 0;
    }

  2. #2
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399

    Re: Login System

    In option 1, you test the read in data against charInfo, but the data has been put into "username" and "password".
    your humble savant

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured