#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;
}
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;
}
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.