So what I'm supposed to do is take a text file and read it into a set of string vectors. The file has three types of data in it, in this order: title, artist, genre. I need to read it into the vectors so that each data type is only read into its specific vector (vector<string> title, vector<string> artist, vector<string> genre).

I'm new at C++, so I guess I'm looking for a little guidance. I know I need to use some type of loop. I was thinking that I would keep track of how many loops occur, and divide by three to tell the program how to read in genre. But the problem is, obviously, if i do divide by two and one that, that will also read in the wrong data types to the wrong vector.

here is my code so far:

#include <iostream> // For input and output to the monitor
#include <fstream> // For file input and output (given)
#include <string> // For text data
#include <vector> // library for using vectors


using namespace std; // To make is easier put this in global namespace

void addsong(vector<string>, vector<string>, vector<string>);
// function: this adds songs to the library
// parameters: string title, string artist, string genre
// functionality: prompts the user for the parameters and checks to make sure
// the song isn't already in the library

int main()
{
	vector<string> title;
	vector<string> artist;
	vector<string> genre;
	ifstream in_stream;
	ofstream out_stream;
	int i = 1;
	int j = 0;
	
	in_stream.open("music_library.txt");
	out_stream.open("outmusic_library.txt");
	while(!in_stream.eof())
	{
		i++;
		if(i%3==0);//will read every third line into genre vector?
		{
			in_stream >> genre[j];
			j++;
		}
		
	}
	
	int choice;
	do
	{
		cout << "1. Add a new song\n";
		cout << "2. Remove an existing song\n";
		cout << "3. Search for a song\n";
		cout << "4. Quit program\n";
		cin >> choice;
		
		switch(choice)
		{
			case 1: //user wants to add a song			
			addsong(title, artist, genre);
				break;
			
			case 2: //user wants to remove an existing song
			
				break;
			
			case 3: //user wants to search for a song
			
				break;
			
			case 4: //user wants to quit program
				cout << "GOODBYE!\n";
				
				break;
			
			
		}	
		
	}while(choice !=4);
	

	in_stream.close( );
	out_stream.close( );
	return 0;
}


void addsong(vector<string> title, vector<string> artist, vector<string> genre) // unfinished
{
	string t_title, a_artist, g_genre;
	
	cout << "Enter the title name: " << endl;
	cin >> t_title; 
	cout << "Enter the artist name: " << endl;
	cin >> a_artist;
	cout << "Enter the genre: " << endl;
	cin >> g_genre;
	
	
	
}

The most important thing about using files is you need to know exactly how they are set up. In this case, presumably the title, artist and song are delimited by something other than space so spaces can appear in the name of each field. Maybe the delimiter is a ~ so it might look like this:

Rock of Ages~anonymous~Mormon Tabernacle Choir

or maybe it's some other delimiter like a comma or a slash or a pound sign or whatever, unless the fields are all restricted to single words in which case spaces could be used as a delimiter. Assuming, there is a delimiter other than space char between the fields, then using one or the other versions of getline() at some point rather than >> seems like a good bet.

this is the loop that i thought up. when i try to run my program, there is no output on the screen. not even the menu shows up. am i not putting the loop in the correct place? also do it look like i'm heading in the right direction?

#include <iostream> // For input and output to the monitor
#include <fstream> // For file input and output (given)
#include <string> // For text data
#include <vector> // library for using vectors


using namespace std; // To make is easier put this in global namespace

void addsong(vector<string>, vector<string>, vector<string>);
// function: this adds songs to the library
// parameters: string title, string artist, string genre
// functionality: prompts the user for the parameters and checks to make sure
// the song isn't already in the library

