Search for a String and Find the Rank

Pages: 123
close the while loop after the last else if
How do you close a while loop? I can't use a break statement. Do I have to create another boolean variable? Should I change the arguments of the while loop?
I put male_found = true; and female_found = true; inside the last else if statement but the name Jordan is still not showing up for a female name. Any ideas?

put the close brace for while after the last if else statement.
Are you reading female name along with male name?

infile>rank>>male>>female;
when i put the if statement and the 3 else if statements in the while loop Jordan doesn't even come up for the male category now.

Can anybody find why the female name is not being found? I feel like I'm so close. I have so much other work to do.
Last edited on
can u provide your text file.?
Do you want to PM it to you? It is 1000 lines long so pasting it here would seem to be too much.

I guess I can't attach a file in a PM. I'll post the first 100 lines of the text file.
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
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
95
96
97
98
99
100
1 Jacob Emily
2 Michael Emma
3 Joshua Madison
4 Matthew Olivia
5 Ethan Hannah
6 Andrew Abigail
7 Daniel Isabella
8 William Ashley
9 Joseph Samantha
10 Christopher Elizabeth
11 Anthony Alexis
12 Ryan Sarah
13 Nicholas Grace
14 David Alyssa
15 Alexander Sophia
16 Tyler Lauren
17 James Brianna
18 John Kayla
19 Dylan Natalie
20 Nathan Anna
21 Jonathan Jessica
22 Brandon Taylor
23 Samuel Chloe
24 Christian Hailey
25 Benjamin Ava
26 Zachary Jasmine
27 Logan Sydney
28 Jose Victoria
29 Noah Ella
30 Justin Mia
31 Elijah Morgan
32 Gabriel Julia
33 Caleb Kaitlyn
34 Kevin Rachel
35 Austin Katherine
36 Robert Megan
37 Thomas Alexandra
38 Connor Jennifer
39 Evan Destiny
40 Aidan Allison
41 Jack Savannah
42 Luke Haley
43 Jordan Mackenzie
44 Angel Brooke
45 Isaiah Maria
46 Isaac Nicole
47 Jason Makayla
48 Jackson Trinity
49 Hunter Kylie
50 Cameron Kaylee
51 Gavin Paige
52 Mason Lily
53 Aaron Faith
54 Juan Zoe
55 Kyle Stephanie
56 Charles Jenna
57 Luis Andrea
58 Adam Riley
59 Brian Katelyn
60 Aiden Angelina
61 Eric Kimberly
62 Jayden Madeline
63 Alex Mary
64 Bryan Leah
65 Sean Lillian
66 Owen Michelle
67 Lucas Amanda
68 Nathaniel Sara
69 Ian Sofia
70 Jesus Jordan
71 Carlos Alexa
72 Adrian Rebecca
73 Diego Gabrielle
74 Julian Caroline
75 Cole Vanessa
76 Ashton Gabriella
77 Steven Avery
78 Jeremiah Marissa
79 Timothy Ariana
80 Chase Audrey
81 Devin Jada
82 Seth Autumn
83 Jaden Evelyn
84 Colin Jocelyn
85 Cody Maya
86 Landon Arianna
87 Carter Isabel
88 Hayden Amber
89 Xavier Melanie
90 Wyatt Diana
91 Dominic Danielle
92 Richard Sierra
93 Antonio Leslie
94 Jesse Aaliyah
95 Blake Erin
96 Sebastian Amelia
97 Miguel Molly
98 Jake Claire
99 Alejandro Bailey
100 Patrick Melissa
Ah no need :)
Actually IM trying with my own input

56 Bob Jeanine
100 Dan_Smith Clay
200 Mike_Tay jenny


It worked for me and this is the 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
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
#include <iostream>
#include <string> 
#include <fstream>
using namespace std;

void 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;
}


void name_search (string name)
{
	int rank;
	string male, female; 
	ifstream infile;
	bool male_found = false, female_found = false;


	infile.open ("male.txt");

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


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

		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";
	}
}
tyr this code (just change the name of the text file) and tell me whether you get what you expected
I think that is the exact code that I have.

