Creating a Login Database

So basically I have been trying to create a Login Database in C++, in which you must register first. So to do that I think I need to take the two string values, which are the "username" and the "password" that are entered by the user in my "register" function, and assign it in my "database" function as strings. I havent had any problems so far but I cant find a way how to compare these registered username and password with the inputs of my "login" function. Is there a way to create two paths inside one function and prevent functionA from going in way1 while functionB goes only in way2 ?

I am also open for new thoughts if my logic is wrong or too overcharging or too complicated etc.

It has been only a week since I started to learn C++ so please dont criticize too much =)

Thanks in advance
if statement could work
lets say you have an array of usernames. You'll take the username entered by the user and find the index in the array.

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
#include <string>
#include <iostream>
using namespace std;

const int SIZE = 100;	// Storing 100 usernames
string Usernames[SIZE]; // From your database
string Password[SIZE];	// From your database


int GetNameIndex(string query)
{
	for (int i=0; i<SIZE; i++)
	{
		if (query == Usernames[SIZE]) return i; //Return the index
	}
	return -1; //Error code
}

bool PasswordMatches(int index, string passwd)
{
	return (passwd == Password[index]);
}

int main()
{
	string usrname, passwd;
	cout << "Username:";
	cin >> usrname;

	int index = GetNameIndex(usrname);

	cout << "Password:";
	cin >> passwd;

	if (!PasswordMatches(index, passwd))
	{
		cout << "Access denied";
		return 0;
	}

	// Your program

	return 0;
}


If you know a little more about c++, you can replace SIZE and the arrays with vectors. You just need to read them in.
Thank you for your concern this helps greatly =)

I have another little question if I may ^^ how do I create database for this code?
Well, if you're working with webpages and online stuff, then look up SQL. It's a large subject that I won't try and touch here.

If you are looking for something offline, then just save stuff to a file with ofstream. Then when you want to load the database, load it with ifstream.

This example code will populate your database. If you open the file it generates, you could add your own stuff manually too. Learn to append to files if you just want to add a single entry (maybe based on user input).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <fstream>
#include <string>
using namespace std;

int main()
{
	const int SIZE = 4;
	string names[SIZE]		= {"Jim", "Bob", "Tom", "Harry"};
	string passwords[SIZE] = {"cat", "dog", "bird", "fish"};

	ofstream fout("database.txt");

	for (int i = 0; i < SIZE; i++)
	{
		fout << names[i] << " " << passwords[i] << endl;
	}

	return 0;
}



Now I can modify the code above to read in this same database:
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
#include <string>
#include <fstream>
#include <iostream>
using namespace std;

const int SIZE = 100;	// More than required is okay, less is BAD!
string Usernames[SIZE]; // From your database
string Password[SIZE];	// From your database


int GetNameIndex(string query, int size)
{
	for (int i=0; i<size; i++) //We don't want to read garbage, so we limit our loop to the database size.
	{
		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()
{
	//Read the database;
	ifstream fin("database.txt");
	int i=0;

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

	//Now the rest of the program
	string usrname, passwd;
	cout << "Username:";
	cin >> usrname;

	int index = GetNameIndex(usrname, i); //Note that I now send the database size to this function.

	cout << "Password:";
	cin >> passwd;

	if (!PasswordMatches(index, passwd))
	{
		cout << "Access denied";
		return 0;
	}

	// Your program

	return 0;
}
Last edited on
Topic archived. No new replies allowed.