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.
#include <iostream>
#include <fstream>
#include <vector>
usingnamespace 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).
#include <iostream>
#include <fstream>
#include <vector>
usingnamespace 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;
}
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.
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).
#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";
}