Hello.

I'm having 2 difficulties...

1) I am trying to print out a string with spaces... Functions used: addSong and printSong

For ex: (This is not the whole code...I'm pointing out the functions that it is used in)

void addSong(mp3Type*& first, mp3Type*& last)
{
     cout<<"Song title: ";
     cin>>songTitle;
     getline(cin,songTitle);
}

void printSong(mp3Type*& first)
{
    cout<<current-> songTitle <<endl;
}

The output looks like:

Song title: Jerry Newman

It will only print out Newman. I'm trying to print out the whole name though.

I am able to store the string with spaces, but when I print it out, it just gives me the last part of the space... like if I type in "Billy Newman" It prints out "Newman" and thats it.


2) When I select Delete from my menu, I am trying to able to delete the node of the Song and its information stored in the node. When I try to delete, the node with all of the information is still there. What am I currently missing in the deleteSong function?


Any help is appreciated... Thanks


Here is my full program:

#include <iostream>
#include <string>

const int SONGS = 2;

using namespace std;
struct mp3Type
{
    int id;
    string songTitle;
    string artist;
    string length;
    string size;
    mp3Type *link;
};


void addSong(mp3Type*& first, mp3Type*& last);
void deleteSong(mp3Type*& first);
void modifySong(mp3Type*& first, mp3Type*& last);
void printSong(mp3Type*& first);


int main()
{
	mp3Type *first, *last;
	char selection;
	
	
	do
	{
         cout<<"a - Add a song "<<endl;
         cout<<"d - Delete a song"<<endl;
         cout<<"m - Modify a song's information "<<endl;
         cout<<"p - Print a list of songs "<<endl;
         cout<<"q - Quit "<<endl;
         cin>>selection;
         switch(selection)
         {
                          case 'a': addSong(first,last);
                               break;
                          case 'd': deleteSong(first);
                               break;
                          case 'm': modifySong(first,last);
                               break;
                          case 'p': printSong(first);
                               break;
                          case 'q': 
                               break;
                          default: cout<<"Invalid option "<<endl;
         }
     }
          while (selection != 'q');
          
          
         	system("PAUSE");
	        return 0;
}

void addSong(mp3Type*& first, mp3Type*& last)
{
     
     
    int id;
    string songTitle;
    string artist;
    string length;
    string size;
    mp3Type *newNode;
    char selection;
    
    first = NULL;
    last = NULL;
    
    
      
        for (int i = 0; i < SONGS; i++)
         {
         cout<<endl;
         cout<<"Adding song # "<<i+1<<endl;
         cout<<endl;
         cout<<"Song title: ";
         cin>>songTitle;
         getline(cin,songTitle);
         cout<<"ID: ";
         cin>>id;
         cout<<"Artist: ";
         cin>>artist;
         getline(cin,artist);
         cout<<"Length: ";
         cin>>length;
         cout<<"Size: ";
         cin>>size;
  
         newNode = new mp3Type; // create new node
         newNode->songTitle = songTitle;
         newNode->id = id;
         newNode->artist = artist;
         newNode->length = length;
         newNode->size = size;
         newNode->link = NULL;
    
         if (first == NULL)
         {
            first = newNode;
            last = newNode;
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }
         }
     
}

void deleteSong(mp3Type*& first)
{
     
  mp3Type *last,*current,*trailcurrent;
  bool found;
  int id;

  
  cout<<"Enter song ID that you like to delete"<<endl;
  cout<<endl;
  cin>>id;
    
             
         if (first->id == id)
         {
           current = first;
           first = first->link;
         }
         if (first == NULL)
         {
            last = NULL;
            
            delete current;
         }
         else
         {
         found = false;
         trailcurrent = first;
         
         current = first->link;
         }   
   
     
}

void modifySong(mp3Type*& first, mp3Type*& last)
{
     
   int id;
   string songTitle;
   string newsongTitle;
   string newartist;
   string newlength;
   string newsize;
   bool found = false;
   mp3Type *current;
   
   printSong(first);
   
   current = first;
   
   
   cout<<"Enter the id of the song to modify information"<<endl;
   cin>>id;
     
     
   while (current != NULL)
   {         
    if (current->id == id)
    {
     cout<<"Enter the song's new information"<<endl;
     cin>>newsongTitle;
     getline(cin,newsongTitle);
     cin>>newartist;
     getline(cin,newartist);
     cin>>newlength;
     cin>>newsize;
     
     current->songTitle = newsongTitle;
     current->artist = newartist;
     current->length = newlength;
     current->size = newsize;
    } 
   current = current->link; 
    }

}

