Baby name rank

Hello. Writing program to read file and display rank of an entered name. It must display the ranking on the boy list & girl list. Only problem I'm having is when I enter a name that is on both lists, it returns this:

1
2
3
4
5
6
Enter a name to find rank:
Justice
Justice is ranked 518 in popularity among girls.
Justice is not ranked among the top 1000 boy names.
Justice is ranked 519 in popularity among boys.
Justice is not ranked among the top 1000 girl names.

How can I prevent it from displaying the "not" statements if the name is ranked, because putting the not statements outside of the while loop gives even funkier results.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <fstream>
#include <string>

std::string displayRank(std::string);

int main() {
	
	std::string name, male, female;
	int rank;

	std::cout << "Enter a name to find rank: \n";
	std::cin >> name;

	std::string findRank = displayRank(name);

	system("pause");
}

std::string displayRank(std::string name) {

	std::string male, female;
	int rank;
	std::ifstream infile;
	bool useName = false;

	infile.open("babynames2012.txt");

	if (infile.fail()) {
		std::cout << "There was an error.\n";
	}
	
	while (!infile.eof()) {
		
		infile >> rank >> male >> female;

		if (name == male) {
			std::cout << name << " is ranked " << rank << " in popularity among boys.\n";
			useName = true;
			if (!useName) {
				std::cout << name << " is not ranked among the top 1000 boy names.\n";
			}
			if (useName) {
				std::cout << name << " is not ranked among the top 1000 girl names.\n";
			}
		}
		if (name == female) {
			std::cout << name << " is ranked " << rank << " in popularity among girls.\n";
			useName = true;
			if (useName) {
				std::cout << name << " is not ranked among the top 1000 boy names.\n";
			}
			if (!useName) {
				std::cout << name << " is not ranked among the top 1000 girl names.\n";
			}
		}
	}
	return name;
}
Last edited on
Why are you printing information about girl names in your "male" if statement?

The same with the boy's information in the "female" if statement.

By the way have you studied if() else clauses yet?
Last edited on
@jlb,

Because, a male name could also be a female name. So the questions says that if it a name doesn't appear in the opposite gender's list, to print that.

So if Aiden is in the list of boys, but not girls, I need to return that.

And yes I have studied if else, but when I use an else statement, it prints it for all 1000 entries.
But if the name can be both a girl and a boy shouldn't that name be in both lists?

Right. For example, Justice is in both lists, so I don't need it to display the "not" statements, and rather print the rank for both lists.
Okay so the only time you need to print "the name doesn't appear" is if the name is in neither of the lists, which is why I asked if you knew about and considered an if() else clause.

And yes I have studied if else, but when I use an else statement, it prints it for all 1000 entries.

Show what you tried. What exactly do you mean by "it"?

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.
Topic archived. No new replies allowed.