1) I think your displayRank() function doesn’t need to return anything, so far.
2) You’d better not to declare variables before you really need them (avoid declaring all of them in a bunch at the beginning of functions).
3) You don’t need to declare a std::ifstream and later open it, but you can do anything in one statement:
std::ifstream infile("babynames2012.txt");
4) This is the most common way to check a std::istream status:
if (!infile) {
It evaluates to false for every problem.
5) You don’t need to check for EOF while reading a file, you just need to know if the last reading operation was successful or not. Please compare:
1 2 3
|
int rank;
std::string male, female;
while (infile >> rank >> male >> female) {
|
6) The main point is you use only one bool to manage two different conditions:
- a) has the name already being read between male names?
- b) has the name already being read between female names?
After having read the entire file, you can answer those questions:
- if (the name has NOT been read between female names) --> “the name is not ranked between...”
- if (the name has NOT been read between male names) --> “the name is not ranked between...”
I don’t think there’s a way you can give those answers before having read the entire file.