void name_search (string name)
{
int rank;
string male, female;
ifstream infile;
bool male_name_found=false,female_name_found=false;
infile.open ("babynames2004.txt");
if (infile.fail())
{
cout << "The file was corrupt.\n";
}
while (!infile.eof())
{
infile >> rank >> male >> female;
if(name == male && name != female)
{
cout << name << " is ranked " << rank << " in popularity among boys.\n";
male_name_found=true;
}
elseif(name != male && name == female)
{
cout << name << " is ranked " << rank << " in popularity among girls.\n";
female_name_found=true;
}
elseif (name == male && name == female)
{
cout << name << " is ranked " << rank << " in popularity among boys.\n";
cout << name << " is ranked " << rank << " in popularity among girls.\n";
male_name_found=true;
female_name_found=true;
}
}
if(male_name_found==false)
cout<<"Name not listed among male names";
if(female_name_found==false)
cout<<"Name not listed among female names";
infile.close();
}
Why should it? You would be saying "if the name didn't match, say it isn't there even though it might be".
There is no way that you can do the entire thing inside the loop, how can you talk (cout) about the list of 1000 names when you are somewhere in the middle. Just use a bool and int and remember it: