I've read this on some other questions here at DaniWeb so I've been trying to use the suggestions posted. They don't seem to be working correctly for what I need or I'm seriously confused (probably the latter). Anyways, I'd like to establish good programming practices early to avoid developing bad habits.

Here's the code:

int choice;
	cin  >> choice;
	switch(choice) {
		case 1:
			system("cls");
			{
				bool userExists = false;
				FileController fc;
				string tmp;
				char getData[50];
				while (!fc.getInFile().eof()) {
					fc.getInFile() >> tmp;
					if (tmp == userName) {
						cout << "Welcome back, "<< userName << "!\n\n" << endl;
						userExists = true;
						fc.getInFile().close();
						break;
					}
				}
				if (userExists == false) {
					cout << "Great! Let's begin!\n\n" << endl;
				}
			}
			break;
		case 2:
			. . .
		default:
			. . .

Essentially. A user inputs a name and are asked if they'd like to continue using the name (int choice). If they choose yes, the program checks if there's a matching username in the file and posts the "Welcome Back" message. Otherwise, the "Let's begin message."

One problem I do see using the .eof right now is the infinite loop I sometimes get when inputting a name that does not exist yet in the file.

Thanks in advance. You all here work wonders and have helped me in the past :)

'eof' is a flag on a stream; it signals that a failed read attempt to read past the end of a stream has occurred, in other words, by the time your loop comes to find out that the stream has failed, a complete iteration of that loop has already tried working with the failed read - therefore its already too late.

the correct idiom could be termed as "read while data exists to be read". Assuming that 'fc.getInFile()' returns an std::istream& (or ifstream&), your loop would look like

while ( fc.getInFile() >> tmp )
{
    // TODO: handle 'tmp'
}

Also, 'eof' isnt the only reason that a stream could fail. the above idiom catches all stream errors.

so why would this cause an infinite loop here?:

string line;
while(fc.getInFile() >> line) {
	cout << line << endl;
	cout << "---------------------------------------------" << endl;
}

HighScores.txt
---------------------------------------
Devin 101
Steve 465
Brian 546

the output of the above code spits out:
"Devin
---------------------------------------------" infinitely

What is the getInFile defined as?

Well, getInFile is a class function of class FileController, where FileController might be inherited from eos because of eof(). If so, getInFile is very specific to class FileController which is not part of c++ iostream library.

Therefore, nobody is able to state anything on the behaviour of getInFile nor the question "Using .eof for looping is bad practice?" can't be seriously answered, except one knows all implementation details of FileController class. Everything else tends to be guesswork of a kind.

-- tesu

Well, getInFile is a class function of class FileController, where FileController might be inherited from eos because of eof(). If so, getInFile is very specific to class FileController which is not part of c++ iostream library.

Therefore, nobody is able to state anything on the behaviour of getInFile nor the question "Using .eof for looping is bad practice?" can't be seriously answered, except one knows all implementation details of FileController class. Everything else tends to be guesswork of a kind.

-- tesu

Oh that's interesting. Now I'm even more confused, heh. The FileController class is as follows:

class FileController {
public:
	FileController();

	ifstream getInFile();

	ofstream getOutFile();
};

You still haven't provided the definition of getInFile(), but from the behaviour you describe what is likely happening is that you are associating a file with an ifstream in getInFile and then returning that ifstream. The problem is that each time you call getInFile() from within the loop conditional you are associating the same file (presumably) with a new ifstream which then always starts at the beginning of the file which always gives you the first string in the file. You never get beyond the first word with any given ifstream before you create a new ifstream with the next call to getInFile(). The only way you're likely to get out of the loop is to run out of stack space where each of the ifstream objects you've created reside, thereby causing the program to crash.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.