Essentially, I want the code to look in the file and try to find a matching user name and output a welcome message accordingly. Right now it gets hung up on the first word (this case being "Devin") and gets stuck in an infinite loop if you enter anything other than "Devin"
On ho, your opening a new file every time around the loop!!
When you open a new file it starts at the beginning again, no wonder it loops forever. In fact I am surprise you can compile this because you are returning an ifstream by copy, and I thought you couldn't copy streams....
You should only create one ifstream object for your file. fc.getInFile() should simply return a reference to that one copy.So something about the design of your class is not right.
Try making ifstream myFile a member of your class FileController and your call to getInFile() could then simply make sure the file is open. It should return a reference like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
class FileController
{
std::ifstream myFile; // private member
public:
public std::ifstream& getInFile() // return by reference &
{
if(!myFile.is_open())
{
myFile.open(MY_FILE_NAME_);
}
return myFile;
}
};
I don't see how the original could compile. I thought that streams were not copyable in C++. I would recommend posting the code that you are attempting to compile so that we can try it and see exactly what you are compiling and running. I'm not sure what the use of the FileController is. Using a wrapper class for the filestream object doesn't make sense to me.
I suppose I could get rid of the FileController class completely. It was originally a project requirement to have at least 5 classes. Now I have 7 so I could probably remove it.
I noticed the spelling error a few minutes after I posted my error. I was just focusing on the "must have a class/struct/union" part and not looking at the real problem ;)
One question I have: Why do you have 2 'public' declarations in the class file?
1 2
public:
public std::ifstream& getInFile()...
I thought the first 'public' would make everything after it public.
I'm getting this error now (even if I implement the second public):
error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
How big is the file? Does it make sense to just read the file at startup and build a name/score map? In the current design it appears that you will have to do the file i/o many times as you search for each user. I am not sure what the requirements are but it seems to me that you could read each line into a string using getline and std::string. Then you could break the string up and create a std::map<std::string, int> so that you have each players info stored. Then as the program continues to run you can simply search the map for the player name anytime you want to get information for a player. That is just one idea but I don't know enough about your requirements and your initial post only contained a small snip from the program so I really don't have a good sense of what you are trying to make the program do.
@kempofighter
I'll try to clear some of it up. The program is a guessing game that will store users names and their scores so the file can be infinitely long, depending on how many people play it (for now, it is very short).
This loop is trying to check if a user has previously played (ie there is a username existing in the file that the user is using). If it does, it displays a welcome back message. If not, it'll begin with a different message and once the game ends, the user and their score will append to the end of the file.
I have another loop that runs through the program line-by-line that prints out the user and their scores. This is a "viewHighScores()" function. The loop I'm working on now is seperate from the viewHighScores() function.
Here's an example of how this loop should work:
Username = "Devin";
run through file
"Devin" exists in file
"Welcome back, Devin!"
...
Username = "Joe"
run through the file
"Joe" does not exist in file
"Let's play, Joe!"
...
To make your list simple, you should iterate over all lines in the file and on each line split it. Once you done this you can store the results in a std::map<> for querying later.
Have a look at std::getLine() to retrieve an entire line from a file at a time.
Have a look at std::map to store the results.
I guess you could use strtok() to split; but you'd probably wanna write something a bit more robust that does it manually and puts some checks to ensure the format is correct.
Do I really need to store it? It's seems too simple of a check to see if the username string provided matches anywhere in the file to store it. I didn't think it'd make much sense trying to store the data in the program unless I'm overlooking something/not understanding.