Skip to content

Instantly share code, notes, and snippets.

@Battleroid
Created March 18, 2013 12:38
Show Gist options
  • Save Battleroid/5186890 to your computer and use it in GitHub Desktop.
Save Battleroid/5186890 to your computer and use it in GitHub Desktop.
Cumulative baby ranking. Done really badly, not interested in making it work.
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
#include <vector>
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 displayRanks () {
// records
int maleAmount[1000];
int femaleAmount[1000];
string male[1000];
string female[1000];
// file setup
ifstream file("Babynamesranking2001.txt");
string feed;
int count = 0;
// 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
if (count != 1000) {
male[count] = items[1];
female[count] = items[3];
maleAmount[count] = stoi(items[2]);
femaleAmount[count] = stoi(items[4]);
}
count++;
}
}
file.close();
// add values from second file
// file setup
ifstream file2("Babynamesranking2001.txt");
string feed2;
int count2 = 0;
// store items
if (file2.is_open()) {
while (file2.good() && !file2.eof()) {
// split input line by line
getline(file2, feed2);
istringstream ss(feed2);
istream_iterator<string> begin(ss), end;
vector<string> items(begin, end);
// insert items from line
if (count2 != 1000) {
// find and add
for (int i = 0; i < 1000; i++) { // male
if (items[1] == male[i]) {
maleAmount[i] += stoi(items[2]);
}
}
for (int i = 0; i < 1000; i++) { // female
if (items[3] == female[i]) {
femaleAmount[i] += stoi(items[4]);
}
}
}
count2++;
}
}
file2.close();
fstream output("Babycumulative.txt", ios::out);
for (int i = 0; i < 1000; i++) {
output << "#" << (i + 1) << '\t' << male[i] << '\t' << maleAmount[i] << '\t' << female[i] << '\t' << femaleAmount[i] << endl;
}
output.close();
}
int main () {
displayRanks();
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment