User name and password program

Hi everyone,

I am just learning c++ in school. I missed out on a bonus question and I must finish it, for my own knowledge.

what i want to do, is take the login name and password and compare them against the two listed arrays. now my instructor said i can do it with a 2d array, however suggested that I stay parallel. the code i have written so far, validates that the user name and pass word is in the system and marks their position inside an empty array(this concept worked well for finding duplicates), i wish i could use text files like some of the snippets i see but my instructor wants us to stay in the learned knowledge until we get there. I have a background in HTML and python, so programming is not confusing, just having a struggle with this particular logic for some reason. Thanks
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 <iostream>
#include <string>
#include <iomanip>


using namespace std;

int main()
{
	int i = 0;
	int j = 0;
	int b = 0;
	int sizeOfArray = 7;
	int loginCount = 0;
	string login;
	string password;
	string userNames[] = {"User001","User002","User003","User004","User005","User006","User007",}; 
	string userPassword[] = {"password01","password02","password03","password04","password05","password06","password07"};
	string validLogin[] = {0,0,0,0,0,0,0,0};
	string validPassword[] = {0,0,0,0,0,0,0};

	cout << "Login: ";
	cin >> login;
	
	for(i;(i<sizeOfArray)&&userNames[i]!=login;++i)
		continue;

	if(i == sizeOfArray)
	{
		cout << "Invalid login";
	}
	else if(userNames[i] == login)	
	{
		validLogin[i] = "2";
	}
	
	cout << "Password: ";
	cin >> password;

	for(j;(j<sizeOfArray)&&userPassword[j]!=password;++j)
			continue;

	if(j == sizeOfArray)
	{
	cout << "Invalid Password";
	}

	else if(userPassword[j] == password)
	{
		validPassword[j] = "1";
	}
			
	cin.ignore();
	cin.get();
}
Here, analyze my code

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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;

#define usersize 4
#define passsize 4

int main(int nNumberofArgs,char* pszargs[])
{
    string user;
    string pass;
    string users[] = {"user001","user002","user003","user004"};
    string password[] = {"pass001","pass002","pass003","pass004"};

    for(;;)
    {
        cout << "Please login to an account, enter 'quit' as user to quit.\nRemember that this is case sensitive." << endl;
        cout << "Username: ";
        cin >> user;
        if(user == "quit")
        {
            cout << "Shutting Down..." << endl;
            system("PAUSE");
            return 0;
        }
        cout << "Password: ";
        cin >> pass;

        for(int i = 0;i < usersize;i++)
        {
            if(users[i] == user && password[i] == pass)
            {
                cout << "Login successful" << endl << endl;
                break;
            }
            else
            {
                cout << "Login failed" << endl;
                cout << "Either the user didn't exist or the user or password is incorrect..." << endl << endl;
                break;
            }
        }
        system("PAUSE");
        system("CLS");
    }
}
there i go over thinking things again, the only things i dont understand is for(;;) haven't seen that before and int main(nNumberofArgs, char* pszargs[]) this probably doent belong to this snippet i take it. other than that very helpful.
Sorry...You can't backspace when inputing...

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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;

#define usersize 4
#define passsize 4

int main(int nNumberofArgs,char* pszargs[])
{
    int i = 0;
    int admit = 0;
    string user;
    string pass = "";
    string backspace = " ";
    char passrelay;
    string users[usersize] = {"user001","user002","user003","user004"};
    string password[passsize] = {"pass001","pass002","pass003","pass004"};

    for(;;)
    {
        cout << "Please login to an account, enter 'quit' as user to quit.\nRemember that this is case sensitive." << endl;
        cout << "Username: ";
        cin >> user;
        if(user == "quit")
        {
            cout << "Shutting Down..." << endl;
            system("PAUSE");
            return 0;
        }
        cout << "Password: ";
        pass = "";
        for(;;)
        {
            passrelay = getch();
            if(passrelay == '\r')
            {
                break;
            }
            cout << "*";
            pass += passrelay;
        }
        cout << endl;

        for(i = 0;i < usersize;i++)
        {
            if(users[i] == user && password[i] == pass)
            {
                cout << "Login successful" << endl << endl;
                admit = 1;
                break;
            }
        }
        if(admit != 1)
        {
            cout << "Login failed" << endl;
            cout << "Either the user didn't exist or the user or password is incorrect..." << endl << endl;
        }

        admit = 0;

        system("PAUSE");
        system("CLS");
    }
}
Buttttttt, if you modify it to your needs, maybe you can.
I omitted a comment, i reinitialized a counter in the wrong spot, wasn't clearing the password, dooming it all to fail after first iteration, thanks this has been a great help, plus i have things to look up.
Last edited on
for(;;) = loop forever until you break;

ex.

for(int i = 0;i < 8;i++)

First part = initialize a variable to be used as a counter
Second part = loop until 'i' is < 8
Third part = increment 'i'

if you leave it all blank, it'll loop forever
Topic archived. No new replies allowed.