Thread: Problem with a loop reading lines of a file

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    40

    Problem with a loop reading lines of a file

    Hey guys,

    This may seem familiar to a few of you, I posted the same bit of code a couple days ago, but I have a different problem with it this time. The loop reads lines of a file that contains usernames and passwords. This is the whole function. Also, I know goto statements aren't optimal, but I plan on changing it to eliminate them once I have it running.
    Code:
    void newName() {
    	string userName, password;
    	string existingName;
    	int i = 0;
    
    	cout << "Opening username list info. . ." << endl;
    	ifstream userNameDB("users.data");
    
    	/*if (!userNameDB) {
    	    cout << "Error opening 'users.data'." << endl;
    	    cout << "Writing error report. . ." << endl;
    		writeError(0);
    		cout << "Returning to login:" << endl;
    		logIn();
            }*/
    
    	usernameCreate:
    	i = 0;
    	cout << endl << "Enter your desired username(4-12 characters, LETTERS ONLY): ";
    	cin >> userName;
    
    	if (userName.length() > 12 || userName.length() < 4) {
    		cout << endl << "Invalid input, check username length." << endl;
    		goto usernameCreate;
    	}
    
    	for (i = 0; userNameDB >> existingName; i++) {
    		if (userName == existingName) {
    		        cout << "Name already exists, please pick a different one. " << endl;
    			goto usernameCreate;
    		}
    	}
    
        userNameDB.close();
    
    	passwordCreate:
    	cout << "Enter a password(4-12 characters, LETTERS ONLY): ";
    	cin >> password;
    
    	if (password.length() < 4 || password.length() > 12) {
    		cout << "Invalid input, check username length." << endl;
    		goto passwordCreate;
    	}
    
    	cout << "Saving info. . ." << endl;
    	createName(userName, password);
    
    }
    If the file looks like this:
    Code:
    //users.data
    Username
    Password
    Username2
    Password2
    Username3
    Password3
    And I try to create another username "Username", it catches it the first time and tells me that it already exists. But if I try to create a username "Username" right after the first attempt, it's not caught that it already exists. I've been staring and thinking for at least an hour, I can't figure it out. Please help .

    Thanks in advance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Just because you go back to usernameCreate does not magically make the input file rewind itself. Fortunately there is a rewind function.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    That makes sense.. brb working some google magic.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    Found something that works:
    Code:
    userNameDB.seekg(0, ios::beg);
    Anything wrong with this method? Faults that I can't see?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That should work fine.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    And it does, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 12-23-2008, 01:53 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. reading file problem
    By samsam1 in forum Windows Programming
    Replies: 4
    Last Post: 01-15-2003, 06:03 PM