CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  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.

    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 login()
    {
        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 = login();
            cout << endl;
    
            if(option == 1)
            {
                string username;
                string password;
                
                cout << "Enter Name: ";
                cin >> username;
                cout << "Enter Password: ";
                cin >> password;
    
                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;
                    }
                }
            }
    
            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;
    }
    EDIT: Ignore the 2 posts proceeding this 1, I have edited my thread since then as I fixed my original dilemma.
    Last edited by sylphestre; May 13th, 2010 at 10:45 PM.

  2. #2
    Join Date
    May 2010
    Posts
    10

    Re: Saving and Retrieving info.

    It depends on how "secure" you want this to be. The simplest way would be to create a text file with the character's name, say "dudeguy.txt" and then use serialization to output the Character object straight to the file. That way it's easy to know what file to check for, and it's not easily editable with notepad or such.

  3. #3
    Join Date
    May 2010
    Posts
    19

    Re: Saving and Retrieving info.

    This is what I was working with earlier:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main ()
    {
      string line;
      char name[16];
      char pword[10];
      ifstream readFile ("example.txt");
      ofstream writeFile ("example.txt");
      if (writeFile.is_open())
      {
        cout << "Enter your username: ";
        cin >> name;
        cout << endl << "Enter your password: ";
        cin >> pword;
        cout << endl;
        writeFile << name << "\n";
        writeFile << pword;
        writeFile.close();
      }
      else cout << "Unable to open file";
    
      if (readFile.is_open())     // if you can open the file
      {
    
          getline (readFile,line);    // get 1 line from file and asave it to line
    
          cout <<  line << endl;     // output the line
        readFile.close();         // close
      }
    
      else cout << "Unable to open file";
    
      return 0;
    }
    That works for sending what I want to the file.
    But I couldn't figure out how I would be able to access it an authorize a login making sure the username and password were for the right account.

    What I was thinking was like what you said creating a file with the characters name and have the password inside the file and be like:

    Code:
    getline(readFile, line)
    if(pword == line)
    {
       //login was successful do whatever afterwards
    }
    but when I was experimenting with that I couldn't find out how to make it so that the file created was set to the input of the character.

    for example:
    Code:
    we.ll say the variable is userName and passWord
    user inputs PlayerA for userName,
    user inputs Orange for passWord
    ofstream writeFile(userName + ".txt")
    writeFile.open()
    writeFile << passWord
    writeFile.close()
    But that approach wasn't working for me, so now I'm lost again...

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