void printSong(mp3Type*& first)
{
     
     cout<<"Printing song list..."<<endl;
     mp3Type *current;
        
        
        current = new mp3Type;
        current = first;    
        while (current != NULL)
        {
          cout<<endl;
          cout<<current-> songTitle <<endl;
          cout<<current-> id <<endl;
          cout<<current-> artist <<endl;
          cout<<current-> length <<endl;
          cout<<current-> size <<endl;
          current = current->link;
        }
     

}

1) use gets() or cin.getline instead of cin to get the input stream, this will not skip any white space;
2) you didnt do the 'search' job to find correct node with the id you input, so all you need to do is just make a loop and find the correct node.

1) use gets() or cin.getline instead of cin to get the input stream, this will not skip any white space;
2) you didnt do the 'search' job to find correct node with the id you input, so all you need to do is just make a loop and find the correct node.

Can you give me an example of what it will look like? Is it cin.getline(songTitle); ? because it didn't work.

cin.getline() needs char* as its arguments, so you have to declare an char array to store the data. Then pass the length of the string you input.
sample code:
char str[100];
cin.getline(str,100);

cin.getline() needs char* as its arguments, so you have to declare an char array to store the data. Then pass the length of the string you input.
sample code:
char str[100];
cin.getline(str,100);

It didn't work. Here is my function below. I set songTitle and artist as a char like your said and passed the arguements below. When I run the program and do input, it just skips over songTitle and ask for ID.... then it skips over artist and asks for length. The first way worked in my first post, it was just when I printed it out, it would only print the last part. For example Joe Smith... It will accept Joe Smith but only print out Smith.

int id;
    char songTitle[3];
    char artist[3];
    string length;
    string size;
    mp3Type *newNode;
    char selection;
    
    first = NULL;
    last = NULL;
    
    
      
        for (int i = 0; i < SONGS; i++)
         {
         cout<<endl;
         cout<<"Adding song # "<<i+1<<endl;
         cout<<endl;
         cout<<"Song title: ";
         cin.getline(songTitle,3);
         cout<<"ID: ";
         cin>>id;
         cout<<"Artist: ";
         cin.getline(artist,3);
         cout<<"Length: ";
         cin>>length;
         cout<<"Size: ";
         cin>>size;

if you just allocate 3 chars to a char array,
you can only input 2 chars, because a char array need '\0' to indicate its end.

if you just allocate 3 chars to a char array,
you can only input 2 chars, because a char array need '\0' to indicate its end.

I don't understand what you mean by that. I'm just a beginner...

just take your code as an example:
char songTitle[3];
songTitle is ONLY capaple to store 2 characters you input, and songTitle[2], the last of this array , need to carry a '\0' to indicate string songTitle is in its end.
so if you need input more than 2 chars, you have to declare songTitle to a larger array, make sure it is large enough to store all the chars you input; so you can just modify your code to songTitle[100];

just take your code as an example:
char songTitle[3];
songTitle is ONLY capaple to store 2 characters you input, and songTitle[2], the last of this array , need to carry a '\0' to indicate string songTitle is in its end.
so if you need input more than 2 chars, you have to declare songTitle to a larger array, make sure it is large enough to store all the chars you input; so you can just modify your code to songTitle[100];

It still didn't work. It does the same thing. It will accept the the input, but it will print out the last part of it. For example

Input: The Titanic

Prints out: Titanic


Also, if i do not put the cin>>songTitle or cin>>artist.... it will just skip the option and go on to the next without letting the user enter anything in.

For Example

It will look like: Song Title: ID:


Here is the updated function:

void addSong(mp3Type*& first, mp3Type*& last)
{
     
     
    int id;
    char songTitle[100];
    char artist[100];
    string length;
    string size;
    mp3Type *newNode;
    char selection;
    
    first = NULL;
    last = NULL;
    
    
      
        for (int i = 0; i < SONGS; i++)
         {
         cout<<endl;
         cout<<"Adding song # "<<i+1<<endl;
         cout<<endl;
         cout<<"\nSong title: ";
         cin>>songTitle;
         cin.getline(songTitle,100); 
         cout<<"\nID: ";
         cin>>id;
         cout<<"Artist: ";
         cin>>artist;
         cin.getline(artist,100); 
         cout<<"Length: ";
         cin>>length;
         cout<<"Size: ";
         cin>>size;
  
         newNode = new mp3Type; // create new node
         newNode->songTitle = songTitle;
         newNode->id = id;
         newNode->artist = artist;
         newNode->length = length;
         newNode->size = size;
         newNode->link = NULL;
    
         if (first == NULL)
         {
            first = newNode;
            last = newNode;
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }
         }
     
}

Whenever I use getline(cin,songTitle); , it doesn't let me type in the input, it just skips to the next line. Is there another way that I can do to print out the string including the spaces?


Here is the function:

void addSong(mp3Type*& first, mp3Type*& last)
{
     
     
    int id;
    string songTitle;
    string artist;
    string length;
    string size;
    mp3Type *newNode;
    char selection;
    
    first = NULL;
    last = NULL;
    
    
      
        for (int i = 0; i < SONGS; i++)
         {
         cout<<endl;
         cout<<"Adding song # "<<i+1<<endl;
         cout<<endl;
         cout<<"\nSong title: \n";
         getline(cin,songTitle); 
         cout<<"ID: ";
         cin>>id;
         cout<<"Artist: ";
         getline(cin,artist); 
         cout<<"Length: ";
         cin>>length;
         cout<<"Size: ";
         cin>>size;
  
         newNode = new mp3Type; // create new node
         newNode->songTitle = songTitle;
         newNode->id = id;
         newNode->artist = artist;
         newNode->length = length;
         newNode->size = size;
         newNode->link = NULL;
    
         if (first == NULL)
         {
            first = newNode;
            last = newNode;
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }
         }
     
}

I figured out the first step. I need help on step number 2 now. I'm trying to ask a user what song he/she wants to delete. I'm trying to delete the node where the song and its information located. When I print out the song list, I want the song and its information to be gone. Right now, I'm asking the user to type in the ID number for the song he/she wants to delete. It is in the deleteSong function. What am I doing wrong?

#include <iostream>
#include <string>

const int SONGS = 2;

using namespace std;
struct mp3Type
{
    int id;
    string songTitle;
    string artist;
    string length;
    string size;
    mp3Type *link;
};


void addSong(mp3Type*& first, mp3Type*& last);
void deleteSong(mp3Type*& first);
void modifySong(mp3Type*& first, mp3Type*& last);
void printSong(mp3Type*& first);


int main()
{
	mp3Type *first, *last;
	char selection;
	
	
	do
	{
         cout<<"a - Add a song "<<endl;
         cout<<"d - Delete a song"<<endl;
         cout<<"m - Modify a song's information "<<endl;
         cout<<"p - Print a list of songs "<<endl;
         cout<<"q - Quit "<<endl;
         cin>>selection;
         switch(selection)
         {
                          case 'a': addSong(first,last);
                               break;
                          case 'd': deleteSong(first);
                               break;
                          case 'm': modifySong(first,last);
                               break;
                          case 'p': printSong(first);
                               break;
                          case 'q': 
                               break;
                          default: cout<<"Invalid option "<<endl;
         }
     }
          while (selection != 'q');
          
          
         	system("PAUSE");
	        return 0;
}

void addSong(mp3Type*& first, mp3Type*& last)
{
     
     
    int id;
    char songTitle[100];
    char artist[100];
    string length;
    string size;
    mp3Type *newNode;
    char selection;
    
    first = NULL;
    last = NULL;
    
    
      
        for (int i = 0; i < SONGS; i++)
         {
         cout<<endl;
         cout<<"Adding song # "<<i+1<<endl;
         cout<<endl;
         cout<<"\nSong title: ";
         std::cin.getline(songTitle, 100);
         std::cin.getline(songTitle, 100);
         cout<<"ID: ";
         cin>>id;
         cout<<"Artist: ";
         std::cin.getline(artist, 100);
         std::cin.getline(artist, 100);; 
         cout<<"Length: ";
         cin>>length;
         cout<<"Size: ";
         cin>>size;
  
         newNode = new mp3Type; // create new node
         newNode->songTitle = songTitle;
         newNode->id = id;
         newNode->artist = artist;
         newNode->length = length;
         newNode->size = size;
         newNode->link = NULL;
    
         if (first == NULL)
         {
            first = newNode;
            last = newNode;
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }
         }
     
}

void deleteSong(mp3Type*& first)
{
     
  mp3Type *last,*current,*trailcurrent;
  bool found;
  int id;

  
  cout<<"Enter song ID that you like to delete"<<endl;
  cout<<endl;
  cin>>id;
    
          for (int i = 0; i < SONGS; i++)
          {   
         if (first->id == id)
         {
           current = first;
           first = first->link;
         }
         if (first == NULL)
         {
            last = NULL;
            
            delete current;
         }
         else
         {
         found = false;
         trailcurrent = first;
         
         current = first->link;
         }   
         }
     
}

void modifySong(mp3Type*& first, mp3Type*& last)
{
     
   int id;
   string songTitle;
   string newsongTitle;
   string newartist;
   string newlength;
   string newsize;
   bool found = false;
   mp3Type *current;
   
   printSong(first);
   
   current = first;
   
   
   cout<<"Enter the id of the song to modify information"<<endl;
   cin>>id;
     
     
   while (current != NULL)
   {         
    if (current->id == id)
    {
     cout<<"Enter the song's new information"<<endl;
     cin>>newsongTitle;
     getline(cin,newsongTitle);
     cin>>newartist;
     getline(cin,newartist);
     cin>>newlength;
     cin>>newsize;
     
     current->songTitle = newsongTitle;
     current->artist = newartist;
     current->length = newlength;
     current->size = newsize;
    } 
   current = current->link; 
    }

}

void printSong(mp3Type*& first)
{
     
     cout<<"Printing song list..."<<endl;
     mp3Type *current;
        
        
        current = new mp3Type;
        current = first;    
        while (current != NULL)
        {
          cout<<endl;
          cout<<current-> songTitle <<endl;
          cout<<current-> id <<endl;
          cout<<current-> artist <<endl;
          cout<<current-> length <<endl;
          cout<<current-> size <<endl;
          current = current->link;
        }
     

}

Anyone know the solution to my last post?

read this code below and you'll know how to modify your code:

int main(int argc, _TCHAR* argv[])
{
    char songTitle[100]; 
    cin.getline(songTitle,100); 
    cout<<songTitle<<endl; 
    return 0;
}

read this code below and you'll know how to modify your code:
int main(int argc, _TCHAR* argv[])
{
char songTitle[100];
cin.getline(songTitle,100);
cout<<songTitle<<endl;
return 0;
}

I'm done with that problem. I was talking about the 2nd problem. I'm having difficulties deleting a node. Each song has an ID number which contains information stored in the node. I'm asking the user to to select the ID number of the song that he/she wishes to delete. When I print out the list of songs, I want that particular song and its information to be delete.

This is my deleteSong function:

void deleteSong(mp3Type*& first)
{   
mp3Type *last,*current,*trailcurrent;
bool found; 
int id;    


cout<<"Enter song ID that you like to delete"<<endl; 
cout<<endl;  cin>>id;  


 for (int i = 0; i < SONGS; i++)  
    {           
 if (first->id == id)     
    {       
    current = first; 
    first = first->link;      
   }   
      if (first == NULL)     
    {     
       last = NULL;       
      delete current;    
     }     
    else     
    {   
    found = false;     
    trailcurrent = first;      
    current = first->link;  
       }      
      }
 }

hey... does anyone know the solution to my problem?

Hello, I need help!

For my program, I created an mp3 song list. Each song and its information is stored insides nodes. Each song is identified by its ID number. For my deleteSong function, I am asking the user, What ID of the the song he/she wants to delete. When I print the list of songs, I want the deleted song to not be on the list anymore. How do I fix my deleteSong function to make this problem work? (Note: If selecting the ID number of the song doesn't work, can I do it by song title or something)... Thanks


Here is my full program:

#include <iostream>
#include <string>

const int SONGS = 2;

using namespace std;
struct mp3Type
{
    int id;
    string songTitle;
    string artist;
    string length;
    string size;
    mp3Type *link;
};


void addSong(mp3Type*& first, mp3Type*& last);
void deleteSong(mp3Type*& first);
void modifySong(mp3Type*& first, mp3Type*& last);
void printSong(mp3Type*& first);


int main()
{
	mp3Type *first, *last;
	char selection;
	
	
	do
	{
         cout<<"a - Add a song "<<endl;
         cout<<"d - Delete a song"<<endl;
         cout<<"m - Modify a song's information "<<endl;
         cout<<"p - Print a list of songs "<<endl;
         cout<<"q - Quit "<<endl;
         cin>>selection;
         switch(selection)
         {
                          case 'a': addSong(first,last);
                               break;
                          case 'd': deleteSong(first);
                               break;
                          case 'm': modifySong(first,last);
                               break;
                          case 'p': printSong(first);
                               break;
                          case 'q': 
                               break;
                          default: cout<<"Invalid option "<<endl;
         }
     }
          while (selection != 'q');
          
          
         	system("PAUSE");
	        return 0;
}

void addSong(mp3Type*& first, mp3Type*& last)
{
     
     
    int id;
    char songTitle[100];
    char artist[100];
    string length;
    string size;
    mp3Type *newNode;
    char selection;
    
    first = NULL;
    last = NULL;
    
    
      
        for (int i = 0; i < SONGS; i++)
         {
         cout<<endl;
         cout<<"Adding song # "<<i+1<<endl;
         cout<<endl;
         cout<<"\nSong title: ";
         std::cin.getline(songTitle, 100);
         std::cin.getline(songTitle, 100);
         cout<<"ID: ";
         cin>>id;
         cout<<"Artist: ";
         std::cin.getline(artist, 100);
         std::cin.getline(artist, 100);; 
         cout<<"Length: ";
         cin>>length;
         cout<<"Size: ";
         cin>>size;
  
         newNode = new mp3Type; // create new node
         newNode->songTitle = songTitle;
         newNode->id = id;
         newNode->artist = artist;
         newNode->length = length;
         newNode->size = size;
         newNode->link = NULL;
    
         if (first == NULL)
         {
            first = newNode;
            last = newNode;
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }
         }
     
}

void deleteSong(mp3Type*& first)
{
     
  mp3Type *last,*current,*trailcurrent;
  bool found;
  int id;

  
  cout<<"Enter song ID that you like to delete"<<endl;
  cout<<endl;
  cin>>id;
    
          for (int i = 0; i < SONGS; i++)
          {   
         if (first->id == id)
         {
           current = first;
           first = first->link;
         }
         if (first == NULL)
         {
            last = NULL;
            
            delete current;
         }
         else
         {
         found = false;
         trailcurrent = first;
         
         current = first->link;
         }   
         }
     
}

void modifySong(mp3Type*& first, mp3Type*& last)
{
     
   int id;
   string songTitle;
   string newsongTitle;
   string newartist;
   string newlength;
   string newsize;
   bool found = false;
   mp3Type *current;
   
   printSong(first);
   
   current = first;
   
   
   cout<<"Enter the id of the song to modify information"<<endl;
   cin>>id;
     
     
   while (current != NULL)
   {         
    if (current->id == id)
    {
     cout<<"Enter the song's new information"<<endl;
     cin>>newsongTitle;
     getline(cin,newsongTitle);
     cin>>newartist;
     getline(cin,newartist);
     cin>>newlength;
     cin>>newsize;
     
     current->songTitle = newsongTitle;
     current->artist = newartist;
     current->length = newlength;
     current->size = newsize;
    } 
   current = current->link; 
    }

}

void printSong(mp3Type*& first)
{
     
     cout<<"Printing song list..."<<endl;
     mp3Type *current;
        
        
        current = new mp3Type;
        current = first;    
        while (current != NULL)
        {
          cout<<endl;
          cout<<current-> songTitle <<endl;
          cout<<current-> id <<endl;
          cout<<current-> artist <<endl;
          cout<<current-> length <<endl;
          cout<<current-> size <<endl;
          current = current->link;
        }
     

}
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.