The problem seems to be occurring when the name is both in the top 1000 for males and females. The name Jordan occurs on line 43 for males and line 70 for females. But the output just says the rank of the male one. The female one is apparently not found and the program says the name is not in the top 1000 names for a girl. It is on line 70 though.

The only difference is I put infile.close() after the last else if statement.
Last edited on
Thats because when name is found it exits the while loop.
Jordan name found first in line 43 then the program is exited immediately. It dont know even check for the next line
Yea that is what I thought, but do you know how to get around that issue while still using these limited techniques?

By the way, I have it so the if statement and three else if statements are not even in the while loop. It seems like not having them in the while loop has gotten me the closest to a solution.
Last edited on
is it compulsory to have those three if else statements?
You can do your program with the following code also which is simpler and easier.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void name_search (string name)
{
	int rank;
	string male, female; 
	ifstream infile;
	bool male_found = false, female_found = false;


	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<<" "<<rank<<" in male names"<<endl;
		else if(name ==female)
			cout<<name<<" "<<rank<<" in female names"<<endl;
	}
}
But what if the file has this:
1 Alex Alex

Removing the boolean checking from the while condition should fix it.

Also, if/while statements don't need the equals with bool:
1
2
3
4
5
bool found = false;
while (!found)
{
  found = findit();
}


You do not want the else if(name ==female)
change that to just if(name ==female)

pieced this together best I could from what you had posted.

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
#include <iostream>
#include <string> 
#include <fstream>
using namespace std;
string name_search;	//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;
		int rank;
    int boyrank=0;
    int girlrank=0;

		string male, female; 
		ifstream infile;
		string line;
		bool male_found = false, female_found = false;

	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;

		infile.open ("names.txt");
        if(infile.is_open())
        {
        while(!infile.eof())
		{
            getline(infile >> rank >> male >> female,line);
            cout << line << endl;

			if (name == male)
				{male_found=true;
                boyrank=rank;}
			if (name == female)
				{female_found=true;
                girlrank=rank;}
		}

		cout << " End of file " << endl;

		if(male_found == true && female_found != true)
		{
			cout << name << " is ranked " << boyrank << " 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 " << girlrank << " in popularity among girls.\n";
		}
		else if (male_found == true && female_found == true)
		{
			cout << name << " is ranked " << boyrank << " in popularity among boys.\n";
			cout << name << " is ranked " << girlrank << " 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";
		}
		}		
		else
		{
			cout << "The file was corrupt.\n";
        }
	return 0;
}
vichu8888, the code you posted allows name that occurs for both genders, such as Jordan in this case, to be output correctly, but it only tells you if the name comes up for one gender. it does not say that the name did not rank in the top 1000 for the other gender.

LowestOne, thanks for the tip on the argument of an if or while loop.

SamuelAdams, that code doesn't really work. It compiles, but when I enter the name Jacob it says the file is corrupt.
vichu888, I just tweaked the code a little bit to get the program to out what I wanted. The code is 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
42
void name_search (string name)
	{
		int rank;
		string male, female, male_name, female_name; 
		ifstream infile;
		
		infile.open ("babynames2004.txt");
	
		if (infile.fail())
		{
			cout << "The file was corrupt.\n";
		}
	
		while (!infile.eof())
		{
			infile >> rank >> male >> female;
			male_name = male;
			female_name = female;

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


Here is the output. So a name that is just occurs for males and a name that is just occurs for females works. I still have the problem of when the name occurs for both a male and a female, such as for the name Jordan.

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.
Emily is not ranked among the top 1000 boys names.
Emily 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: Jordan
Jordan is ranked 43 in popularity among boys.
Jordan is not ranked among the top 1000 girl names.
Jordan is not ranked among the top 1000 boys names.
Jordan is ranked 70 in popularity among girls.
Jordan is not ranked among the top 1000 boys names.
Jordan is not ranked among the top 1000 girl names.

Would you like to run this program again? (y or n)
Last edited on
This piece of code should actually say name != female, but when I do that the program gets messed up. If I entered the name Jacob now the program says the name does not occur for either gender 1000 times.

1
2
3
4
5
if (name != male && name == female)
			{
				cout << name << " is not ranked among the top 1000 boys names.\n";
				cout << name << " is not ranked among the top 1000 girl names.\n";
			}
Last edited on
Pages: 123