I have an assignment for C++ beginners class. It specifies that my program must read in the names of .txt file. The program is called BabyNames and upon prompting the user to enter a name, it will take in the name, compare it to the .txt file and either find a match among the boy and girl names (or both). If it finds a match it should output the rank of the name. it should also indicate if there is no match. the .txt file looks like this....
1 Jacob Emily
2 Michael Emma
3 Joshua Madison
4 Matthew Olivia
5 Ethan Hannah
and so on and so forth for 1,000 total lines.
Im using visual studio 2010 as my compiler, and here is what I have so far. Need help.
Code:#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
int main ( )
{
using namespace std;
string inputName, boyName, girlName;
ifstream babyFile;
int rank;
char another = 'y';
while (another =='y')
{
babyFile.open("babynames2004.txt");
// verify that it opened
if (babyFile.fail( ))
{
cout << "Can't open babynames2004.txt\n";
exit(1);
}
cout << "Enter the name to search for: ";
cin >> inputName;
while (babyFile >> rank)
{
babyFile >> boyName;
babyFile >> girlName;
if (inputName == boyName)
{
cout << inputName << " is ranked "
<< rank << " in popularity among boys.\n";
}
}
babyFile.close();
babyFile.clear();
cout << "Try another name? ";
cin >> another;
}
return 0;
}