The text file babynamestxt contains a list of the 1000 most
The text file babynames.txt contains a list of the 1000 most popular boy and girl names in the United States for the year 2012 as compiled by the Social Security Administration.
This is the space-delimited file of 1000 entries in which the rank is listed first, followed by the corresponding boy name and girl name. The most popular names are listed first and the least popular names are listed last. (As an extra, also include the 2013, 2014, and 2015 from the Social Security Administration website, allowing the user to select which year they\'d like to search). For example, the file begins with:
This indicates that Jacob is the most popular boys name and Sophia is the most popular girl name. Mason is the second most popular name and Emma is the second most popular girl name.
Write a program using functions and arrays that allows the user to input a name. The program should then read from the file and search for a matching name among the girls and boys. If a match is found, it should output the rank of the name. The program should also indicate if there is no match.
For example, if the user enters the name \'Justice\' then the program should output:
If the user enters the name \'Walter\' then the program should output:
**The eof function shouldn\'t be used, but the extraction operator >> instead. Thank you!
Solution
Please follow the code and comments for description :
CODE :
#include <iostream> // required header files
#include <fstream>
#include <iomanip>
#include <string>
#include <cstddef>
using namespace std;
int main() // driver method
{
string line, userName; // required initialisations
int count = 0, foundBoyIndex = 0, foundGirlIndex = 0;
bool foundBoy = false, foundGirl = false, notFoundBoy = false, notFoundGirl = false;
ifstream inputFile (\"babynames.txt\"); // stream of data for the inout and the output files
cout << \"Please Enter a Name : \"; // prompt to enter the data from the user
cin >> userName;
if (inputFile.is_open()) // checking for the inputFile is open or not
{
while ( getline (inputFile,line) ) // iterating over the loop of the data to get the lines
{
count++;
size_t units = line.find(\" \"); // getting the unit used from the line read
string boysName = line.substr(0, units); // saving the data
string girlsName = line.substr(units + 1); // saving the data
if(userName == boysName) { // checking if the data is matched or not
foundBoy = true; // flag value true
foundBoyIndex = count; // saving the count
} if(userName == girlsName){
foundGirl = true;
foundGirlIndex = count;
} if(userName != boysName){
notFoundBoy = true;
} if(userName != girlsName){
notFoundGirl = true;
}
}
if(foundBoy == true) { // checking for the flag values
cout << userName << \" is ranked \" << foundBoyIndex << \" in popularity among boys.\" << endl; // if found returning the data to user
} else if(notFoundBoy == true){
cout << userName << \" is not ranked among the top 1000 boys names.\" << endl;
}
if(foundGirl == true) { // checking for the flag values
cout << userName << \" is ranked \" << foundGirlIndex << \" in popularity among girls.\" << endl; // if found return the data to user
} else if(notFoundGirl = true){
cout << userName << \" is not ranked among the top 1000 girls names.\" << endl;
}
inputFile.close(); // closing the file
}else {
cout << \"Unable to open file\"; // if unopened prompt the user
}
return 0;
}
OUTPUT :
Please Enter a Name : Jacob
Jacob is ranked 1 in popularity among boys.
Jacob is ranked 4 in popularity among girls.
Please Enter a Name : Adams
Adams is not ranked among the top 1000 boys names.
Adams is not ranked among the top 1000 girls names.
Please Enter a Name : Emma
Emma is ranked 4 in popularity among boys.
Emma is ranked 2 in popularity among girls.
babynames.txt :
Jacob Sophia
Mason Emma
Ethan Isabella
Emma Jacob
Hope this is helpful.

