the "addsong" function working, i just dont know how to tell the compiler to skip over elements in the vectors with empty strings, when writing the new data to the music_library file. this is because i have to write a remove song function where, after the song is removed, the location of that songs data (within a vector) will be replaced with an empty string.

here's my code:

#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;
	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;
	
	


	
	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);
	
	cin.get();
	in_stream.close( );
	return 0;
}


void addsong(vector<string>& titles, vector<string>& artists, vector<string>& genres)
{
	ofstream in_stream;
	in_stream.open("music_library.txt", ios::app);
	
	string t_title, a_artist, g_genre;
	
	if(in_stream.fail( ))
	{
		cout << "File failed to open." << endl;
	}
		
	cout << "Enter the title name: " << endl;
	cin.ignore(100, '\n');
	getline(cin, t_title); 
	titles.push_back(t_title);
	cout << "Enter the artist name: " << endl;
	getline(cin, a_artist);
	artists.push_back(a_artist);
	cout << "Enter the genre: " << endl;
	getline(cin, g_genre);
	genres.push_back(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);
			in_stream << endl << t_title << endl << a_artist << endl << g_genre << endl;
			
			return;

		}	
		else
				cout << "Item exist!" << endl;
				return;
	}

		
	in_stream.close( );
}

If you would erase() the element instead of replacing it with an empty string you wouldn't have the problem you are posting.

this seems to work. i dont know if there's any way for me to clean it up and make it look neater, but it functions.

void addsong(vector<string>& titles, vector<string>& artists, vector<string>& genres)
{
	ofstream in_stream;
	in_stream.open("music_library.txt", ios::app);
	
	string t_title, a_artist, g_genre;
	
	if(in_stream.fail( ))
	{
		cout << "File failed to open." << endl;
	}
		
	cout << "Enter the title name: " << endl;
	cin.ignore(100, '\n');
	getline(cin, t_title); 
	titles.push_back(t_title);
	cout << "Enter the artist name: " << endl;
	getline(cin, a_artist);
	artists.push_back(a_artist);
	cout << "Enter the genre: " << endl;
	getline(cin, g_genre);
	genres.push_back(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);
			
			int length_t = t_title.length( );
			int length_a = a_artist.length( );
			int length_g = g_genre.length( );
			
			if(length_t==0||length_a==0||length_g==0)
			{
				if(length_t==0&&length_a > 0&&length_g > 0)
				{
					in_stream << endl << a_artist << endl << g_genre;
				}
				if(length_a==0&&length_t > 0&&length_g >0)
				{
					in_stream << endl << t_title << endl << g_genre;
				}
				if(length_g==0&&length_t > 0&&length_a >0)
				{
					in_stream << endl << t_title << endl << a_artist;
				}
			}

			if(length_g==0&&length_a==0)
			{
				cout << "No genre type or artist name added to library." << endl;
				in_stream << endl << t_title;
			}
			if(length_g==0&&length_t==0)
			{
				cout << "No genre type or song title were added to the library." << endl;
				in_stream << endl << a_artist;
			}
			if(length_a==0&&length_t==0)
			{
				cout << "No artist name or song title were added to the library." << endl;
				in_stream << endl << g_genre;
			}
			if(length_g > 0 && length_t > 0 && length_a > 0)
			{
				in_stream << endl << t_title << endl << a_artist << endl << g_genre;
			}
			return;

		}	
		else
				cout << "Item exist!" << endl;
				return;
	}

		
	in_stream.close( );
}

?
a quick question. how do you make sure the compiler doesn't care about whether input is upper case or lower case? is there an easy way to do that?

in c++ you have to transform each string to either upper-case or lower-case transform(string1.begin(),string1.end(),toupper); Or if you want to use C functions, most compilers has a function that ignores case, such as stricmp(string1.c_str(), string2.c_str())

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.