Display rank from txt file

Wrote some code to display rank of an entered name off of "babynames2012.txt."

It compiles, but does not display anything. Tried passing by reference, but that doesn't affect anything.

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
#include <iostream>
#include <fstream>
#include <string>

std::string displayRank(std::string, std::string, std::string, int);

int main() {
	
	std::string name;

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

	std::string displayRank(name);

	system("pause");
}

std::string displayRank(std::string name, std::string male, std::string female, int rank) {
	
        std::ifstream infile;
	infile.open("babynames2012.txt");
	
        if (infile.fail()) {
		std::cout << "There was an error";
	}
	while (!infile.eof()) {
		
		infile >> rank >> male >> female;

		if (name == male) {
			std::cout << name << " is ranked " << rank << " in popularity among boys.";
			break;
		}
		else {
			std::cout << name << " is not ranked among the top 1000 boy names of 2012.";
			break;
		}
		if (name == female) {
			std::cout << name << " is ranked " << rank << " in popularity among girls.";
			break;
		}
		else {
			std::cout << name << " is not ranked among the top 1000 girl names of 2012.";
			break;
		}

	}
	return name;
}
Last edited on
This is not a function call:
 
	std::string displayRank(name);

It is a variable definition that creates a string called displayRank initialized to the value of name.

And the displayRank function takes 4 strings, not just one. So it needs to be called something like:

 
	std::string ret = displayRank(name, male, female, rank);

Although I'm not sure what all those parameters do.
Last edited on
@tpb

It only displays correct answer for rank 1. Shouldn't it find the entered name(if on list) while searching through end of file?

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
std::string displayRank(std::string);

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

	std::cout << "Enter a name to find rank: ";
	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;

	infile.open("babynames2012.txt");

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

		if (name == male) {
			std::cout << name << " is ranked " << rank << " in popularity among boys.";
			break;
		}
		else {
			std::cout << name << " is not ranked among the top 1000 boy names of 2012.";
			break;
		}
		if (name == female) {
			std::cout << name << " is ranked " << rank << " in popularity among girls.";
			break;
		}
		else {
			std::cout << name << " is not ranked among the top 1000 girl names of 2012.";
			break;
		}

	}
	return name;
}
Last edited on
It's those break statements. You're thinking of a switch, where the breaks are needed. They don't do anything related to if/else, but they do break out of the nearest containing loop. So the first break quits your while loop.

Also, I don't think you want it to say "So-and-so is not ranked among..." over and over and over again for every line in the file. Maybe just comment those out for now to get the other part working, but you need to print that message (if appropriate) after the while loop, so you'll need some way of remembering if you found a boy and/or girl name.

Last edited on
@tpb

You're right. Makes sense how the break statements would take it out of the loop.
I'll figure out how to display the "not ranked.

Thanks for your help!
Last edited on
Topic archived. No new replies allowed.