Trying to take 2 vectors from 1 file and write into 2 seperate files

Hey guys. So I was trying to do a programming project where I take the most popular boy and girl baby names and save the data into 2 vectors. I was then supposed to write the two vectors out into 2 seperate file I cannot figure out how to do that. Thanks in advance.

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
64
65
66
67
68
69
70
71
 #include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
int totalNames = 0;

//Create a vector to hold our data.  A vector is similar to an array,
//but it is an object that can change its size to accomodate any amount of data,
//so that you can program without worrying about sizing your array.

vector<string> TotalBabyNames;
vector<string> GirlBabyNames;
vector<string> BoyBabyNames;
string girlname;
string boyname;

TotalBabyNames.reserve(2000);
// create an ifstream object for the file in input mode
ifstream infile("babynames 2014.txt", ios::in);
ofstream girloutfile("girlbabynames 2014.txt");
ofstream boyoutfile("boybabynames 2014.txt");
if (!infile.is_open())
{
return -1;
}

if (!girloutfile.is_open())
{
return -1;
}
if (!boyoutfile.is_open())
{
return -1;
}
//create an iterator to loop through the vector
vector<string>::iterator Git = GirlBabyNames.begin();
while (Git != GirlBabyNames.end())
{
//write the data at the iterator position
girloutfile << *Git << endl;

//advance the iterator
Git++;
}
vector<string>::iterator Bit = BoyBabyNames.begin();
while (Bit != BoyBabyNames.end())
{
//write the data at the iterator position
boyoutfile << *Bit << endl;

//advance the iterator
Bit++;
}
// Loop and read the data and place it into the vector at the end.
while (infile >> girlname)
{
GirlBabyNames.push_back(girlname);
BoyBabyNames.push_back(boyname);
}

//close the file when done with it
infile.close();

cin.get();
return 0;
}
Lines 39-40, 49-57: You're iterating through empty arrays. When you program starts, those vectors are empty.

Line 59-63: You assume all names in the file are girl names.

Line 62: boyname is an empty string.

Can't really provide much more help since you haven't specified the format of the input file.

Edit: By googling "babynames 2014.txt" I found the following problem which is similar to yours.
You have a data file that contains the 1000 most popular baby names of 2014 (babynames 2014.txt). The file has 1000 lines in it, each of which has a rank, the boy name for that rank, and the girl name for that rank: Your goal is to read the data in that file into two separate vectors, one for girls and one for boys, then write the data in each vector to separate files, one for girls and one for boys. You should only store the names in the vectors. You should not store the position as you process. However, your output for each file should also include the correct rank.


Your code was pretty much correct, except it was in the wrong order and you needed to read both names from the input file (see line 34).
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
64
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
	int totalNames = 0;

	//Create a vector to hold our data.  A vector is similar to an array,
	//but it is an object that can change its size to accomodate any amount of data,
	//so that you can program without worrying about sizing your array.

	vector<string> GirlBabyNames;
	vector<string> BoyBabyNames;
	string girlname;
	string boyname;
	
	// create an ifstream object for the file in input mode
	ifstream infile("babynames 2014.txt", ios::in);
	ofstream girloutfile("girlbabynames 2014.txt");
	ofstream boyoutfile("boybabynames 2014.txt");

	//	Verify the files opened corre3ctly
	if (!infile.is_open())
		return -1;
	if (!girloutfile.is_open())
		return -1;	
	if (!boyoutfile.is_open())
		return -1;
	
	// Loop and read the data and place it into the vector at the end.
	while (infile >> boyname >> girlname)
	{
		GirlBabyNames.push_back(girlname);
		BoyBabyNames.push_back(boyname);
	}
	//close the file when done with it
	infile.close();

	//create an iterator to loop through the vector
	vector<string>::iterator Git = GirlBabyNames.begin();
	while (Git != GirlBabyNames.end())
	{
		//write the data at the iterator position
		girloutfile << *Git << endl;

		//advance the iterator
		Git++;
	}
	vector<string>::iterator Bit = BoyBabyNames.begin();
	while (Bit != BoyBabyNames.end())
	{
		//write the data at the iterator position
		boyoutfile << *Bit << endl;

		//advance the iterator
		Bit++;
	}
	
	cin.get();
	return 0;
}
Last edited on
Assuming that the input data file has the format of boyname girlname on each line (with no white-space chars in the names), then possibly:

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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <iterator>

int main() {
	std::vector<std::string> GirlBabyNames;
	std::vector<std::string> BoyBabyNames;
	std::ifstream infile("babynames 2014.txt");
	std::ofstream girloutfile("girlbabynames 2014.txt");
	std::ofstream boyoutfile("boybabynames 2014.txt");

	if (!infile || !girloutfile || !boyoutfile)
		return (std::cout << "Cannot open file(s)\n"), - 1;

	for (std::string boyname, girlname; infile >> boyname >> girlname; ) {
		GirlBabyNames.push_back(std::move(girlname));
		BoyBabyNames.push_back(std::move(boyname));
	}

	for (const auto& g : GirlBabyNames)
		girloutfile << g << '\n';

	for (const auto& b : BoyBabyNames)
		boyoutfile << b << '\n';
}

Last edited on
Thank you guys so much.
I have run into another problem, when I step through my code, my strings take numbers and save that as well as the actual baby names.
I do not know how that is happening, the strings are not supposed to take numbers.
Last edited on
Not sure exactly what you mean, but "1234" is a string. if its in a text file and you read into a string, it gets the 'text version' of the number. That is perfectly OK, if its what you wanted. If its not what you wanted, its a bug, of course. If the file has numbers, you have to read them, even if you throw them away, you have to consume everything in the file sometimes to skip past it. You can read them into a string to discard them; if you need them as a number, go ahead and read them into a numeric data type (this is more expensive, so if discarding, read as string, the process to convert text to number and reverse are sluggish in several places in most compilers).
Last edited on
> my strings take numbers and save that as well as the actual baby names.

What does your input file look like (what does this program print out)?

1
2
3
4
5
6
7
#include <iostream>
#include <fstream>

int main()
{
    std::cout << std::ifstream( "babynames 2014.txt" ).rdbuf() ;
}

ifstream infile("babynames 2014.txt", ios::in);
Last edited on
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

int main()
{
    if( std::ifstream in_file{ "babynames 2014.txt" } ) // if the file was opened for input
    {
        std::vector<std::string> extracted_boy_names ;
        std::vector<std::string> extracted_girl_names ;

        int rank ;
        std::string boy_name ;
        std::string girl_name ;

        while( in_file >> rank >> boy_name >> girl_name ) // for each 3-tuple successfully read from the file
        {
            // ignore the rank (the number), add the names to the appropriate vector
            extracted_boy_names.push_back(boy_name) ;
            extracted_girl_names.push_back(girl_name) ;
        }

        // TO DO: write out contents of the vectors to two files etc.
    }

    else std::cerr << "error: failed to open the input file\n";
}
Topic archived. No new replies allowed.