GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Dec-2007, 10:54
dankaroll dankaroll is offline
New Member
 
Join Date: Dec 2007
Posts: 3
dankaroll is on a distinguished road

MP3 Song collection... deleting song from array


Hey guys, I'm new to C++ and I need help completing a project for the class. Basically I've created a mp3 music database, where someone can enter 3 songs and the title, artist, and genre for each. I've got everything pretty much all set, except I cant figure out how I would delete a song from the list (when op==7), and move the other songs up.

Someone please help!

The first part of the code was helper code provided by our instructor, and he said we might need "currentsong" later. Anyway here's the code.

CPP / C++ / C Code:


#include <iostream.h>
#include <iomanip.h>    // For setw()
#include <string.h>     // For strcmp()


const int MAX_Songs = 3;   // Maximum number of MP3s
int index = 0;

struct MP_Three
{
    char	 Title [15];
    char	 Artist[15];
    char     Genre[15];	
};

int main()
{
	MP_Three Collection[MAX_Songs]; // collection (Array) of MP3s
	char	 CurrentTitle [15];
    char	 CurrentArtist[15];
    char     CurrentGenre[15];
    int length = 0;        // Number of entries
    MP_Three CurrentSong;             // Current record being entered
   
	int op;
	
	//************************************************
	//Menu
	//************************************************
	while(op != 8)
	{
		cout << "*** choose an operation ***"<<endl;
		cout << "1 Add a MP3 Collection"<<endl;
		cout << "2 Search by Title"<<endl;
		cout << "3 Search by Artist"<<endl;
		cout << "4 View Entries" << endl;
		cout << "5 Edit Entries" << endl;
		cout << "6 Search by Genre" << endl;
		cout << "7 Delete MP3" << endl;
		cout << "8 Quit"<< endl<<endl;
		cin>>op;
		cin.ignore(1,'\n');
		//****************************************************
		//User Selections
		//****************************************************
		
		if (op == 1)
		{
			index = 0;
			while (index < MAX_Songs)
			{
			cout << "Enter the song title of the MP3 ";
			cin >> Collection[index].Title;
		
		    cout << "Enter the Artist ";
			cin >> Collection[index].Artist;
		
		    cout << "Enter the Genre ";
			cin >> Collection[index].Genre;
			
			index++;
           }

		
		}
		if (op == 2)
		{
			
		
			
			cout << "Enter Title"<<endl;
			cin >> CurrentTitle;
			int found = 0;		
			int k;
			for (k = 0; k < MAX_Songs; k++)
			{
				if (strcmp(CurrentTitle, Collection[k].Title) == 0)
				{   
	  
				index = k;
				cout<<endl<<CurrentTitle<<" found at index "<< index<<endl<<endl;
				cout<<Collection[index].Title<<endl;
				cout<<Collection[index].Artist<<endl;
				cout<<Collection[index].Genre<<endl;
				found = 1;
				}
	
			}
		if (found == 0)
				cout<<endl<< "MP3 record not found"<<endl<<endl;
			
		}
		if (op == 3)
		{
			
		
			
			cout << "Enter Artist"<<endl;
			cin >> CurrentArtist;
			int found = 0;		
			int k;
			for (k = 0; k < MAX_Songs; k++)
			{
				if (strcmp(CurrentArtist, Collection[k].Artist) == 0)
				{   
	  
				index = k;
				cout<<endl<<CurrentArtist<<" found at index "<< index<<endl<<endl;
				cout<<Collection[index].Title<<endl;
				cout<<Collection[index].Artist<<endl;
				cout<<Collection[index].Genre<<endl;
				found = 1;
				}
	
			}
		if (found == 0)
				cout<<endl<< "MP3 record not found"<<endl<<endl;
			
		}
		
		
		if (op == 4)
		{
			int i;
		for (i = 0; i < MAX_Songs; i++)
			{
			cout << "Title: " << Collection[i].Title << endl;
			
		    cout << "Artist: " << Collection[i].Artist << endl;
	
		    cout << "Genre " <<Collection[i].Genre << endl << endl;
            }
		}
		
		if (op == 5)
		{
			
		
			
			cout << "Enter Title of song you want to replace."<<endl;
			cin >> CurrentTitle;
			int found = 0;		
			int k;
			for (k = 0; k < MAX_Songs; k++)
			{
				if (strcmp(CurrentTitle, Collection[k].Title) == 0)
				{   
	  
				index = k;
				cout<<endl<<CurrentTitle<<" found at index "<< index<<endl<<endl;
				cout<<Collection[index].Title<<endl;
				cout<<Collection[index].Artist<<endl;
				cout<<Collection[index].Genre<<endl;
				found = 1;
				
				cout << "Enter the new song title of the MP3 ";
			cin >> Collection[index].Title;
		
		    cout << "Enter the new Artist ";
			cin >> Collection[index].Artist;
		
		    cout << "Enter the new Genre ";
			cin >> Collection[index].Genre;
				}
			}
		if (found == 0)
				cout<<endl<< "MP3 record not found"<<endl<<endl;
			
		}
		if (op == 6)
		{
			
		
			
			cout << "Enter Genre"<<endl;
			cin >> CurrentGenre;
			int found = 0;		
			int k;
			for (k = 0; k < MAX_Songs; k++)
			{
				if (strcmp(CurrentGenre, Collection[k].Genre) == 0)
				{   
	  
				index = k;
				cout<<endl<<CurrentGenre<<" found at index "<< index<<endl<<endl;
				cout<<Collection[index].Title<<endl;
				cout<<Collection[index].Artist<<endl;
				cout<<Collection[index].Genre<<endl;
				found = 1;
				}
	
			}
		if (found == 0)
				cout<<endl<< "MP3 record not found"<<endl<<endl;
			
		}
		
		if (op == 7)
		{
			
		
			//need code here to delete!
		
	}
	return 0;
}
  #2  
Old 09-Dec-2007, 14:58
whitepenguin whitepenguin is offline
New Member
 
Join Date: Dec 2007
Posts: 9
whitepenguin is on a distinguished road

Re: MP3 Song collection... deleting song from array


Hi Im new to this Forum too very happy to know u

I have few words to say :
To let people easy to read you code ,and for u when you want to review and edit it, I have some recommedation
Quote:
1.you should use switch()-case structure to easy manage your Menu Selection

2.You should write a separeate Function for each selection


For Example

CPP / C++ / C Code:
void Option1();
void Option2();
......

void Main()
{
         .....
      switch(op)
      {   
          case '1': 
                Option1();
               break;
          case '2':
                Option2();
               break;
          case '3':
              .....
       }
}

Ive looked through your Code , your list is an Array of Struct ,not a "Link List" , so to delete a song from array we need to do few more works.





Code:
0. Write A separate Function to Locate your Song Index int Search(char *p,int &Ind); this function searchs through the Array to find the song if it finds the song then it returns 1 and the index in the Ind For Ex: int result=Search("Breaking My Heart",&Songindex) result will be 0 or 1, if result is 1 then Songindex has the index of the song in the Array If Result is 0 then there isnt that kind of song in the array I saw you repeat your searching code in almost every option ,so It is needed to write this Funtion to save your typing work. 1. Let the User Specify the name of the song 2. int result = Search(name,SongIndex) 3. Check the result a. No --> return to Main menu or whatever b.Yes +Declare another Array of MP_Tree EX: MP_TREE Temp[MAX_SONGS] ; +Loop through the Main Array of Song ,Copy All songs to Temp except the song whose Index is the songindex Ex:
CPP / C++ / C Code:
                      index=0; //Index for Collection
                      index2=0; Index for Temp;
                    while(index<MAX_SONGS)
                    {
                          if(index!=SongIndex)
                          { 
                             Copy(Temp[index2],Collection[index]);
                             // Copy Collection to temp
                             index++;
                             index2++;
                          }
                          else //except the song wanted to be deleteds
                              index++;  //we pass the song wanted to deleted in Collection
               
Now Copy all the song from Temp back to Collection!


PS: Sometimes we need declare a global Variable to keep track of the current number of songs in the Collection
EX: MAX_SONGS =20;
int NumberOfSongs
May be in your list right now only have 10 songs for ex. You'll see in some case you need this Variable
  #3  
Old 09-Dec-2007, 16:47
dankaroll dankaroll is offline
New Member
 
Join Date: Dec 2007
Posts: 3
dankaroll is on a distinguished road

Re: MP3 Song collection... deleting song from array


I really appreciate all you wrote here, but excuse my ignorance, but can you show me how this would exactly go into my code?

I really need to be spoon fed this stuff! haha

Could I make the delete a song work with the code I have, or would i have to rewrite the whole thing?
  #4  
Old 09-Dec-2007, 19:26
dankaroll dankaroll is offline
New Member
 
Join Date: Dec 2007
Posts: 3
dankaroll is on a distinguished road

Re: MP3 Song collection... deleting song from array


Hey I ended up using this to get it to work

CPP / C++ / C Code:
			int k;// index of song that is going to be deleted.
			char searchTitle[15];
			int found = -1;// index of song with title
			char ans[1]; // stores answer to Y/N prompts

			cout<< "Enter the title of the MP3 you would like to delete: ";
			cin >> searchTitle;// input song name to be deleted.

			for (k = 0; k < MAX_Songs; k++)
            {
				if (strcmp(searchTitle, Collection[k].Title) == 0)
				{
					found = k;
					break;
				}
			}
  #5  
Old 09-Dec-2007, 23:06
inevitable inevitable is offline
Junior Member
 
Join Date: Nov 2007
Posts: 53
inevitable is on a distinguished road

Re: MP3 Song collection... deleting song from array


Below is a sample program that works...
I recommend you to make the program more efficient....

CPP / C++ / C Code:
#include <iostream.h>
#include <iomanip.h>    // For setw()
#include <string.h>     // For strcmp()


const int MAX_Songs = 3;   // Maximum number of MP3s
static int index = 0;
static int lastRecord;

void showMenu();
int deleteMP3();
void searchByGenre();
void editEntries();
void viewEntries();
void searchByArtist();
void searchByTitle();
int addMP3();

struct MP_Three
{
    char	 Title [15];
    char	 Artist[15];
    char     Genre[15];	
};

MP_Three Collection[MAX_Songs]; // collection (Array) of MP3s
char	 CurrentTitle[15];
char	 CurrentArtist[15];
char     CurrentGenre[15];
MP_Three CurrentSong;             // Current record being entered

int main()
{
    int length = 0;        // Number of entries
	int op;
	
	//************************************************
	//Menu
	//************************************************
	do
	{
		showMenu();
		cin>>op;
		cin.ignore(1,'\n');
		switch(op)
		{
		case 1:
			if(addMP3())
			{
				cout<<"Data Entered Succesfully\n";
			}
			else
			{
				cout<<"Error entering the details:, Playlist is Full\n";
			}
			break;
		case 2:
			searchByTitle();
			break;
		case 3:
			searchByArtist();
			break;
		case 4:
			viewEntries();
			break;
		case 5:
			editEntries();
			break;
		case 6:
			searchByGenre();
			break;
		case 7:
			if(deleteMP3())
			{
				cout<<"Data Deleted Succesfully\n";
			}
			else
			{
				cout<<"Error Deleting the details: \n";
			}
			break;
		case 8:
			break;
		default :
			break;
		}
	}while(op != 8);
	return 0;
}

void showMenu()
{
	cout << "*** choose an operation ***"<<endl;
	cout << "1 Add a MP3 Collection"<<endl;
	cout << "2 Search by Title"<<endl;
	cout << "3 Search by Artist"<<endl;
	cout << "4 View Entries" << endl;
	cout << "5 Edit Entries" << endl;
	cout << "6 Search by Genre" << endl;
	cout << "7 Delete MP3" << endl;
	cout << "8 Quit"<< endl<<endl;
}

int addMP3()
{
	if(lastRecord < MAX_Songs)
	{
		cout << "Enter the song title of the MP3 ";
		cin >> Collection[lastRecord].Title;
		
	    cout << "Enter the Artist ";
		cin >> Collection[lastRecord].Artist;
		
	    cout << "Enter the Genre ";
		cin >> Collection[lastRecord].Genre;
		lastRecord++;
	}
	else
	{
		return 0;
	}
	return 1;
}

void searchByTitle()
{
	cout << "Enter Title"<<endl;
	cin >> CurrentTitle;
	int found = 0;		
	int k;
	if(lastRecord >= 0)
	{
		for (k = 0; k < lastRecord; k++)
		{
			if (strcmp(CurrentTitle, Collection[k].Title) == 0)
			{   
				cout<<endl<<CurrentTitle<<" found at index "<< k<<endl<<endl;
				cout<<Collection[k].Title<<endl;
				cout<<Collection[k].Artist<<endl;
				cout<<Collection[k].Genre<<endl;
				found = 1;
			}
			if (found == 0)
			{
				cout<<endl<< "MP3 record not found"<<endl<<endl;
			}
		}
	}
	else
	{
		cout<<"Empty record List"<<endl;
	}
}

void searchByArtist()
{
	cout << "Enter Artist"<<endl;
	cin >> CurrentArtist;
	int found = 0;		
	int k;
	if(lastRecord >=0 )
	{
		for (k = 0; k < lastRecord; k++)
		{
			if (strcmp(CurrentArtist, Collection[k].Artist) == 0)
			{   
				cout<<endl<<CurrentArtist<<" found at index "<< k<<endl<<endl;
				cout<<Collection[k].Title<<endl;
				cout<<Collection[k].Artist<<endl;
				cout<<Collection[k].Genre<<endl;
				found = 1;
			}
			if (found == 0)
			{
				cout<<endl<< "MP3 record not found"<<endl<<endl;	
			}
		}
	}
	else
	{
		cout<<"Empty record List"<<endl;
	}
}

void viewEntries()
{
	int i;
	if(lastRecord >=0)
	{
		for (i = 0; i < lastRecord; i++)
		{
			cout << "Title: " << Collection[i].Title << endl;
			cout << "Artist: " << Collection[i].Artist << endl;
			cout << "Genre " <<Collection[i].Genre << endl << endl;
		}
	}
	else
	{
		cout<<"Empty record List"<<endl;
	}
}

void editEntries()
{
	cout << "Enter Title of song you want to replace."<<endl;
	cin >> CurrentTitle;
	int found = 0;		
	int k;
	if(lastRecord >= 0)
	{
		for (k = 0; k < lastRecord; k++)
		{
			if (strcmp(CurrentTitle, Collection[k].Title) == 0)
			{   
				cout<<endl<<CurrentTitle<<" found at index "<< k<<endl<<endl;
				cout<<Collection[k].Title<<endl;
				cout<<Collection[k].Artist<<endl;
				cout<<Collection[k].Genre<<endl;
				found = 1;
					
				cout << "Enter the new song title of the MP3 ";
				cin >> Collection[k].Title;
		
				cout << "Enter the new Artist ";
				cin >> Collection[k].Artist;
			
				cout << "Enter the new Genre ";
				cin >> Collection[k].Genre;
			}
		}
		if (found == 0)
		{
			cout<<endl<< "MP3 record not found"<<endl<<endl;
		}
	}
	else
	{
		cout<<"Empty record List"<<endl;
	}
}

void searchByGenre()
{
	cout << "Enter Genre"<<endl;
	cin >> CurrentGenre;
	int found = 0;		
	int k;
	if(lastRecord >= 0)
	{
		for (k = 0; k < lastRecord; k++)
		{
			if (strcmp(CurrentGenre, Collection[k].Genre) == 0)
			{   
				cout<<endl<<CurrentGenre<<" found at index "<< k<<endl<<endl;
				cout<<Collection[k].Title<<endl;
				cout<<Collection[k].Artist<<endl;
				cout<<Collection[k].Genre<<endl;
				found = 1;
			}
			if (found == 0)
			{
				cout<<endl<< "MP3 record not found"<<endl<<endl;
			}	
		}
	}
	else
	{
		cout<<"Empty record List"<<endl;
	}
}

int deleteMP3()
{
	int found = 0;
	cout << "Enter Title of song you want to delete."<<endl;
	cin >> CurrentTitle;
	if(lastRecord >= 0)
	{
		for (int k = 0; k < lastRecord; k++)
		{
			if (strcmp(CurrentTitle, Collection[k].Title) == 0)
			{   
				cout<<endl<<CurrentGenre<<" found at index "<< k<<endl<<endl;
				cout<<Collection[k].Title<<endl;
				cout<<Collection[k].Artist<<endl;
				cout<<Collection[k].Genre<<endl;
				for(int j=k;j<lastRecord;j++)
				{
					strcpy(Collection[j+1].Title,Collection[j].Title);
					strcpy(Collection[j+1].Artist,Collection[j].Artist);
					strcpy(Collection[j+1].Genre,Collection[j].Genre);
				}
				lastRecord--;
				found = 1;
			}
			if(found == 0)
			{
				cout<<endl<< "MP3 record not found"<<endl<<endl;
				return 0;
			}	
		}
	}
	else
	{
		cout<<"Empty record List"<<endl;
	}
	return 1;
}

Think differently to make the program usable universally
for ex,
In the above program
we are not accepting string, we are accepting a word.
Similarly there are many areas you have to look for.
This is just a prototype, how a program should generally look like...
Last edited by LuciWiz : 10-Dec-2007 at 13:02. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #6  
Old 10-Dec-2007, 00:37
whitepenguin whitepenguin is offline
New Member
 
Join Date: Dec 2007
Posts: 9
whitepenguin is on a distinguished road

Re: MP3 Song collection... deleting song from array


CPP / C++ / C Code:

//This is your Song Collection: Maximum Songs : 100 , Number of Songs exist in the list : 0 
// 
//*************************************************************************************************************
//*                |                  |                |                |                 |                |
//*                |                  |                |                |                 |                |
//*                |                  |                |                |                 |                |
//*************************************************************************************************************
//   Index: 0      Index:1       Index: 2   Index: 3   .........  . . . . ..  .. . 



#include <iostream.h>
#include <iomanip.h>    // For setw()
#include <string.h>     // For strcmp()



const int MAX_Songs = 100; //---> We need to add one song at a time since it is so big
// U can implement to add 10 song at a time for ex
int index = 0; 

struct MP_Three
{
    char	 Title [15];
    char	 Artist[15];
    char     Genre[15];	
    
};

struct Album
{
    MP_Three Collection[MAX_Songs]; // collection (Array) of MP3s
	
    int NOS; // This is used to track  number of songs in the List Collection , When We Add Our Song to the List we need to do NOS++   --> Why?
            // Say you wanna search a song in the list ,you dont have to loop from 0 -> MAX_SONGS ,but to NOS  coz maybe right now Collection just have 3 songs in 100 songs Collection , so why bother to check
            // from 0-> 100 song --> Save CPU Work.
    //you need to Implement one more feature to allow user add one Song at a time in the end of the list , not INdex 99 but Index NOS-1
    // When Ever u add a song to the Collection ,update NOS
};






void AddSong(Album &Al);
void SearchTitle(Album &Al);
void SearchArtist(Album &Al);
void ViewEntries(Album &Al);
void EditEntries(Album &Al);
void SearchGenre(Album &Al);
void DeleteMp3(Album &Al);


int main()
{
	Album MyMP3Album;
    MyMP3Album.NOS=0;
       
	int op;
	
	//************************************************
	//Menu
	//************************************************
	while(op != 8)
	{
        clrscr();
		cout << "*** choose an operation ***"<<endl;
		cout << "1 Add a MP3 Collection"<<endl;
		cout << "2 Search by Title"<<endl;
		cout << "3 Search by Artist"<<endl;
		cout << "4 View Entries" << endl;
		cout << "5 Edit Entries" << endl;
		cout << "6 Search by Genre" << endl;
		cout << "7 Delete MP3" << endl;
		cout << "8 Quit"<< endl<<endl;
		cin>>op;
		cin.ignore(1,'\n');
		//****************************************************
		//User Selections
		//****************************************************
        
        //********* Switch -Case Structure**********************
        switch(op)
        {
            case 1:
                AddSong(Album &Al);
                break;
            case 2:
                SearchTitle(Album &Al);
                break;
            case 3:
                SearchArtist(Album &Al);;
                break;
            case 4:
                ViewEntries(Album &Al);
                break;
            case 5:
                EditEntries(Album &Al);
                break;
            case 6:
                SearchGenre(Album &Al);
                break;
            case 7:
                DeleteMp3(Album &Al);
                break;
            
        };
        //****************************************************
        
    }
    return 0;
}

void DeleteMp3(Album &Al)
{
    Album Temp;
    Temp.NOS=0;
    char searchTitle[15];
    
    cout<< "Enter the title of the MP3 you would like to delete: ";
    cin >> searchTitle;
    
    for(int i=0;i<NOS;i++) // Loop From 0 -> NOS not MAX=100 :D
    {
        if (strcmp(searchTitle, Al.Collection[i].Title) != 0) //If not that song whose title  we wanted to delete then Copy to Temp and increase NOS by 1 
        {
            //Copy from Al---> Temp
            strcpy(Temp.Collection[Temp.NOS].Title,Al.Collection[i].Title);
            strcpy(Temp.Collection[Temp.NOS].Genre,Al.Collection[i].Genre);
            strcpy(Temp.Collection[Temp.NOS].Artist,Al.Collection[i].Artist);
            NOS++;
        }
    }
    
    Al.NOS=Temp.NOS;
    
     for(i=0;i<Temp.NOS;i++) // Loop From 0 -> NOS not MAX=100 :D , Copy back to our Album
    {
        strcpy(Al.Collection[i].Title,Temp.Collection[i].Title);
        strcpy(Al.Collection[i].Genre,Temp.Collection[i].Genre);
        strcpy(Al.Collection[i].Artist,Temp.Collection[i].Artist);
        
    }
}
            
    
// If you know POINTER the function above would be more shorter save more memory :D
// You need to test My function be4 use it ,check for appropriation , coz I havent tested 
    
 
 

Recent GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling errors in binary tree code rahul_2550 C Programming Language 1 22-Mar-2007 15:07
Sort an array of structures sassy99 C Programming Language 10 15-Feb-2007 07:46
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 19:31
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 21:26
Creating N string gwk C Programming Language 3 20-Jul-2004 23:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:05.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.