Vector Help

I'm trying to write a program that works as a login system.
- One user is pre-made in the code
- Through the GUI, that user can make other users and give them 'privileges'
- Privileges give a user the right to create other users or access protected data
- This file will interact with other files. The other files will contain the protected data.

*I'm brand new to C++ and fairly new to programming concepts, so any help or advice that you can give me regarding the project overall is appreciated. I still have not done my research on how to link external files and change the data in those files through the GUI. I have not researched how to make the GUI yet. All help is appreciated.

EDIT: Also, how would I make each method a different file and allow it to be called from the main file through the main method? E.G. Calling addUser(), an external method, from the main file.

***The Issue***
Currently, I'm getting this error on lines 9/10

10|error: 'usernamelist' does not name a type|
23|error: invalid use of member function (did you forget the '()' ?)|


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
  #include<vector>
#include<iostream>
#include<string>
using namespace std;
vector <string> passwordlist;
vector <string> usernamelist;
string adminUser[1] = {"James"};
string adminPass[1] = {"Nine"};
passwordlist.assign(adminPass, adminPass+1);
usernamelist.assign(adminUser, adminUser+1);
string UserEntered;
string PassEntered;
bool ValidUser;

bool signIn()
{
    cout << "Please sign in." << endl;
    cout << "Username: " << endl;
    cin >> UserEntered;
    cout << "Password: " << endl;
    cin >> PassEntered;

    for(int y = 0; y < passwordlist.size; y++)
    {
        if(UserEntered == usernamelist[y] && PassEntered == passwordlist[y])
        {
            ValidUser = true;
        }
        else if(UserEntered != usernamelist[y] || PassEntered != passwordlist[y])
        {
            cout << "Invalid Information";
            ValidUser = false;
        }
    }
    return ValidUser;
}
void adminMenu()
{
    int option;
    cout << "Welcome to the Administrator menu." << "\n" << "Please choose an option." << "\n";
    cout << "1. Add User" << "\n";
    cout << "2. Remove User" << "\n";
    cout << "3. Add file to directory";
    cin >> option;
    switch (option)
    {
    case 1:
        addUser();
        breakl
    case 2:
        //removeUser();
    }

}
void addUser()
    {

        cout << "Please verify your username and password." << endl;
        cout << "Username: " << endl;
        cin >> UserEntered;
        cout << "Password: " << endl;
        cin >> PassEntered;

        string info;
    do
    {
        cout << "Add a username then password.\n";
        cout << "Enter user name: ";
        cin >> info;
        usernamelist.push_back(info);
        cout << "Enter password: ";
        cin >> info;
        passwordlist.push_back(info);
        cout << "Showing user names and passwords entered...\n";
        for(int i = 0; i <usernamelist.size() && i < passwordlist.size(); i++)
            {
                cout << "User Name: " << usernamelist[i] << endl;
                cout << "Password: " << passwordlist[i] << endl;
            }
        cout << "\nContinue entering new usernames and passwords?";
        cin >> info;
    }
    while(info == "yes");
    }
int main()
{

}
Last edited on
Hi, you can't call the function assign or any other function in the global scope. You must do it inside another function (for example "main").
Ahh okay thanks.
What about at Line 23?
In addition to minomic's answer, on line 23, you missed the braces for size()
Wow lol
Such simple mistakes. Well all I need now is to figure out how to add the usernames to a separate file and call the methods from main from separate files.
Also, I guess my control flow is off because I keep having to rearrange the adminMenu method... it says not declared in scope.

Here is the updated 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include<vector>
#include<iostream>
#include<string>
using namespace std;
vector <string> passwordlist;
vector <string> usernamelist;
string adminUser[1] = {"James"};
string adminPass[1] = {"Nine"};
string UserEntered;
string PassEntered;
bool ValidUser;


bool signIn()
{
    cout << "Please sign in." << endl;
    cout << "Username: " << endl;
    cin >> UserEntered;
    cout << "Password: " << endl;
    cin >> PassEntered;

    for(int y = 0; y < passwordlist.size(); y++)
    {
        if(UserEntered == usernamelist[y] && PassEntered == passwordlist[y])
        {
            ValidUser = true;
        }
        else if(UserEntered != usernamelist[y] || PassEntered != passwordlist[y])
        {
            cout << "Invalid Information";
            ValidUser = false;
        }
    }
    return ValidUser;
}




void addUser()
    {

        signIn();
        if(ValidUser)
        {

            string info;
            startEntering:
            {
                cout << "Add a username then password.\n";
                cout << "Enter user name: ";
                cin >> info;
                usernamelist.push_back(info);
                cout << "Enter password: ";
                cin >> info;
                passwordlist.push_back(info);
                cout << "Showing user names and passwords entered...\n";
                for(int i = 0; i <usernamelist.size() && i < passwordlist.size(); i++)
                    {
                        cout << "User Name: " << usernamelist[i] << endl;
                        cout << "Password: " << passwordlist[i] << endl;
                    }
                cout << "\nContinue entering new usernames and passwords?";
                cin >> info;
                if(info == "yes")
                {
                    goto startEntering;
                }
                else
                {
                    adminMenu();
                }
            }
        }
        else
        {
                reSign();
        }
    }


public void adminMenu()
{
    int option;
    cout << "Welcome to the Administrator menu." << "\n" << "Please choose an option." << "\n";
    cout << "1. Add User" << "\n";
    cout << "2. Remove User" << "\n";
    cout << "3. Add file to directory";
    cin >> option;
    switch (option)
    {
    case 1:
        addUser();
        break;
    }

}

int reSign()
{
    string answer;
    cout << "\nWould you like to try again?";
    cin >> answer;
    if(answer == "yes" || answer == "YES" || answer == "Yes")
    {
        signIn();
    }
    else if(answer == "no" || answer == "No" || answer == "NO")
    {
        return(0);
    }
    else
    {
        cout << "Invalid input.";
    }
}




int main()
{
    passwordlist.assign(adminPass, adminPass+1);
    usernamelist.assign(adminUser, adminUser+1);
    signIn();
    if(ValidUser)
    {
        adminMenu();
    }
    else
    {
        reSign();
    }
}
By calling from main I mean

having adminMenu in its own file, then reSign in its own file etc.
This seems more organized. I've tried to do include but it didn't work for me.
Hiya,
Would you be up for learning classes? To me this screams out to have something like the following:

in LoginSystem.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// todo: add a header guard: http://en.wikipedia.org/wiki/Include_guard

class LoginSystem
{
public:
	bool signIn();
	int reSign();
	void addUser();
	void adminMenu();
private:
        // no one in the outside world (i.e. main) needs to see these variables
	vector <string> passwordlist;
	vector <string> usernamelist;
	string adminUser;
	string adminPass;
	string UserEntered;
	string PassEntered;
	bool ValidUser;
};


in LoginSystem.cpp:
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
#include"LoginSystem.h"
// plus other relevant includes

bool LoginSystem::signIn()
{
	cout << "Please sign in." << endl;
	cout << "Username: " << endl;
	cin >> UserEntered;
	cout << "Password: " << endl;
	cin >> PassEntered;

	for (int y = 0; y < passwordlist.size(); y++)
	{
		if (UserEntered == usernamelist[y] && PassEntered == passwordlist[y])
		{
			ValidUser = true;
		}
		else if (UserEntered != usernamelist[y] || PassEntered != passwordlist[y])
		{
			cout << "Invalid Information";
			ValidUser = false;
		}
	}
	return ValidUser;
}




int LoginSystem::reSign()
{
	string answer;
	cout << "\nWould you like to try again?";
	cin >> answer;
	if (answer == "yes" || answer == "YES" || answer == "Yes")
	{
		signIn();
	}
	else if (answer == "no" || answer == "No" || answer == "NO")
	{
		return(0);
	}
	else
	{
		cout << "Invalid input.";
	}
}

void LoginSystem::addUser()
{

	signIn();
	if (ValidUser)
	{

		string info;
	startEntering:
		{
			cout << "Add a username then password.\n";
			cout << "Enter user name: ";
			cin >> info;
			usernamelist.push_back(info);
			cout << "Enter password: ";
			cin >> info;
			passwordlist.push_back(info);
			cout << "Showing user names and passwords entered...\n";
			for (int i = 0; i < usernamelist.size() && i < passwordlist.size(); i++)
			{
				cout << "User Name: " << usernamelist[i] << endl;
				cout << "Password: " << passwordlist[i] << endl;
			}
			cout << "\nContinue entering new usernames and passwords?";
			cin >> info;
			if (info == "yes")
			{
				goto startEntering;
			}
			else
			{
				adminMenu();
			}
		}
	}
	else
	{
		reSign();
	}
}


void LoginSystem::adminMenu()
{
	int option;
	cout << "Welcome to the Administrator menu." << "\n" << "Please choose an option." << "\n";
	cout << "1. Add User" << "\n";
	cout << "2. Remove User" << "\n";
	cout << "3. Add file to directory";
	cin >> option;
	switch (option)
	{
	case 1:
		addUser();
		break;
	}

}


and then your main.cpp might look like this:

1
2
3
4
5
6
7
8
9
10
11
12
#include "LoginSystem.h"

int main()
{
	// create one system:
	LoginSystem mySystem;

	// then call whatever methods you want, for example:
	mySystem.addUser();
	mySystem.adminMenu();

}
Last edited on
Wow mutexe, I think that's it.

Thank you very much. I guess I have a lot of studying to do on classes.
Last edited on
Also, how can I make the vectors save before the program ends?
That was previous usernames and passwords entered are not lost?
'save' as in write these names to a file?

edit: just noticed a few other things:
1. don't use goto. Consider using some kind of loop instead.
2. Your reSign() method returns an int, but only 1 out of the 3 possible paths in that method you actually return an int (not a show-stopper, but it looks a bit weird).
3. same kind of thing with signIn(): you've told the compiler it returns a bool, which your function does indeed do, but you never test the return value of this.4
4. if you are going to use a class, make sure you initialise your class variables in your class constructor.


Last edited on
Yes.
Yes.


Have a read of this:
http://www.cplusplus.com/doc/tutorial/files/

You'd want to add a save() method to your class. either call it explicitly or call it in your destructor. Your save might want to iterate over your vectors and class variables and write these out to the file.
You might want to then add a load() to do the reverse of your save()
Last edited on
I'm just gonna read through the tutorials a few times.
Thanks for all of your help.
Topic archived. No new replies allowed.