int main()
{
	vector<string> title;
	vector<string> artist;
	vector<string> genre;
	ifstream in_stream;
	ofstream out_stream;//writes to file

	in_stream.open("music_library.txt");
	out_stream.open("outmusic_library.txt");
	int i = 1;
	
	
	[B]while(! in_stream.eof( ))//reads the file into vectors
	{
		char symbol;
		do
		{
			if(i==1||(i%3==1))
			{
				int j = 0;
				in_stream >> title[j];
				j++;//j keeps track of the index value
				
			}
			if(i==2||(i%3==2))
			{
				int j = 0;
				in_stream >> artist[j];
				j++;
			}
			if(i==3||(i%3==0))
			{
				int j = 0;
				in_stream >> genre[j];
				j++;
			}
			cin.get(symbol);
		}while(symbol !='\n');
		
		i++;//i keeps track of the line number
		
	}//errors	
	[/B]

	int choice;
	do
	{
		
		in_stream.open("music_library.txt");
		out_stream.open("outmusic_library.txt");
	
		while(!in_stream.eof())
		{
			char symbol;
			do
			{
			
				cin.get(symbol);
			
			}while(symbol !='\n');
		
		
	}//errors
				
		cout << "1. Add a new song\n";
		cout << "2. Remove an existing song\n";
		cout << "3. Search for a song\n";
		cout << "4. Quit program\n";
		cin >> choice;
		
		
		
		switch(choice)
		{
			case 1: //user wants to add a song			
			addsong(title, artist, genre);
				break;
			
			case 2: //user wants to remove an existing song
			
				break;
			
			case 3: //user wants to search for a song
			
				break;
			
			case 4: //user wants to quit program
				cout << "GOODBYE!\n";
				
				break;
			
			
		}	
		
	}while(choice !=4);
	

	in_stream.close( );
	out_stream.close( );
	return 0;
}


void addsong(vector<string> title, vector<string> artist, vector<string> genre)
{
	string t_title, a_artist, g_genre;
	
	cout << "Enter the title name: " << endl;
	cin >> t_title; 
	cout << "Enter the artist name: " << endl;
	cin >> a_artist;
	cout << "Enter the genre: " << endl;
	cin >> g_genre;
	
	
	
}
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

int main(int argc, char **argv) {
	vector<string> titles;
	vector<string> artists;
	vector<string> genres;
	ifstream in_stream;
	ofstream out_stream;
	in_stream.open("music_library.txt");
	if (in_stream.is_open()) {
		string title, artist, genre;
		while(in_stream >> title >> artist >> genre){
			titles.push_back(title);
			artists.push_back(artist);
			genres.push_back(genre);
		}
	} else
		cout << "Unable to open file music_library.txt";
	cout << "Titles size: " << titles.size() << endl;
	cout << "Artist size: " << artists.size() << endl;
	cout << "Geners size: " << genres.size() << endl;
	return 0;
}

I understand everything that you did in the code other than:

int main(int argc, char **argv)

what exactly does this do? my class hasnt gotten to that point yet.

1)
int main()

will work fine unless you are running the program from the command line.

2) Don't use the return value of eof() to control a loop like this:

while(! in_stream.eof( ))

You're asking for trouble that won't happen in a readily predicatble manner if you do.

3) Please indicate file structure by posting one or two lines of the file.

so what i need to do is take a given file with 3 data type already present. the file is organized as followed:

song title1
artist1
genre1
song title2
artist2
genre2
etc.

"music_library.txt"

Here it Goes Again
OK Go
Rock
Fade to Black
Metallica
Metal
Enter Sandman
Metallica
Metal
Walk
Pantera
Metal
Learn to Fly
Foo Fighters
Rock
I Wanna Be Sedated
The Ramones
Punk
Stairway to Heaven
Led Zeppelin
Rock
Words
Paul Van Dyk
Electronic
Punk
Ferry Corsten
Electronic
Let Go
Paul Van Dyk
Electronic

when the program starts up, the file should be read into a set of vectors (vector<string>titles, vector<string>artists, vector<string>genre). when the user exits the program the information in the vectors should be written back to the file (it should overwrite any information already present). Also if an empty string is stored in the vector, it should be skipped and not sent to the file.

#include <iostream> // For input and output to the monitor
#include <fstream> // For file input and output (given)
#include <string> // For text data
#include <vector> // library for using vectors


using namespace std; // To make is easier put this in global namespace

void addsong(vector<string>, vector<string>, vector<string>);
// function: this adds songs to the library
// parameters: string title, string artist, string genre
// functionality: prompts the user for the parameters and checks to make sure
// the song isn't already in the library


int main() 
{
	vector<string> titles;
	vector<string> artists;
	vector<string> genres;
	ifstream in_stream;
	ofstream out_stream;
	in_stream.open("music_library.txt");
	int i=1;
	if (in_stream.is_open()) 
	{
		string title, artist, genre;
		while(in_stream >> title >> artist >> genre)
		{
			if((i%3==1)||i==1)
			{
				titles.push_back(title);
			}
			if((i%3==2)||i==2)
			{
				artists.push_back(artist);
			}
			if((i%3==0)||i==3)
			{
				genres.push_back(genre);
			}
			i++;
		}	
	} 
	
	else
		cout << "Unable to open file music_library.txt";
				
	cout << "Titles size: " << titles.size() << endl;
	cout << "Artist size: " << artists.size() << endl;
	cout << "Genre size: " << genres.size() << endl;
	
	


	
	int choice;
	do
	{
		cout << "\n\n1. Add a new song\n";
		cout << "2. Remove an existing song\n";
		cout << "3. Search for a song\n";
		cout << "4. Quit program\n";
		cin >> choice;
		
		switch(choice)
		{
			case 1: //user wants to add a song			
			addsong(titles, artists, genres);
				break;
			
			case 2: //user wants to remove an existing song
			
				break;
			
			case 3: //user wants to search for a song
			
				break;
			
			case 4: //user wants to quit program
				cout << "GOODBYE!\n";
				
				break;
			
			
		}	
		
	}while(choice !=4);
	

	in_stream.close( );
	out_stream.close( );
	return 0;
}


void addsong(vector<string> titles, vector<string> artists, vector<string> genres)
{
	string t_title, a_artist, g_genre;
	
	cout << "Enter the title name: " << endl;
	cin >> t_title; 
	cout << "Enter the artist name: " << endl;
	cin >> a_artist;
	cout << "Enter the genre: " << endl;
	cin >> g_genre;
	
	
	
}

also, i have to make sure that when adding a song to the library that it doesn't already exist (can have same titles but must have different artist names). to do this do i use some function similar to strcmp? or use string length?

int i=1;
	string tmp;
	if (in_stream.is_open()) 
	{
		while(in_stream.peek () != EOF)
		{
			if(i%3==1)
			{
				titles.push_back(tmp);
			}
			if(i%3==2)
			{
				artists.push_back(tmp);
			}
			if(i%3==0)
			{
				genres.push_back(tmp);
			}
			i++;
		}	
	}

this is better and here is adding function

void addsong(vector<string>& titles, vector<string>& artists, vector<string>& genres)
{
	string t_title, a_artist, g_genre;
	
	cout << "Enter the title name: " << endl;
	cin >> t_title; 
	cout << "Enter the artist name: " << endl;
	cin >> a_artist;
	cout << "Enter the genre: " << endl;
	cin >> g_genre;
	for(size_t i = 0; i < titles.size(); ++i){
		if (titles[i].compare(t_title) && artists[i].compare(a_artist)){
			cout << "Item exist!" << endl;
			return;
		}
	}
	titles.push_back(t_title);
	artists.push_back(a_artist);
	genres.push_back(g_genre);
}

when i run the program and try to add a song, the addsong function always tells me that the item already exists.

here's the code that you had, which i changed around a little bit. let me know if i did anything wrong.

void addsong(vector<string>& titles, vector<string>& artists, vector<string>& genres)
{
	ifstream in_stream;
	in_stream.open("music_library.txt");
	
	string t_title, a_artist, g_genre;
	
	cout << "Enter the title name: " << endl;
	cin.ignore(100, '\n');
	getline(cin, t_title); 
	cout << "Enter the artist name: " << endl;
	getline(cin, a_artist);
	cout << "Enter the genre: " << endl;
	getline(cin, g_genre);
	for(size_t i = 0; i < titles.size(); ++i)
	{
		if (titles[i].compare(t_title) && artists[i].compare(a_artist))
		{
			cout << "Item exist!" << endl;
			return;
		}
	}
	titles.push_back(t_title);
	artists.push_back(a_artist);
	genres.push_back(g_genre);
	
	in_stream.close( );
}


}

