0

I am trying to take a file that has a lot of baby names for both boys and girls and numbering them based on popularity (these names are in 3 columns, column 1 = number, column 2 = boy name, column 3 = girl name), eg. 1 Noah Emma, what I need to do is take this file and make 2 vectors for boys and girls names and then use those vectors to write 2 separate files that each number the boys names and the other to number the girls names correctly(eg. File 1 = 1 Noah, File 2 = 1 Emma). This is all I have at the moment. Some help would be much appreciated. Right now the biggest part I'm trying to figure out is how to seperate the two names into the vectors I created called boy_names and girl_names.

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
    //vector<string> boy_names;
    //vector<string> girl_names;
    string boy_names;
    string girl_names;

    int line_number;

    ifstream file_reader("babynames 2014.txt", ios::in);

    ofstream b_out("boys 2014", ios::out);
    ofstream g_out("girls 2014", ios::out);

    if (!file_reader)
    {
        cout << "Unable to read babyname 2014.txt. File may not be present in the current directory.";
        return -1;
    }

    while (file_reader >> line_number >> boy_names >> girl_names)
    {
        b_out << line_number++ << "  " << boy_names << endl;

        g_out << line_number++ - 1 << "  " << girl_names << endl;
    }

        cin.ignore();
        cin.get();

        return 0;
}
3
  • 3
    I don't see an attempt of populating just one of the vectors, let alone two of them. You should start out small -- populate just one vector with names. Once you get comfortable with doing that, then expand the code to populate two vectors. Jun 23, 2020 at 1:14
  • 1) Please update where you're at with this question. I went out on a limb to try to help, and you haven't responded at all. 2) You need parentheses after if (!file_reader). 3) You don't seem to be reading anything and you don't seem to be using the vectors you created. 4) Your question is confusing - it's not at all clear what you ARE trying to do :(
    – paulsm4
    Jun 23, 2020 at 3:31
  • The part that I'm struggling with is that I have these two vectors at the top but I'm not sure how to proplerly populate them. With the code I currently have the two strings boy_names and girl_names are able to do what I want the vectors to do but I'm just not sure how to take the data and put them into the vectors and then how to actually use the vectors. Vectors are a little tricky and hard for me to understand right now.
    – Aces Radix
    Jun 23, 2020 at 5:03

1 Answer 1

0
#include<bits/stdc++.h>
using namespace std;
int main()
{
    ifstream myfile("files.txt");
    string fullname;
    vector<string> boy_name;
    vector<string> girl_name;
    while(getline(myfile,fullname))
    {
        string b_name,g_name;
        size_t pos=fullname.find(' ');
        size_t pos1=fullname.find(' ',pos+1);
        b_name=fullname.substr(pos+1,pos1-1);//getting the boy name
        boy_name.push_back(b_name);
        size_t pos2=fullname.find(' ',pos1+1);
        g_name=fullname.substr(pos1+1);//getting the girl name
        girl_name.push_back(g_name);
    }
//printing the two vectors
    for(int i=0;i<boy_name.size();i++) cout<<boy_name[i]<<"\n";
    cout<<endl;
    for(int i=0;i<boy_name.size();i++) cout<<girl_name[i]<<"\n";

}

As you said the main problem was getting the strings into the appropriate boy and girl vectors...I have resolved it by simply reading the file line by line and extracting the appropriate substring and pushing those into the corresponding vectors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.