Search for a String and Find the Rank

Pages: 123
I was assigned a problem where I have to ask a user to enter a name he or she would like to search for. I have a text file of the most popular 1000 male and female names, so 2000 names total. The format is the rank (i.e. 1, 2, 3, etc), the male name, and then the female name. So 1000 lines of a number a male name and a female name.

I have to search the file for the name, and if it is found, I say to the user "name" was the "rank" most popular name. I'm limited to basic things like while loops, for loops, and if statements. I'm not sure how to even go about coding this. I was thinking of putting a while loop inside of a count controlled for loop, but I'm having problems setting it up and I'm not sure if it's even the best way to solve the problem. I would prefer to have a sentinel controlled loop, but I'm not sure if that is possible here. I'd appreciate any suggestions or ideas to give me a hint for how to write the code for this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int rank;

string male,female,user_name;
cin>>user_name;
 while(!file.eof())
{
  file>>rank>>male>>female;
   if(user_name==male)
{
  cout<<user_name<<"was"<<rank<<endl;
 break;
}
else if(user_name==female)
{
cout<<user_name<<"was"<<rank<<endl;
break;
}

}
Since I'm new to programming and the course I'm in probably doesn't teach someone the best way to begin, I may be not thinking correctly about this problem.

first line looks like this:
1 Bob Mary

I guess I was thinking more like how I I might read the line and not how the compiler would read it. Would the compiler read the 1, male name, and female name as three separate strings?

I want the program to check the name for both boys and girls and then have the program say the rank, if applicable, for both genders. I think my problem is how I'm writing my function. First thing is I guess string cannot be a function data type. Is that correct? The other thing is I should probably have more functions. A function for searching the file and another function for output? Does that sound right?

Here is my code so far:

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
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string> 
#include <fstream>
using namespace std;

string name_search (string);	//Function Prototype
//Searches a text file for a name and outputs the rank of the name to the screen

int main ()
{
	string name;
	char answer;

	do 
	{
	
	cout << "This program allows you to search for the rank of a name from a list of the" 
		 << "1000 most popular male and female baby names of 2004.\n";
	cout << "Enter the name you would like to search for: ";
	cin >> name;

	name_search (name);

	cout << "\nWould you like to run this program again? (y or n)\n";
	cin >> answer;
	}
	while (answer == 'y' || answer == 'Y');

	return 0;
}


	string name_search (string name)
	{
		int rank;
		string male, female; 
		ifstream infile;
		
		infile.open ("name_list.txt");

		if (infile.fail())
		{
			cout << "The file was corrupt.\n";
		}


		while (!infile.eof())
		{
			infile >> rank >> male >> female;

			if (name == male)
			{
				cout << name << " is ranked " << rank << " in popularity among boys.\n";
			}
			else
			{
				cout << name << "is not ranked among the top 1000 boys names.\n";
			}
			if (name == female)
			{
				cout << name << " is ranked " << rank << " in popularity among girls.\n";
			}
			else
			{
				cout << name << "is not ranked among the top 1000 girl names.\n";
			}
		}
	}
I think you have done everything correct,except onething
You have to pass ifstream class and its object infile as reference to the function
because like cin ,infile>>rank>>male>>female doesnt work without proper calling in a function.

so just change your function heading to

string name_search (ifstream& infile,string name)


If you still have problem, please provide error output.
Last edited on
I'm having a couple problems.

First, I don't know if I can use string as the data type for a function. I'm thinking I should use a void function anyway, but is it possible to have a string data type function?

Second, when I run the program and enter a name the whole text file is scanned and there is no output. I realized I had to call infile.close() in the if statement so that the entire file wasn't scanned. The file still scans until the name is found though. The output of the program is below. I didn't edit the output.

This is the function that is producing the output below.
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
void name_search (string name)
	{
		int rank;
		string male, female; 
		ifstream infile;
		
		infile.open ("babynames2004.txt");

		if (infile.fail())
		{
			cout << "The file was corrupt.\n";
		}


		while (!infile.eof())
		{
			infile >> rank >> male >> female;

			if (name == male)
			{
				cout << name << " is ranked " << rank << " in popularity among boys.\n";
				infile.close ();
			}
			else
			{
				cout << name << " is not ranked among the top 1000 boys names.\n";
			}
			if (name == female)
			{
				cout << name << " is ranked " << rank << " in popularity among girls.\n";
				infile.close ();
			}
			else
			{
				cout << name << " is not ranked among the top 1000 girl names.\n";
			}
		}
	}



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
This program allows you to search for the rank of a name from a list of the 1000
 most popular male and female baby names of 2004.

