C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Iron Speed
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old May 1st, 2008, 01:24 PM
rem0404 rem0404 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 5 rem0404 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 43 m 17 sec
Reputation Power: 0
File IO - Replacing strings in vectors

i'm working on the "removesong" function. i need to check the vectors that already have the data separated and stored in them, and i need to see if the entered song title and artist name exist. if they do, i need to replace that same vector location with an empty string. i can't seem to get this to work, i need to also right the new vectors back into the file. it isn't supposed to completely overwrite the file, just remove the specified data.

file:

Code:
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


program:

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

void removesong(vector<string>& titles, vector<string>& artists, vector<string>& genres);
// function: this removes songs from the library and replaces them with whitespace
// parameters: title, artist, genre
// funtionality: prompts user for song and artist of song wishing to remove

void searchsong(vector<string>& titles, vector<string>& artists, vector<string>& genres);


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
				removesong(titles, artists, genres);
				break;
			
			case 3: //user wants to search for a song
				searchsong(titles, artists, genres);
				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))
		//compare is a function that i found on http://www.cplusplus.com/
		{	
			
			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( );
}
void removesong(vector<string>& titles, vector<string>& artists, vector<string>& genres)
{
	ofstream in_stream;
	
	in_stream.open("music_library.txt");
	
	string t_title, a_artist, g_genre;	
	
	cout << "Please enter the song title which you wish to remove: " << endl;
	cin.ignore(100, '\n');
	getline(cin, t_title); 
	titles.push_back(t_title);

	cout << "Please enter the corresponding artist name: " << endl;
	getline(cin, a_artist);
	artists.push_back(a_artist);

	for(unsigned int i = 0; i < titles.size(); ++i)
	{
		if(t_title.compare(titles[i]) !=0&&(a_artist.compare(artists[i])!=0))
		{
			cout << "Data not found in library!" << endl;
			while(int j=0 < titles.size())
			{
				in_stream << titles[j] << artists[j] << genres[j];
				j++;
				return;
			}
		}
		else
			titles[i] = ' ';
			artists[i] = ' ';
			genres[i] = ' ';

			titles.push_back(t_title);
			artists.push_back(a_artist);
			genres.push_back(g_genre);
			
			in_stream << titles[i] << artists[i] << genres[i];
			
			
		
			
			cout << "Song data has been removed." << endl;
			
			return;
	
	
	}
	in_stream.close();

}

void searchsong(vector<string>& titles, vector<string>& artists, vector<string>& genres)
{
	ifstream in_stream;
	ofstream library;
	in_stream.open("music_library.txt");
	library.open("library.txt");
	int choice;
	string t_title, a_artist, g_genre;
	
	do
	{
		cout << "1. Search by song title." << endl;
		cout << "2. Search by artist." << endl;
		cout << "3. Search by genre." << endl;
		cin >> choice;
		
		switch(choice)
		{
			case 1:
				cout << "Enter song title: " << endl;
				cin.ignore(100, '\n');
				getline(cin, t_title);
				for(unsigned int i = 0; i < titles.size(); i++)
				{
					if(t_title.compare(titles[i])==0)
					{
						cout << titles[i] << endl << artists[i] << endl << genres[i] << endl << endl;
					}
				}
			
				break;
				
			case 2: 
				cout << "Enter artist: " << endl;
				cin.ignore(100, '\n');
				getline(cin, a_artist);
				for(unsigned int i = 0; i < artists.size(); i++)
				{
					if(a_artist.compare(artists[i])==0)
					{
						cout << titles[i] << endl << artists[i] << endl << genres[i] << endl << endl;
					}
				}
				break;
				
			case 3:
				cout << "Enter genre: " << endl;
				cin.ignore(100, '\n');
				getline(cin, g_genre);
				for(unsigned int i = 0; i < genres.size(); i++)
				{
					if(g_genre.compare(genres[i])==0)
					{
						cout << titles[i] << endl << artists[i] << endl << genres[i] << endl << endl;
					}					
				}
			
				break;
		}
		
		
	}while(!in_stream.eof());
	
	in_stream.close();
	library.close();
}

Reply With Quote
  #2  
Old May 6th, 2008, 08:25 AM
MaHuJa MaHuJa is offline
Contributing User
Click here for more information.
 
Join Date: Dec 2007
Posts: 338 MaHuJa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 14 h 13 m 10 sec
Reputation Power: 1
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
To actually remove an element from a vector, try
erase(vectorname[5]);
This will then move all the subsequent elements back one step, as if it didn't exist. This is probably better than setting the strings to empty ones.

Then, save the vector back to the file, overwriting it. "Simply" modifying the file will be far more difficult.

-----

I would like to introduce you to the concept of structures and classes.

Given
Code:
struct Track {
  string artist;
  string title;
  string genre;
};

you would need only a single vector<Track> tracks which would be easier to pass around to functions, and the delete function would need only one call to erase. (Even if you added more fields, like length, rating, etc.)

It's even possible to make it such that you can just use
infile >> mytrack; instead of that long if-else chain. The 'detail code' still has to be written somewhere, but it's out of the way, making it quite clear to a human reader what the function is doing with less analyzing.

Also, consider the changes your program would have to go through to add e.g. a "length" field, with your old version vs this.

If you don't see it as much of a bother, try considering how it would be if you continued working on this as a "real" application, the kind that grows to 100s or 1000s of functions, and then wanted to add a "rating" field.


Though you'll probably learn about this sooner or later anyway, I thought I'd show you a convenient tool for the job you are doing right now.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > File IO - Replacing strings in vectors


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five &quot;checkpoints&quot; for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway