C++ Create an application that searches a file of male and female first names. A
ID: 3861007 • Letter: C
Question
C++
Create an application that searches a file of male and female first names. A link to the file is provided on the class webpage. "FirstNames2015.txt" is a list of the most popular baby names in the United States and was provided by the Social Security Administration.
Each line in the file contains a boy's name and a girl's name. The file is space-delimited, meaning that the space character is used to separate the boy name from the girl name. The file begins with:
Noah Emma
Liam Olivia
Mason Sophia
Jacob Ava
William Isabella
Ethan Mia
James Abigail
Write a program that asks the user to enter a name. The program then searches the file for the name and displays the position of the name in the file if it matches either a boy name or a girl name. The program also displays a message if the name could not be found. Sample outputs:
Enter a name to search: Daniel
Daniel is ranked 12 for boy names
Daniel was not found for girls
Enter a name to search: Cassandra
Cassandra is ranked 511 for girl names
Cassandra was not found for boys
Enter a name to search: Taylor
Taylor is ranked 76 for girl names
Taylor is ranked 462 for boy names
NOTE: You can use any method you wish to match the name input on the keyboard with the names in the file. You can compare two strings ignoring upper/lower case by using #include<cstring> in C++ or #include<string.h> in C and the following comparison if ( stricmp(string1,string2) == 0)
If your compiler does not have stricmp, you may need to use _stricmp or strcasecmp.
===============================================================================
"FirstNames2015.txt"
Explanation / Answer
Program:
#include <iostream>
#include <string>
#include <fstream> // library that contains file input/output functions
using namespace std;
bool icompare_pred(unsigned char a, unsigned char b)
{
return std::tolower(a) == std::tolower(b);//convert charcters into lower case and compare two charcters return true if equal
}
bool icompare(string const& a, string const& b)
{
if (a.length()==b.length()) {//check length of the two strings are equal
return std::equal(b.begin(), b.end(),
a.begin(), icompare_pred);
}
else {
return false;//if not return false
}
}
int main()
{
int position=0,boypos=0,girlpos=0;
string boy,girl,searchKey;
ifstream fin;
fin.open("myfile.txt"); //opening an input stream for file FirstNames2015.txt
/*checking whether file could be opened or not. If file does not exist or don't have read permissions, file
stream could not be opened.*/
if(fin.is_open())
{
//file opened successfully so we are here
//cout << "File Opened successfully!!!. Reading data from file into array" << endl;
cout<<"enter search name: ";
cin>>searchKey;
//this loop run until end of file (eof) does not occur
while(!fin.eof())
{
fin >> boy; //reading boy name from file to boy
fin >> girl;//reading girl name from file to girl
position++;//increament position
if(icompare(searchKey,boy))
{
boypos=position;//when find searchkey then boypos=position
}
if(icompare(searchKey,girl))
{
girlpos=position;//when find searchkey then girlpos=position
}
}
if(boypos!=0)//check whether search key is find or not in boys names
cout<<searchKey<<" is ranked "<<boypos<<" for boy names"<<endl;
else
cout<<searchKey<<" was not found for boys"<<endl;
if(girlpos!=0)//check whether search key is find or not in girls names
cout<<searchKey<<" is ranked "<<girlpos<<" for girls names"<<endl;
else
cout<<searchKey<<" was not found for girls"<<endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.