Linear Search for Login

Hi, I am in need of some help! I am writing a web site login program that compares the input of a User ID, password, and PIN to that from a text file. So far I have successfully opened, echoprinted, and assigned the file to an array. Now I just have to search the ID array for a match and (if a match is found), then search the password and PIN for that ID to see if they all match. The user has three tries to successfully log in. Also is there anyway I can check the input from the user to make sure there is no spaces in the ID and password and that the PIN is all digits? I really need help with the searching part! Please help!

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
 #include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

typedef struct UserRecord
{
	string userID;
	string password;
	int PIN;
};

int ProcessFile (ifstream &, UserRecord []);
int CheckUsername (UserRecord [], int);
void CheckInfo (UserRecord [], int);

int main()
{
	const int MAX_USERS = 50;
	int actualUsers, usernameMatch;
	ifstream inFile;
	

	cout << "**********************************" << endl
		 << "* Welcome to MyGreatWebsite.com! *" << endl
		 << "**********************************" << endl;

	inFile.open("users.txt");
	
	if(inFile)
	{
	UserRecord loginInfo[MAX_USERS];

	actualUsers = ProcessFile(inFile, loginInfo);

	usernameMatch = CheckUsername(loginInfo, actualUsers);

	CheckInfo(loginInfo, usernameMatch); 
	}
	else

	return 0;
}

int ProcessFile (ifstream & inFile, UserRecord userData[])
{
	int currentUsers;
	
	inFile >> currentUsers;

	for(int count = 0; count < currentUsers; count++)
	{
		inFile >> userData[count].userID;
		inFile >> userData[count].password;
		inFile >> userData[count].PIN;
	}

	cout << "  There are currently " << currentUsers << " active" << endl
		 << "     members on our website!" << endl << endl;

	cout << "User ID" << setw(14) << "Password" << setw(12) << "PIN" << endl;
	cout << "_______" << setw(14) << "________" << setw(13) << "____" << endl;
	for (int count = 0; count < currentUsers; count++)
	{
		cout << userData[count].userID << setw(15) << userData[count].password << setw(13) << userData[count].PIN << endl;
	}

	return currentUsers;

}

int CheckUsername (UserRecord loginInfo[], int actualUsers)
{
	string usernameAttempt;
	string passwordAttempt;
	int pinAttempt, loginAttempts = 0, index = 0, position = -1, MAX_ATTEMPTS = 3;
	bool found = false;

	cout << endl << " If you are one of the " << actualUsers << " active" << endl
		 << "members on our site, please log in!" << endl << endl;

	cout << "Please enter a valid User ID (no blanks!): ";
	getline(cin, usernameAttempt);

	cout << endl << "Please enter a valid password (no blanks!): ";
	getline(cin, passwordAttempt);
	
	cout << endl << "Please enter a valid PIN number: ";
	cin >> pinAttempt;


	do
	{
		while (index < actualUsers && !found)
		{
			if (loginInfo[index].userID == usernameAttempt)
			{
				if (loginInfo[index].password == passwordAttempt)
					if(loginInfo[index].PIN == pinAttempt)
					{
						found = true;
						position = index;
						cout << index << "Hello";
					}
			}
			index++;
		}
		loginAttempts++;
		return position;
	}
	while (loginAttempts < MAX_ATTEMPTS && !position);
}
Last edited on
See your classmate's solution :)
http://www.cplusplus.com/forum/beginner/129350/
Thanks, but they have removed their code so I am not sure what was going on with their variables and stuff. Thanks though. :)
Topic archived. No new replies allowed.