Login System (with database file)

Alright, so I need a program that will read to see if a database file exists, and if it doesn't, asks the user to create a username and password, then put them in the database file. Then regardless of the result of the file check, ask to login. When the user logs in, I want it to be the username and password that is in the database file.

ALSO:
I want to be able to have multiple users.
I would PREFER to have one database file, and not two, as this complicates things.
Say there are 5 users. I want user 4 to be able to edit JUST their username and password from the database.
I want to have a guest mode.
I want to have ONE admin.

I know that is a lot, but I have a code that will do most of it, there are just a few problems.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

#include <cstdlib>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include "functions.h"


using namespace std;

const int SIZE = 100;	// Always use MORE than required
string Usernames[SIZE]; // From the database
string Password[SIZE];	// From the database

int GetNameIndex(string query, int size)
{
	for (int i=0; i<size; i++) //Prevents the program from reading garbage
	{
		if (query == Usernames[i]) return i; //Return the index
	}
	return -1; //Error code
}
bool PasswordMatches(int index, string passwd)
{
	return (passwd == Password[index]);
}



int main(int argc, char** argv) {   
    initUserCheck(); // Checks to make sure that a user is registered, if not creates a new one.
    
    //Starts the Login Process.
    //Read the database:
        
        const char *appdata = std::getenv ( "APPDATA" );
        std::string path = appdata;
        path += "\\myprogram\\files\\database.dtbf";
        std::ifstream fin ( path.c_str() );
        
	int i=0;

	while (!fin.eof()){
		fin >> Usernames[i] >> Password[i];
		i++; //This represents the number of lines we could extract from the database
	}

        //Begin VISUAL login
        RetryLogin:
	string usrname, passwd;
	printf("Username: ");
	cin >> usrname;

	int index = GetNameIndex(usrname, i); //Send the database size to this function.

	printf("Password: ");
	cin >> passwd;
                        
	if (!PasswordMatches(index, passwd)){ // If Password+Username doesn't match.
	
            printf("Correct Username, incorrect Password.  Please try again.  \n");
            system("PAUSE");
            goto RetryLogin;
	}
    
    fin.close();
    printf("You have successfully logged in.\n\n");

    // Rest of the Program... 


Probems with this code:
If a user enters a wrong username, the program freezes, and then becomes unresponsive (in English, it crashes). There is no guest, or admin mode.

Last thing:
lets say that the file, filename has this:

sillyguy
password
mike
legos
sarah
ilikepie


How does mike, edit JUST their username and password, without messing with everyone elses?

Thanks!!

If you don't get anything about this, I will get back to you ASAP.
Last edited on
Files are treated as streams, so you can't go into the middle of a file and edit specific parts directly. You'll need to create a second file, read the lines out of the first one, possibly change them, write them to the second file, delete the first file, rename the second file.
Isn't C++ wonderful?
Use sqlite library, it was designed fot this kind of purpose:
http://www.sqlite.org/

Files are treated as streams, so you can't go into the middle of a file and edit specific parts directly. You'll need to create a second file, read the lines out of the first one, possibly change them, write them to the second file, delete the first file, rename the second file.
Isn't C++ wonderful?


How should I go about doing that, should I completely pitch the code I currently have? Can you show me some source code?
Figured it out myself. Just sat down and started thinking... Anything wrong with this?? It meets all of my requirements except for the one file thing... but whatever.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <cstdlib>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include "functions.h"


using namespace std;

int main(){    
    initUserCheck(); // Checks to make sure that a user is registered, if not creates a new one.

    Login:
    string usrname;
    string psword;
    string rpsword;
    string path;
    string ext = ".DTBF";
    int adminLevel = 0;
    
    printf("Username: ");
    cin >> usrname;
    printf("Password: ");
    cin >> psword;
    
    const char *appdata = std::getenv ( "APPDATA" );
    path = appdata;
    path += "\\myprogram\\files\\users\\" + usrname + ext;
    
    ifstream readUser(path.c_str());
    readUser >> rpsword;
        
    if(!readUser){
        
        if(usrname == "Guest" or "guest"){
        
        printf("You are a guest.  \n");
        goto MainProgram;
        
    }
        printf("Incorrect Username or Password.  \n");
        system("PAUSE");
        printf("\n");
        goto Login;
        
    }
    
    if(psword != rpsword){
        
        printf("Incorrect Username or Password.  \n");
        printf("Please press any key, and then retry the login.\n");
        cin.get();
        goto Login;
        
    }
    
    if (usrname == "admin" or usrname == "Admin"){
        
        adminLevel = 1;
        
    }

MainProgram:
//MainCode 


If you have any things to say about improving this login system code, then feel free. Thanks guys!!
Topic archived. No new replies allowed.