Well here is most of it. I got bored though.
You can surely figure out how to write the rest of it. I have to get up for work in 3 hours lol.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <map>
//This defines class for our user data
//This is a very under-coded class, but you should get the picture.
class User
{
public:
User() {};//Constructor not needed with this class
~User(){};//Destructor not needed with this class
//This function returns const reference to the username
const std::string& getUser()
{
return m_user;
}
const std::string& getPassword()
{
return m_password;
}
void setUsername(const std::string& p_Username)
{
if ( m_user.length() == 0 )
m_user = p_Username;
}
//This function will ask the user for the old password before allowing them to change it.
//Unless the override parameter is true then it will do it without asking.
int setPassword(const std::string& p_Password, bool p_Override)
{
if (p_Override==false)
{
std::string t_password; //tempory password variable
std::cout << "\nPlease enter current password: ";
std::getline(std::cin,t_password,'\n');//get input until '\n'(newline) is found
if ( t_password.compare(m_password) == 0 ) //checks if they are exactly the same
{
m_password = p_Password; //assign new password to our member variable
std::cout << "\nYour new password is: " << m_password;
return 0; //Success
}
return -1;//Bad password
}
else
{
m_password = p_Password;
return 0;//Success
}
}
//This will take a string and return true if it matches the password, and false if it doesn't
bool Check(const std::string& p_Password)
{
if ( p_Password.compare(m_password) == 0 )
return true;
else
return false;
}
private:
/*
This is private data. Or data that can only be accessed by the class functions.
*/
std::string m_user; //string to hold the username (duh?)
std::string m_password;//string to hold the password (duh again?)
};
//forward declarations of our functions.
int LoadUsersFromFile(const std::string& file, std::map<std::string,User>& users);
int SaveUsersToFile(const std::string& file, const std::map<std::string,User>& users);
int main()
{
//Declare our program variables
bool quit = false;
std::string file = "users.xtu";
std::map<std::string,User> MyUsers;//a map of our users by username
//use our function to load the users
if ( LoadUsersFromFile(file,MyUsers) == 0 )
{
system("pause");
//Loaded users now continue program.
std::string input;
while (quit != true)
{
system("cls");//don't use this, but im lazy and don't feel like writing a better one.
std::cout.flush();
std::cout << "Welcome to Password Keeper.\n";
std::cout << "-=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
std::cout << "(1)Create New User \n";
std::cout << "(2)Delete Old User \n";
std::cout << "(3)Access Cur User \n";
std::cout << "(4)Purge User File \n";
std::cout << "(5)Quit program \n";
std::cout << "Selection: ";
std::cin >> input;
if ( input.length() > 0 )
{
switch(input.at(0))
{
case '1':
{
system("cls");
std::cout << "Create new user.\n";
std::string new_username;
std::string new_password;
std::cout << "Enter new username: ";
std::getline(std::cin,new_username);
std::cout << "Enter new password: ";
std::getline(std::cin,new_password);
MyUsers[new_username].setUsername(new_username);
MyUsers[new_username].setPassword(new_password, true);
}
break;
case '2':
{
system("cls");
std::cout << "Delete old user.\n";
std::string old_username;
std::cout << "Enter user to delete: ";
std::getline(std::cin,old_username);
if ( MyUsers.find(old_username)->first == old_username )
{
MyUsers.erase(MyUsers.find(old_username));
}
else
{
std::cout << "\nUser does not exist.";
system("pause");
break;//it doesn't exist
}
}
break;
case '3':
{
bool done = false;
std::string t_input;
while(done != true)
{
std::cout << "User access menu.\n";
std::cout << "-=-=-=-=-=-=-=-=-\n";
std::cout << "(1)Get User Info \n";
std::cout << "(2)Set UserName \n";
std::cout << "(3)Set PassWord \n";
std::cout << "(4)Return to Menu\n";
std::cin >> t_input;
if ( t_input.length() > 0 )
{
switch(t_input.at(0))
{
case '1':
{
//
}
break;
case '2':
{
//
}
break;
case '3':
{
//
}
break;
case '4':
{
//
}
break;
default:
done = true;
break;
}
}
}
}
break;
case '4':
{
//Purge user file
}
break;
case '5':
{
quit = true;
break;
}
break;
default:
{
quit = false;
break;
}
}
}
}
}
else
{
std::cout << "Error loading users file. Aborting program operation.\n";
system("pause");
return -1;
}
if ( SaveUsersToFile(file,MyUsers) != 0 )
std::cout << "Failed to save user information.\n";
system("pause");
return 0;
}
int LoadUsersFromFile(const std::string& file,std::map<std::string,User>& users)
{
std::ifstream load(file.c_str());
if(load) //if our filestream opened then continue
{
std::string t_user, t_password;
/*
This expects the file to be in this format
MyUserName$MyPassWord$MyOtherUserName$MyOtherPassword$
without any newlines.
*/
while(std::getline(load,t_user,'$') && std::getline(load,t_password,'$'))
{
users[t_user].setUsername(t_user);
users[t_user].setPassword(t_password,true);
}
load.close();
return 0;
}
else
return -1;//error failed to open file.
}
int SaveUsersToFile(const std::string& file, const std::map<std::string,User>& users)
{
return 0;
}
You need to finish the main() function. (not much more to do)
You also need to implement the SaveUsersToFile() function.(dont forget how the load function works and work it from there.)