Thread: Simple login/password check

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    14

    Exclamation Simple login/password check

    Hello I have to implement a simple user name and password checking algorithm. Valid user names and associated passwords are contained in a text file named passwords.txt . The form of this file is as follows:
    Code:
    ///passwords.txt
    
    admin 1
    pass1
    admin2
    pass2
    admin3
    pass3
    I need to have a loop that does not end unless both the user name and password are entered correctly. The code below works fine if the user name and password are entered correctly. What is does not do is repeat from the beginning if EITHER the user name or the password are incorrect. That is what I need with a message saying user name not found or password not found. Can anyone PLEASE help me with this! It is for an assignment which is complete except for this, I can't figure it out honest!

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    
    
    
    using namespace std;
    
    int main () 
        {
        
        string    firstName,
            lastName,
            userName,
            password;
        cout <<"\n";
        cout << setw(30) << left << "\n\tPlease login, login must consist of only TWO names seperated by a space:\n" << right ;
        cout << "\n\tEnter name:\t";
        cin >> firstName >> lastName;
        userName = firstName+" "+lastName;
        ifstream in("passwords.txt"); 
        string inbuf;
        while( !in.eof())
            {
            getline(in,inbuf);
            
            if(inbuf == userName)
                {
                cout << "\n\tUser: " << userName << " found.\n\n\tPlease enter your password: " ;
                cin >>password;
                getline(in,inbuf);
                                
                if(inbuf == password)
                    {
                    cout << "\n\tPassword verified."<< endl ;
                    
                    }
                else { 
                    cout << "\n\tPassword incorrect. " << endl;
                    
                    }
                
                }
                        
                
            }
        
        in.close();
        return 0 ;
        }
    Thanks for your time

    ceevee

  2. #2
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    
    
    
    using namespace std;
    
    int main () 
        {
        
        string    firstName,
            lastName,
            userName,
            password;
    ifstream in;
    	while(1)
    {
        cout <<"\n";
        cout << setw(30)<< "\n\tPlease login, login must consist of only TWO names seperated by a space:\n" ;
        cout << "\n\tEnter name:\t";
        cin >> firstName >> lastName;
        userName = firstName+" "+lastName;
        in.open("passwords.txt"); 
        string inbuf;	
        while( !in.eof())
            {
            getline(in,inbuf);
            
            if(inbuf == userName)
                {
                cout << "\n\tUser: " << userName << " found.\n\n\tPlease enter your password: " ;
                cin >>password;
                getline(in,inbuf);
                                
                if(inbuf == password)
                    {
                    cout << "\n\tPassword verified."<< endl ;
              
                    }
                else 
    				{ 
                    cout << "\n\tPassword incorrect. " << endl;
    				break;             
                    }
                
                }
                        
                
            }
    	cout<<"user name not found"<<endl;
    in.close();
     }  
        
        return 0 ;
        }
    is this what you need?

    blow me ... ...

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    14
    Yes almost.

    There are two things that happen now.

    1. The program does not terminate if both the user name and password are correct.

    2. It displays the message user name not found at the end even when both user name and password are correct.

    It does however stay in a loop if either are wrong so this is essential progress so thanks for that!

    This login function is incorporated into a much bigger program by renaming the main function to:

    int login ();

    login is then invoked from the main program. There is another module called menu which main invokes upon successful completion of the login function. I have tried this
    Code:
    cout << "\n\tPassword verified."<< endl ;
    menu ();
    after including menu.hpp which invokes the menu function at the correct point but when I try to quit the menu it goes back to the beginning of the login function.

    I'll compare your code to mine and see if I understand how to fix the problem.

    ceevee

  4. #4
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    
    
    
    using namespace std;
    
    
    void login()
    {
    string    firstName,
            lastName,
            userName,
            password;
    ifstream in;
    	while(1)
    {
        cout <<"\n";
        cout << setw(30)<< "\n\tPlease login, login must consist of only TWO names seperated by a space:\n" ;
        cout << "\n\tEnter name:\t";
        cin >> firstName >> lastName;
        userName = firstName+" "+lastName;
        in.open("passwords.txt"); 
        string inbuf;	
        while( !in.eof())
            {
            getline(in,inbuf);
            
            if(inbuf == userName)
                {
                cout << "\n\tUser: " << userName << " found.\n\n\tPlease enter your password: " ;
                cin >>password;
                getline(in,inbuf);
                                
                if(inbuf == password)
                    {
                    cout << "\n\tPassword verified."<< endl ;
              		return;
                    }
                else 
    				{ 
                    cout << "\n\tPassword incorrect. " << endl;
    				break;             
                    }
                
                }
                        
                
            }
    	cout<<"user name not found"<<endl;
    in.close();
     }  
    }
    
    
    
    int main () 
        {
        login();
        menu();
        
        return 0 ;
        }
    how about this one?

    blow me ... ...

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    14
    So VERY nearly there now. The only problem now is that if I deliberately enter the user name or the password incorrectly a few times it can't find a correct user name or password when I put that in sebsequently.

    I REALLY appreciate the help by the way!

    ceevee

    PS just had a thought, is there anyway of ensuring that it starts to search from the very first line each time? I think that maybe it reaches the EOF and searches from where it left off.
    Last edited by ceevee; 01-16-2005 at 09:08 AM.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    73
    If you are trying to add any security to this at all, I would not use a .txt file, or if I did, I would somehow create a code such as the letter w = x, x = y, etc... Or I would use hex codes, although both of these can be broken easily.

  7. #7
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    
    
    
    using namespace std;
    
    
    bool login()
    {
    string    firstName,
            lastName,
            userName,
            password;
    ifstream in;
        cout <<"\n";
        cout << setw(30)<< "\n\tPlease login, login must consist of only TWO names seperated by a space:\n" ;
        cout << "\n\tEnter name:\t";
        cin >> firstName >> lastName;
        userName = firstName+" "+lastName;
        in.open("passwords.txt"); 
        string inbuf;	
        while( !in.eof())
            {
            getline(in,inbuf);        
            if(inbuf == userName)
                {
                cout << "\n\tUser: " << userName << " found.\n\n\tPlease enter your password: " ;
                cin >>password;
                getline(in,inbuf);                           
                if(inbuf == password)
                    {
                    cout << "\n\tPassword verified."<< endl ;
                                    in.close();
              		return true;
                    }
                else 
    				{ 
                    cout << "\n\tPassword incorrect. " << endl;
    				in.clode();
    				return false;             
                    }
                }            
            }
    	cout<<"user name not found"<<endl;
    in.close(); 
    return false;
    }
    
    
    
    	int main () 
        {
        while(!login());
        menu();
        
        return 0 ;
        }
    tell me if it works fine
    Last edited by Hermitsky; 01-16-2005 at 09:28 AM.

    blow me ... ...

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    14
    Firstly for ElWhapo here is something of an explanation of what this is for. I am doing a degree in Electronics and because of the proliferation of micro controllers and other programmable devices there is an element of programming in my course. It's early days but last year we did structured programming and this semester we are being given an introduction to object oriented programming. This is part of assignment and is one of the 'extras' they have asked for in the assignment. So there is no real need for security features, it is merely a task.

    Hermitsky it now just quits when either the user name or password are incorrect, leading straight on to the menu function. It seems quite tricky this, I have done many functions for this assignment that I consider much more complex! I left this until almost last because I thought it would be easy!

    Its part of a bank account program by the way

  9. #9
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    i have edited my last code, try it again.

    blow me ... ...

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Edit: didnt know it was a task. Not going to give code.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    14
    Well that is ok Shatki but if you actually read the post from the beginning you will see that it is my initial code that is being modified, I have not come here asking anyone to do my assignment! It is a minute part of it that I cannot work out for myself, and I have tried believe me. I have spent HOURS on this. I read the guidelines and felt that what I was doing was legitimate. In the real world ( I am 39 years old and have spent a good deal of time in the real world) an engineer who won't ask for help is a fool.

    Rant over!

    So do I need to include a header for clode?

    ceevee

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    alright, ill give you a few hints of how I would do it:
    Use a map.
    Read everything into the map.
    Check input from user with the objects in the map.

  13. #13
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Here you go.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    int ScanFile(std::string UserName, std::string Password);
    bool goodup;
    
    int main()
    {
    	std::string UserName, Password;
    	
    	while (goodup != 1)
    	{
    		std::cout << "Please enter a username consisting of a first and last name, seperated by a single space: ";
    		std::getline(std::cin,UserName);
    	
    		std::cout << "\nPlease enter your password: ";
    		std::cin >> Password;
    				
    		ScanFile(UserName, Password);
    	}
    	
    	std::cin.get();
    	
    	return(0);
    }
    
    int ScanFile(std::string UserName, std::string Password)
    {
    	std::string temp;
    	
    	std::ifstream ifile("passwords.txt");
    		while (!ifile.eof())
    		{
    			std::getline(ifile, temp);
    			if (temp == UserName)
    			{
    				std::getline(ifile, temp);
    				if (temp == Password)
    				{
    					std::cout << "\nLogin successful.";
    					goodup = 1;
    					break;
    				}
    				else
    				{
    					std::cout << "\nBad password.\n\n";
    					break;
    				}
    			}
    			if (ifile.eof() && temp != UserName)
    			{
    				std::cout << "\nInvalid username.\n\n";
    			}
    		}
    	ifile.close();
    	
    	std::cin.ignore(80, '\n');
    	
    	return(0);
    }

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    14
    Ahhh yes I see it almost works but it needs you to hit enter twice instead of once. I can't see why either, this is a big help.

    Shaki I have been looking at Maps and will not use them but thanks for the introduction to STLs. These have not even been mentioned in our lectures.

    ceevee

  15. #15
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by ceevee
    Ahhh yes I see it almost works but it needs you to hit enter twice instead of once. I can't see why either, this is a big help.

    Shaki I have been looking at Maps and will not use them but thanks for the introduction to STLs. These have not even been mentioned in our lectures.

    ceevee
    You might be using a compiler that has a "debug" mode, where it inserts the equivilant of my std::cin.get() line in at the end of the program. Run the .exe standalone and you'll only have to hit enter once.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. allegro issues
    By mramazing in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2009, 11:56 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple check program
    By Shadow in forum C Programming
    Replies: 3
    Last Post: 06-05-2002, 06:20 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM