First, some general comments about the approach before moving on to the actual code:
1. popular name rankings is taken from
https://www.ssa.gov/oact/babynames/, saved in a text file and then read into the program using getline()
2. the string argument of getline, representing each line of the above file, is used to instantiate a stringstream object that reads the rank, male name, female name into a vector<tuple<int,string,string>> (if unfamiliar, read up how each of these work and then come back here if you have any further queries)
3. ourclass.txt (a sample file is shown further below) is read into a vector<string>
4. both .txt files are closed since they are no longer required
5. nested range loops over the ourclass (outer-loop) and popularnamerank(inner-loop) vectors are run to check matches and the results are used to fill a third vector that lists the names from the class with their popularity ranks (class names that don't feature on the popular names list is ranked 0; range loops is a C++11 feature that your compiler should support, else you can use iterators)
6. the program has no code in case the files don't open, you must put this in for robustness
7. finally, the program prints the names of the members of the class with their popularity rankings
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
|
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
#include<tuple>
#include <algorithm>
using namespace std;
int main(){
ifstream File_pnr;//pnr == popular name ranking;
vector<tuple<int, string, string>> v_pnr;
int rank;
string name_male, name_female;
File_pnr.open("F:\\popularnamerankings.txt");
if(File_pnr.is_open()){
string line;
while(getline(File_pnr, line)){
stringstream stream(line);
while(stream>>rank>>name_male>>name_female){
v_pnr.emplace_back(rank, name_male, name_female);
}
}
}
File_pnr.close();// done with this file, can close it now;
ifstream File_oc; //oc == our class;
vector<string> v_oc;
File_oc.open("F:\\ourclass.txt");
if(File_oc.is_open()){
string line;//can use same variable name as above due to local scope;
while(getline(File_oc, line)){
v_oc.push_back(line);//since line is type string no need for stringstream;
}
}
File_oc.close();// done with this file, can close it now;
vector<tuple<string,int>>v_ocr;//ocr == our class ranked; // the container could even be a pair instead of tuple;
for(auto& itr_oc : v_oc){
int match = 0;
for(auto& itr_pnr : v_pnr){
if(itr_oc == get<1>(itr_pnr) || itr_oc == get<2> (itr_pnr)){
match ++;
v_ocr.emplace_back(itr_oc,get<0>(itr_pnr));
}
}
if(match == 0){
v_ocr.emplace_back(itr_oc,0);
}
}
cout<<"Name: \tRank: \n";
for(auto& itr_ocr : v_ocr){
cout<<get<0>(itr_ocr)<<"\t"<<get<1>(itr_ocr)<<"\n";
}
}
|
ourclass.txt
Noah
Mason
William
James
Michael
Olivia
Ava
Mia
Abigail
Emily
Harper
Jack
John
Sam
Emily
Laura
Teddy
Erica
Fiona
Vladimir
Alexander
Program Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
Name: Rank:
Noah 1
Mason 3
William 5
James 7
Michael 9
Olivia 2
Ava 4
Mia 6
Abigail 7
Emily 8
Harper 10
Jack 0
John 0
Sam 0
Emily 8
Laura 0
Teddy 0
Erica 0
Fiona 0
Vladimir 0
Alexander 8
|