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 61 62 63 64 65 66 67 68 69 70 71
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void readin (ifstream& names, string& mArray, string& fArray);
string searchArray (string mArray[], string fArray[], string target);
int main () {
ifstream names;
int year;
cout<<"Enter 2012 2013 2014 or 2015: ";
cin>>year;
if (year == 2012)
names.open("babynames2012.txt");
else if (year == 2013)
names.open("babynames2013.txt");
else if (year == 2014)
names.open("babynames2014.txt");
else if (year == 2015)
names.open("babynames2015.txt");
else terminate();
string target;
string mArray[1000]; //array for male name
string fArray[1000]; //array for female name
readin (names, mArray[1000], fArray[1000]);
cout<<"Enter a name (capitalize the first letter): ";
cin>>target;
target = searchArray (mArray[1000], fArray[1000], target);
}
void readin (ifstream& names, string& mArray, string& fArray) {
int i=0, order;
while (names >> order){
names >> mArray[i] >> fArray[i];
i++;
}
}
string searchArray (string mArray[], string fArray[], string target) {
int i;
bool mname, fname;
for (i=0; i<1000; i++) {
if (target == mArray[i])
cout<<target<<" is ranked"<<i<<" in popularity among boy names.";
mname = true;
}
if (target == fArray[i]) {
cout<<target<<" is ranked"<<i<<" in popularity among girl names.";
fname = true;
}
if (mname != true)
cout<<target<<" is not ranked among the top 1000 boy names.";
if (fname != true)
cout<<target<<" is not ranked among the top 1000 girl names.";
return target;
}
|