Thread: Comparing Character Arrays

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    23

    Comparing Character Arrays

    Hi this is code simply compares an entered string of characters with a binary file entry. The only problem is when the two character arrays are compared and are the same they program is trating it like they are the same. Could someone please find the flaw in my program.
    Code:
    	char UserName[10];
    	char Password[10];
    	char Accounts[10];
    
    	char EnteredUser[10];
    	char EnteredPassword[10];
    
    	long KeyCount = 0;
    
    	cout << "Enter User Name:";
    	cin >> EnteredUser;
    
    	cout << "\nEnter Use Password:";
    	cin >> EnteredPassword;
    
    	ifstream Users("Data/Users.db", ios::in || ios::binary);
    
    	while (!Users.eof())
    	{
    		Users.seekg(KeyCount, ios::beg);
    		Users.read(UserName,10);
    
    		//right here it isnt showing true when it should
    		if (EnteredUser == UserName)
    		{
    			KeyCount = KeyCount+10;
    			Users.seekg(KeyCount, ios::beg);
        		Users.read(Password,10);
    			
    			if (EnteredPassword != Password)
    			{
    				cout << "Incorrect Password";
    				return 0;
    			}
    
    			if (EnteredPassword == Password)
    			{
    				cout << "Login is good";
    				KeyCount = KeyCount + 10;
    				Users.seekg(KeyCount, ios::beg);
    				Users.read(Accounts, 10);	
    
    				if (Accounts == "Admin")
    				{
    					Users.close();
    					return 1;
    				}
    				else
    				{
    					Users.close();
    					return 2;
    				}
    			}
    			KeyCount = KeyCount - 10;
    		}
    		KeyCount = KeyCount + 30;	
    	}
    	cout << "Login Failed";
    	Users.close();
    	return 0;
    }

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    And wouldn't getline() me more appropriate here?:
    Code:
    cin >> EnteredUser;
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    23
    Thanks for the suggestion Salem everything is working good now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  2. Question on character arrays
    By Countfog in forum C Programming
    Replies: 9
    Last Post: 04-30-2008, 01:06 AM
  3. Character Arrays
    By JHaney in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2005, 12:53 PM
  4. multidimentional character arrays
    By ckeener in forum C++ Programming
    Replies: 7
    Last Post: 03-11-2005, 08:49 PM
  5. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 08:24 PM