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 '()' ?)|
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.
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_guardclass 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;
};
#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;
}
elseif (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();
}
elseif (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();
}
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.
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()