Input, Output, and everything in between. A cry for help.

This is a discussion on Input, Output, and everything in between. A cry for help. within the C++ Programming forums, part of the General Programming Boards category; What I am working on ( My first program) is a password keeper that would store and hold your passwords ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    26

    Input, Output, and everything in between. A cry for help.

    What I am working on ( My first program) is a password keeper that would store and hold your passwords for you and is basically user friendly. There is a problem which i am encountering however and is this:
    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int input;
        int second_input;
        std::string master_password;
        std::string first_password;
        std::string second_password;
        std::string third_password;
        std::string fourth_password;
        std::string first_password_name;
        std::string second_password_name;
        std::string third_password_name;
        std::string fourth_password_name;
        
        cout<<"You have initiazilized Password Keeper,\n";
        cout<<"a program which stores your passwords \n";
        cout<<"in a password protected enviorment.\n";
        cout<<" \n";
        do
        {
            cout<<"\n";
            cout<<"Please enter the password to access your \n";
            cout<<"passwords: ";
            cin>> input;
            cin.ignore();
           
                if ( input == master_password )
                {
                    cout<<" \n";
                    cout<<"Access Granted.\n";
                    cout<<"\n";
                    cout<<"1."<< first_password_name<<".\n";
                    cout<<"2."<< second_password_name<<".\n";
                    cout<<"3."<< third_password_name <<"\n";
                    cout<<"4."<< fourth_password_name<<".\n";
                    cout<<"5.Password Managment.\n";
                    cout<<"Input the password you would desire: ";
                    cin>> second_input;
                    cin.ignore();
                    switch ( second_input )
                    {
                        case 1:
                        cout<<"\n";
                        cout<<"Your "<< first_password_name<<" is "<< first_password <<".\n";
                        break;
                        case 2:
                        cout<<"\n";
                        cout<<"Your "<< second_password_name<<" is "<< second_password<<"\n";
                        break;
                        case 3:
                        cout<<"\n";
                        cout<<"Your"<< third_password_name<<" is "<< third_password <<"\n";
                        break;
                        case 4:
                        cout<<"\n";
                        cout<<"Your"<< fourth_password_name<<" is "<< fourth_password <<"\n";
                        break;
                        case 5:
                        cout<<"\n";
                        cout<<"Password Managment accessed.";
                        do
                        {
                            cout<<"\n";
                            cout<<"1.Edit Master password.\n";
                            cout<<"2.Edit First password name.\n";
                            cout<<"3.Edit First password.\n";
                            cout<<"4.Edit Second password name.\n";
                            cout<<"5.Edit Second passsword.\n";
                            cout<<"6.Edit Third password name.\n";
                            cout<<"7.Edit Third password.\n";
                            cout<<"8.Edit Fourth password name.\n";
                            cout<<"9.Edit Fourth password.\n";
                            cout<<"10.Quit.\n";
                            cout<<"What would you like to do: ";
                            cin>> input;
                            
                            switch ( input )
                            {
                                case 1:
                                {
                                    ofstream myfile ("master_password.txt");
                                    cout<<"Enter the master password you would desire: ";
                                    cin>> master_password;
                                    myfile << master_password;
                                    myfile.close();
                                    input<< 0;
                                    break;
                                }
                                case 2:
                                {
                                    ofstream fpnfile ("first_password_name.txt");
                                    cout<<"\n";
                                    cout<<"\n";
                                    cout<<"Enter the new name of you first password i.e.'Email': ";
                                    cin>>first_password_name;
                                    fpnfile << first_password_name;
                                    fpnfile.close();
                                    input<< 0;
                                    break;
                                }
                                case 3:
                                {
                                    ofstream fpfile ("first_password.txt");
                                    cout<<"\n";
                                    cout<<"\n";
                                    cout<<"Enter the new atcual first password: ";
                                    cin>>first_password;
                                    fpfile << first_password;
                                    fpfile.close();
                                    input<< 0;
                                    break;
                                }
                            }
                        }
                        while ( input !=10, input < 11 );
                        break;
                        
                        default:
                        cout<<"Incorrect Selection";
                        break;
                    }
                }
                
                else
                {
                    cout<<"\n";
                    cout<<"Access Denied.\n";
                    break;
                }
            }
            while ( input != 908 )
                ;
            cin.get();
        }
    This is my code as it stands now. The problems I am facing are that the input == master_password says it does not recognize "operator==" so I was wondering if there is another way to check if the input by the user is equal to the stored password. The other problem or question i have is that I am storing all the passwords in different files which when ever that string inside the file was needed, the file would have to be opened then closed. So I was wondering if it was more efficient to somehow store all the passwords in one file so i wouldn't have to open each and every file for outputting the password names and actual passwords and how I could do that.

    Thanks!,

    A13W

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    umm, you are trying to compare an int to a string of course it won't work.
    make the input a string as well. Then parse it for numbers you may need. Also if you want the password to be able to have spaces you will need to use the std::getline() function provided in <string>.
    Your code needs some real rethinking though. Have you thought of maybe breaking it into functions or even better classes? I realize classes may be beyond your expertise, but functions would be very helpful in this case.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    Ok. Sorry i dont know what that means but, parse?

    And so, for functions, could you give me an idea on how to break it up?

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Actually I am writing a class version of it right now. It will take a bit, but i am commenting it all as i go.
    I can't think of how to explain parse, but maybe when i post the code you will be able to understand better.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    Ok, thank you very much.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I am going to have to finish this in the morning, but I will PM you with the code and/or files. Sorry for the inconvenience.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    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.)
    Last edited by Raigne; 03-01-2008 at 01:12 PM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Just a thought concerning separation of concerns: instead of having an option to ask the user for the password in User::setPassword, it would be better to just set the password, then have a free function that handles asking the user for the password and setting the password by calling User::setPassword. This way setPassword only handles setting the password of the User, not asking for the password.

    getUser(), getPassword() and Check() should be const member functions.

    Incidentally, ( m_user.length() == 0 ) can be simplified to ( m_user.empty() ), and ( p_Password.compare(m_password) == 0 ) can be simplified to ( p_Password == m_password ).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    I think I understand most of it.... So does it actually store passwords in a file? and where could i see that in the code so I could understand it better?

    int LoadUsersFromFile(const std::string& file, std::map<std::string,User>& users);

    And this, I just have a few questions, what does the & do and map<std::string,User>& users);
    how does that affect the program?
    Sorry I am asking so many questions.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    The & makes it a reference to an existing variable. Without the & it would make a copy of the std::map instead of changing the contents of the one we pass to it.

    A13W, no it does not save the users/passwords to a file. I made a function for it, you ujst need to fill it in. Look at the load from file function,and write the save function basically the same way. Just saving instead of loading.

    laserlight, yea thanks for clarifying. I was extremely tired when i was writing, and i was just trying to get the point across.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Note:
    Code:
    input<< 0;
    That doesn't do anything.

    The & makes it a reference to an existing variable. Without the & it would make a copy of the std::map instead of changing the contents of the one we pass to it.
    Elaboration about references: http://www.cprogramming.com/tutorial/references.html

    Information about maps: http://www.cprogramming.com/tutorial/stl/stlmap.html

    Tutorial index: http://cprogramming.com/tutorial.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21