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?
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
#include <iostream>
#include <string>
#include <fstream>
usingnamespace 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;
elseif (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";
}
elseif (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";
}
elseif (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";
}
elseif (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";
}
}
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.
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.
#include <iostream>
#include <string>
#include <fstream>
usingnamespace 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";
}
elseif (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";
}
elseif (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";
}
elseif (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.
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)
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";
}