comparing files and strings

I have to open a file that lists all of the names in our class and compare it to a file of names ranked by popularity and then output the class names with their ranking to a new file. I've been working on this program for days and I'm so lost.
here's what I have so far:
Any help would be greatly appreciated!
*************************************************************************

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

void getclassnames(inputclass);
void getnameranking(outputrank, classname);

int main()
{
getclassnames(inputclass);




}

void getclassnames(inputclass)
{
int num;
string boyname;
string girlname;

ifstream classinput("ourclass.txt");
ofstream namesout("classrankings.txt");

namesout.open();
classinput.open();

while(classinput>>boyname>>girlname)
{
namesout<<boyname<<" "<<girlname;
}


}

void getnameranking(outputrank, classname)
{

int rank;
string boyname;
string girlname;
string rankedgirlname;
string rankedboyname;
ofstream outrank("babynames.txt");
ifstream namein("ourclass.txt");
ifstream readranks("babynames.txt");

namein.open();
readranks.open();

namein>>boyname>>girlname

readranks>>rank>>rankedboyname>>rankedgirlname;

while(readranks>>rank>>rankedboyname>>rankedgirlname)
{
if(boyname==rankedboyname)
{
outrank<<rank<<" "<<boyname<<endl;
}

***************************************************************************







}






how about samples of your input files?
the "ourclass.txt" is just a list of names, one name per line. The "popularnamerankings.txt" file is a list with a ranking number, male name and female name in that order per line.
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

thank you so much for your help! However, we haven't started/are allowed to use vectors yet. I'm getting so frustrated with this program. I know it shouldn't be that hard but for some reason I just cannot figure it out. I have rewrote it multiple times and still can't get it to work.
I can't get it to match up the names with their rank, it will only output one or the other. This is what I've come up with now.
-------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

string getclassname();
void getranking();

int main()
{
getclassname();
getranking();


return 0;
}

string getclassname()
{
string name;
ifstream inputclass;
ofstream output;

inputclass.open("ourclass.txt");
output.open("outputfile.txt");


inputclass>>name;
output<<name<<endl;



return name;
}

void getranking()
{
int rank;
string boyname, girlname, name;
ofstream output;
ifstream ranked;
output.open("outputfile.txt");
ranked.open("babynames.txt");

while(ranked>>rank>>boyname>>girlname)
{
getclassname();

if(name==boyname)
{
output<<rank<<boyname<<endl;
}
else(name==girlname);
{
output<<rank<<girlname<<endl;
}
if(name!=boyname)
{
ranked.close();
}
else(name!=girlname);
{
ranked.close();
}
}

}

----------------------------------------------------------------------------------------------------------------
At this point I don't even care if I get it done in time to turn in, I just need to get it to work out of pure frustration.
Yes, I can understand your frustration.

One thing you could do is to have three files instead of two:

1 ourclass.txt (names of all the students in your class)
2. popularboyname.txt (just the top 10 boys' names listed according to popularity rank)
3. populargirlname.txt (just the top 10 girls' names listed according to popularity rank)

Now you can read off the names into three separate array of strings, for example in the case of popular boys' name:
1
2
3
4
5
6
7
8
9
10
string b_name[10];
ifstream File_b; 
File_b.open("F:\\popularboyname.txt");
if(File_b.is_open()){
int i = 0; 
while(File_b>>b_name[i]){
		i++;
		}
	}
File_b.close();

Then you run nested loops to compare each name in oc_name[ ] vs names in b_name[10] and g_name[10] and, since b_name[ ] and g_name[ ] entries are ranked, the index of the array that matches any name from oc_name [ ] can also be used to spell out the rank of the name in the popularity charts; additionally you can have some code for the case a name doesn't exist in the list of popular names
Last edited on
Topic archived. No new replies allowed.