Last active
December 15, 2015 02:19
-
-
Save Battleroid/5186885 to your computer and use it in GitHub Desktop.
Baby name ranking, works. Finds the ranking of the name with given information.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fstream> | |
#include <iostream> | |
#include <string> | |
#include <sstream> | |
#include <iterator> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
template <typename T> | |
T stoi (const string &input) { | |
stringstream ss(input); | |
T result; | |
return ss >> result ? result : 0; | |
} | |
string itos (int input) { | |
stringstream ss; | |
ss << input; | |
return ss.str(); | |
} | |
void findRank (int year, char sex, string name) { | |
// records | |
vector<int> rank; | |
vector<int> maleAmount; | |
vector<int> femaleAmount; | |
vector<string> male; | |
vector<string> female; | |
// year setup for proper filename | |
string filename = "Babynamesranking" + itos(year) + ".txt"; | |
// file setup | |
ifstream file(filename); | |
string feed; | |
// store items | |
if (file.is_open()) { | |
while (file.good() && !file.eof()) { | |
// split input line by line | |
getline(file, feed); | |
istringstream ss(feed); | |
istream_iterator<string> begin(ss), end; | |
vector<string> items(begin, end); | |
// insert items from line | |
rank.push_back(stoi(items[0])); | |
male.push_back(items[1]); | |
maleAmount.push_back(stoi(items[2])); | |
female.push_back(items[3]); | |
femaleAmount.push_back(stoi(items[4])); | |
} | |
} else if (file.fail()) { | |
cout << "File does not exist."; | |
} else { | |
cout << "File is currently open."; | |
} | |
// find | |
if (tolower(sex) == 'm') { | |
vector<string>::iterator it; | |
it = find(male.begin(), male.end(), name); | |
if (it != male.end()) { | |
cout << name << " is ranked #" << ((it - male.begin()) + 1) << " in the year " << year << "." << endl; | |
} else { | |
cout << name << " is not ranked in the year " << year << "."; | |
} | |
} else { | |
vector<string>::iterator it; | |
it = find(female.begin(), female.end(), name); | |
if (it != female.end()) { | |
cout << name << " is ranked #" << ((it - female.begin()) + 1) << " in the year " << year << "." << endl; | |
} else { | |
cout << name << " is not ranked in the year " << year << "."; | |
} | |
} | |
} | |
int main () { | |
int year; | |
char sex; | |
string name; | |
cout << "Enter year: "; | |
cin >> year; | |
cout << "Enter gender: "; | |
cin >> sex; | |
cout << "Enter name: "; | |
cin >> name; | |
findRank(year, sex, name); | |
system("pause"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment