/* Program to read in tab-separated values with information about
* congressional members, sort that data, and print out the first
* 10 results.
* Right now it just sorts based on first names and prints out the
* first names.
* Your challenge: Use a struct to store more information about
* each congressperson. Have your program sort them according to age
* and then print out the first and last names of the 10 youngest
* congressional members.
* (This is the sample solution to that challenge.)
*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct person {
string first;
string last;
int age;
};
bool readcong(person& rep, istream& fin);
void sort(person* arr, int n);
bool before(person a, person b);
int main() {
ifstream congfile("legislators-current.tsv");
person* congress = new person[1000];
int n=0;
while (readcong(congress[n], congfile)) {
++n;
}
sort(congress, n);
for (int i=0; i<10; ++i) {
cout << congress[i].first << ' ' << congress[i].last << ' '
<< congress[i].age << endl;
}
delete [] congress;
return 0;
}
bool readcong(person& rep, istream& fin) {
int birthyear, birthmonth, birthday;
string last_name, first_name, gender, type, state, district, party,
url, address, phone, contact_form, rss_url, twitter, facebook,
facebook_id, youtube, youtube_id, bioguide_id, thomas_id,
opensecrets_id, lis_id, cspan_id, govtrack_id, votesmart_id,
ballotpedia_id, washington_post_id, icpsr_id, wikipedia_id;
fin >> last_name >> first_name >> birthyear >> birthmonth >> birthday
>> gender >> type >> state >> district >> party >> url >> address >> phone
>> contact_form >> rss_url >> twitter >> facebook >> facebook_id
>> youtube >> youtube_id >> bioguide_id >> thomas_id >> opensecrets_id
>> lis_id >> cspan_id >> govtrack_id >> votesmart_id >> ballotpedia_id
>> washington_post_id >> icpsr_id >> wikipedia_id;
rep.first = first_name;
rep.last = last_name;
// based off today's date = November 14, 2016
rep.age = 2015 - birthyear;
if (birthmonth < 11 || (birthmonth == 11 && birthday <= 14))
// they had their birthday in 2016
++rep.age;
return bool(fin);
}
bool before(person a, person b) {
return a.age < b.age;
}
void sort(person* arr, int n) {
for (int i=0; i<n-1; ++i) {
int nexti=i;
for (int j=i+1; j<n; ++j) {
if (before(arr[j], arr[nexti]))
nexti=j;
}
person temp = arr[nexti];
arr[nexti] = arr[i];
arr[i] = temp;
}
}