Enter the name you would like to search for: Jordan
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is ranked 43 in popularity among boys.
Jordan is not ranked among the top 1000 girl names.
Jordan is ranked 43 in popularity among boys.
Jordan is not ranked among the top 1000 girl names.

Would you like to run this program again? (y or n)
The problem you didnt user break; statement in the if else statements.
If you dont use break statement, while (!infile.eof()) will be executed until its read end of file.

Your code should be like this
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string> 
#include <fstream>
using namespace std;

string name_search (string);	//Function Prototype
//Searches a text file for a name and outputs the rank of the name to the screen

int main ()
{
	string name;
	char answer;

	do 
	{
	
	cout << "This program allows you to search for the rank of a name from a list of the" 
		 << "1000 most popular male and female baby names of 2004.\n";
	cout << "Enter the name you would like to search for: ";
	cin >> name;

	name_search (name);

	cout << "\nWould you like to run this program again? (y or n)\n";
	cin >> answer;
	}
	while (answer == 'y' || answer == 'Y');

	return 0;
}


	string name_search (string name)
	{
		int rank;
		string male, female; 
		ifstream infile;
		
		infile.open ("male.txt");

		if (infile.fail())
		{
			cout << "The file was corrupt.\n";
		}


		while (!infile.eof())
		{
			infile >> rank >> male >> female;

			if (name == male)
			{
				cout << name << " is ranked " << rank << " in popularity among boys.\n";
				break;
			}
			else
			{
				cout << name << "is not ranked among the top 1000 boys names.\n";
				break;
			}
			if (name == female)
			{
				cout << name << " is ranked " << rank << " in popularity among girls.\n";
				break;
			}
			else
			{
				cout << name << "is not ranked among the top 1000 girl names.\n";
				break;
			}
		}
		return name;
	}
Not using break statements was wort of my issue with the pass by reference value. This homework is due the 25 and I was trying to get it done early. I wanted to use something like pass by reference but we didn't go over it in class, so I wasn't sure if I could use it on the homework. The teacher does not like us to use things not covered in class. Luckily it was covered in class today so i can use it. The other thing is the teacher said we can't use break statements in anything other than switches.

I was thinking about converting the if statement to a switch. I assume the way I would do that is have a temporary variable to hold the male name and the female name and use those variables as cases in the switch. It's a pain to write code in this class because it's seems like its more of lets play this game, rather than let's wright the best code we can to solve this problem.

If you dont want use break, then use some bool variable and check for condition
for e.g
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
void name_search (string name)
	{
		int rank;
		string male, female; 
		ifstream infile;
		bool found=false;
		infile.open ("male.txt");

		if (infile.fail())
		{
			cout << "The file was corrupt.\n";
		}


		while (!infile.eof()&& found==false)
		{
			infile >> rank >> male;

			if (name == male)
				found=true;
			else if (name == female)
				found=true;
						
		}
		if(found==true)
			cout<<rank<<" "<<name;
		else
			cout<<" No name"<<endl;
		

	}


I changed the function to a void, because you are not using returned value name from the function anywhere in the main
I did change the function to a void even though I didn't edit the code I posted earlier. I would use a break statement, but the teacher said it's bad programming and she does not allow it used outside of switch statements.

The assignment says the output should look like this:

For example, if the user enters the name “Justice” then the program should output:

Justice is ranked 406 in popularity among boys.
Justice is ranked 497 in popularity among girls.

If the user enters the name “Walter” then the program should output:

Walter is ranked 366 in popularity among boys.
Walter is not ranked among the top 1000 girl names.



With this code,

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
void name_search (string name)
	{
		int rank;
		string male, female; 
		ifstream infile;
		bool found = false;
		
		infile.open ("babynames2004.txt");

		if (infile.fail())
		{
			cout << "The file was corrupt.\n";
		}


		while (!infile.eof() && found == false)
		{
			infile >> rank >> male >> female;

			if (male == name)
			{
				cout << name << " is ranked " << rank << " in popularity among boys.\n";
				found = true;
				infile.close ();
			}
			else
			{
				cout << name << " is not ranked among the top 1000 boys names.\n";
			}
			if (female == name)
			{
				cout << name << " is ranked " << rank << " in popularity among girls.\n";
				infile.close ();
				found = true;
			}
			else
			{
				cout << name << " is not ranked among the top 1000 girl names.\n";
			}
		}
	}



the output looks like this

This program allows you to search for the rank of a name from a list of the 1000
 most popular male and female baby names of 2004.

Enter the name you would like to search for: Jordan
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.
Jordan is ranked 43 in popularity among boys.
Jordan is not ranked among the top 1000 girl names.

Would you like to run this program again? (y or n)


The out should say that
Jordan is ranked 43 in popularity among boys
and
Jordan is ranked 70 in popularity among girls.

How do I make it so all the names prior to the name match are not output? Also how do I have the program properly find the girl name match?
I guess my biggest question is how does the compiler read the line below?
1 Jacob Emily

This seems to be my biggest issue because I'm not sure how the compiler is addressing the code so I'm not sure how to troubleshoot the issue. I currently have the rank as an int and the male and females names as strings. Should I make the rank a string also? Does the compiler see int(1), string(male), string(female)?

There are two issues. One is that the else statement keeps being executed until the matching name is encountered. The other issue is that the female name match is not occurring and there is no output for the female name?

I just need to find out the fundamental problem and then the code seems like it would be fairly simple after that.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

ifstream fin("file.txt")
string search;
cin>>search;

int rank=1;
const char *n=name.c_str();
n+=2;
while (!fin.eof)
{
    getline(fin, name); //1 jacob emily
    name.erase(0,2);
    if (search.c_str()==n)
        break;
    rank++;
}

if (rank!=1000)
{
cout<<name<<" is rank "<<rank;
}



or if you just want to search the first name...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ifstream fin("file.txt")
string search;
cin>>search;

int rank=1;
while (!fin.eof)
{
    //1 jacob emily
    fin>>name; //name=1
    fin>>name; //name=jacob
    if (search==name)
        break;
    fin>>name;
    rank++;
}

if (rank!=1000)
{
cout<<name<<" is rank "<<rank;
}
Last edited on
Is the ifstream stream object is calling the function erase? We don't talk about anything like this in class, but knowing why everything is used and the exact names really helps me understand the language.

The teacher is very strict and wouldn't let me use the erase function. Is there a way around this? I'm confined to very little techniques so I need to be able to code this problem in a very strict way. That is what is making this so frustrating. I really appreciate the help. Hopefully a solution can be found.

I can't use a break with an if statement.
Last edited on
I just need help refining this code to get the output I want. It sucks, but I cannot really add additional structures to this code. I was thinking I would have to use getline but I'm not sure how to implement it into the code below.

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
void name_search (string name)
	{
		int rank;
		string male, female; 
		ifstream infile;
		bool found = false;
		
		infile.open ("babynames2004.txt");

		if (infile.fail())
		{
			cout << "The file was corrupt.\n";
		}


		while (!infile.eof() && found == false)
		{
			infile >> rank >> male >> female;

			if (male == name)
			{
				cout << name << " is ranked " << rank << " in popularity among boys.\n";
				infile.close ();
				found = true;
			}
			else 
			{
				cout << name << " is not ranked among the top 1000 boys names.\n";
			}
			if (female == name)
			{
				cout << name << " is ranked " << rank << " in popularity among girls.\n";
				infile.close ();
				found = true;
			}
			else
			{
				cout << name << " is not ranked among the top 1000 girl names.\n";
			}
		}
	}
Last edited on
then use the 2nd code i posted...

just do
string temp="";
fin>>temp;
fin>>name;
temp+" "+name;

to get the full name;
Last edited on
Does that allow me to get check both the male name and female name on each line?

So I guess based on what the teacher confined me to I can't make code that is more I guess "self sufficient"? I mean I would have to know the rank and first name right since I can't use more advanced techniques?

I need to get the rank, the male name, and the female name on each line. I need those three things from each line.
Last edited on
theeright, can you write the code out. I'm not sure what your saying.

Sorry if I'm being a pain, but this is taking ridiculously long becuase of these stupid limitations and I have a ton of organic chemistry, polymer science and material characterization to do.

The >> operator extracts formatted input from the stream buffer (in your case, a file). By formatted, we mean ASCII characters. The operator ignores all white spaces characters, extracts characters until it hits an unexpected (a decimal point while extracting an integer) or white space character. The code: infile >> rank >> male >> female is perfect for your situation.

If you were to use getline() you would have one string that looked something like this: "1 John Mary", and it would be up to you to figure out how to parse the string into its proper sections.

vichu8888s code does not work?


I would have to tweak his code to fit what i need the program to do, unless I'm reading the code wrong.

The user enters a name.

The program calls a function that should attach to a file and check the file for that name in both the male and female categories.

The male "category" is the 2nd string (first string if 1 counts as an int), and the female "category" is the 3rd string (or 2nd string based on the above logic).

The program them tells the user what the rank was for both the male and the female or tells if the name did not show up for one of them.
Alright, I tweaked vichu8888s code around a little. Here is what I have now.

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
void name_search (string name)
	{
		int rank;
		string male, female; 
		ifstream infile;
		bool male_found = false, female_found = false;


		infile.open ("babynames2004.txt");
				
		if (infile.fail())
		{
			cout << "The file was corrupt.\n";
		}


		while (!infile.eof() && male_found == false && female_found == false)
		{
			infile >> rank >> male;

			if (name == male)
				male_found=true;
			else if (name == female)
				female_found=true;
						
		}

		if(male_found == true && female_found != true)
		{
			cout << name << " is ranked " << rank << " in popularity among boys.\n";
			cout << name << " is not ranked among the top 1000 girl names.\n";
		}
		else if (male_found != true && female_found == true)
		{
			cout << name << " is not ranked among the top 1000 boys names.\n";
			cout << name << " is ranked " << rank << " in popularity among girls.\n";
		}
		else if (male_found == true && female_found == true)
		{
			cout << name << " is ranked " << rank << " in popularity among boys.\n";
			cout << name << " is ranked " << rank << " in popularity among girls.\n";
		}
		else if (male_found != true && female_found != true)
		{
			cout << name << " is not ranked among the top 1000 boys names.\n";
			cout << name << " is not ranked among the top 1000 girl names.\n";
		}
	}


I only get the proper output for Jacob. Here it is:

This program allows you to search for the rank of a name from a list of the 1000
 most popular male and female baby names of 2004.

Enter the name you would like to search for: Jacob
Jacob is ranked 1 in popularity among boys.
Jacob is not ranked among the top 1000 girl names.

Would you like to run this program again? (y or n)



I do not get any output for any other name. Nothing even happens after I enter a name.

I'm closer to a solution, but I'm still having a problem. Does anybody know what the problem is now?
Okay, I just added a variable for the female name since the code vichu8888 wrote was just for the male name. This allowed the names on the first line of the text file to be properly output. With other names the output is not correct.

Here is some output:

This program allows you to search for the rank of a name from a list of the 1000
 most popular male and female baby names of 2004.

Enter the name you would like to search for: Jacob
Jacob is ranked 1 in popularity among boys.
Jacob is not ranked among the top 1000 girl names.

Would you like to run this program again? (y or n)
y
This program allows you to search for the rank of a name from a list of the 1000
 most popular male and female baby names of 2004.

Enter the name you would like to search for: Emily
Emily is not ranked among the top 1000 boys names.
Emily is ranked 1 in popularity among girls.

Would you like to run this program again? (y or n)
y
This program allows you to search for the rank of a name from a list of the 1000
 most popular male and female baby names of 2004.

Enter the name you would like to search for: Jordan
Jordan is ranked 43 in popularity among boys.
Jordan is not ranked among the top 1000 girl names.

Would you like to run this program again? (y or n)



The first line of the text file is "1 Jacob Emily". The code works for Jacob and Emily. The code does not produce the correct output for the name Jordan though. Jordan is on line 43 for males and line 70 for females.

Does anybody know why this is happening? I feel like I'm really close to getting this done.
Last edited on
Pages: 123