also what is the purpose for size_t? is that related to the size of the titles vector? sorry, i'm still just a beginner. as for the compare operator, does that read in the input and compare it to every item in the vector?

thank you so much for the guidance.

i feel like i'm making this a lot more complicated then it actually is. i've changed up my code but now it says that everything is being added to the library!

void addsong(vector<string>& titles, vector<string>& artists, vector<string>& genres)
{
	ifstream in_stream;
	in_stream.open("music_library.txt");
	
	string t_title, a_artist, g_genre;
	
	cout << "Enter the title name: " << endl;
	cin.ignore(100, '\n');
	getline(cin, t_title); 
	cout << "Enter the artist name: " << endl;
	getline(cin, a_artist);
	cout << "Enter the genre: " << endl;
	getline(cin, g_genre);
	for(unsigned int i = 0; i < titles.size(); ++i)
	{
		if(t_title.compare(titles[i]) !=0&&(a_artist.compare(artists[i])!=0))
		{	
			
			cout << "Items have been added to library" << endl;
			titles.push_back(t_title);
			artists.push_back(a_artist);
			genres.push_back(g_genre);
		}	
		else
				cout << "Item exist!" << endl;
				return;
	}

		
	in_stream.close( );
}

also i've tested to see if the program is organizing the file correctly into the different vectors by printing to screen different vector indexes, and it is really off. i'm really lost. like when i put

cout << titles[3];

the screen output was "Be"

i figured it out! it's working fine now (for opening the file and then storing it into separate vectors).

#include <iostream> // For input and output to the monitor
#include <fstream> // For file input and output (given)
#include <string> // For text data
#include <vector> // library for using vectors


using namespace std; // To make is easier put this in global namespace

void addsong(vector<string>&, vector<string>&, vector<string>&);
// function: this adds songs to the library
// parameters: string title, string artist, string genre
// functionality: prompts the user for the parameters and checks to make sure
// the song isn't already in the library


int main() 
{
	vector<string> titles;
	vector<string> artists;
	vector<string> genres;
	
	ifstream in_stream;
	ofstream out_stream;
	in_stream.open("music_library.txt");
	
	int i=1;
	if (in_stream.fail( )) 
	{
		cout << "Unable to open file.\n";

	} 
	
	else
	{
				
		string title, artist, genre;
		while(!in_stream.eof())
		{
			if((i%3==1)||i==1)
			{
				getline(in_stream, title);
				titles.push_back(title);
				i++;
			}
			if((i%3==2)||i==2)
			{
				getline(in_stream, artist);
				artists.push_back(artist);
				i++;
			}
			if((i%3==0)||i==3)
			{
				getline(in_stream, genre);
				genres.push_back(genre);
				i++;
			}
			
		}	
	}

	cout << "Titles size: " << titles.size() << endl;
	cout << "Artist size: " << artists.size() << endl;
	cout << "Genre size: " << genres.size() << endl;

Where does it check to see if the title already exists?

string title, artist, genre;
int len, i;
bool okayToAdd;
while(getline(in_stream, title))
{
    getline(in_stream, artist);
    getline(in_stream, genre);
 
    okayToAdd = true;
    len = titles.size();		
    for(i = 0; i < len; ++i)
    {
        if((title == titles[i]) && (artist == artists[i]))
        {
           okayToAdd = false;  
           break;
        }
    }   
     
    if(okayToAdd)       
    {  
        titles.push_back(title);
        artists.push_back(artist);
        genres.push_back(genre);
     }
}
if(in_stream.eof())
{
  cout << "entire file read" << endl;
}
else
{
  cout << "file contents corrupted" << endl;
}
This article has been dead for over six months. Start a new discussion instead.