1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cctype>
using namespace std;
void prompt(string& name);
void nameTester(ifstream& inStream, string name, string male, string female, int placement, int& malePlacement, int& femalePlacement, bool& isMale, bool& isFemale);
void output(string name, int malePlacement, int femalePlacement, bool isMale, bool isFemale);
int main()
{
ifstream inStream;
bool isMale = false, isFemale = false;
string name, female, male;
int placement, malePlacement, femalePlacement;
prompt(name);
nameTester(inStream, name, male, female, placement, malePlacement, femalePlacement, isMale, isFemale);
output(name, malePlacement, femalePlacement, isMale, isFemale);
}
void prompt(string& name)
{
cout << "Enter a name and this program will check if the name is ranked in the top 1,000 most popular names: ";
cin >> name;
}
void nameTester(ifstream& inStream, string name, string male, string female, int placement, int& malePlacement, int& femalePlacement, bool& isMale, bool& isFemale)
{
inStream.open("babynames2004.txt");
if (inStream.fail())
{
cout << "There was an error opening the file. The program will now close.\n";
exit(1);
}
while (! inStream.eof())
{
inStream >> placement >> male >> female;
if (name == male)
{
isMale = true;
malePlacement = placement;
}
if (name == female)
{
isFemale = true;
femalePlacement = placement;
}
}
inStream.close();
}
void output(string name, int malePlacement, int femalePlacement, bool isMale, bool isFemale)
{
if (isMale == true)
cout << name << " is ranked " << malePlacement << " in popularity among boys\n";
else
cout << name << " is not ranked among the top 1000 boy names\n";
if (isFemale == true)
cout << name << " is ranked " << femalePlacement << " in popularity among girls\n";
else
cout << name << " is not ranked among the top 1000 girl names\n";
}
|