Variable of class member
Apr 27, 2008 at 5:54pm UTC
If I call the addSong function in the main(), the songHead pointer variable changes every single time, but If I give the value like below.
list.addSong("a");
list.addSong("aa");
list.addSong("aaa");
list.addSong("aaaa");
the songHead pointer varialbe doesn't change.
My question is how can I not change the songHead pointer varialbe, if I want to get the values from user.
Thank you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
void main ()
{
songList list;
const int MAX_SIZE = 81;
char songName[MAX_SIZE];
bool check = true ;
while (check)
{
cout << "Enter the song name (to finish enter '~') : " ;
cin.getline(songName,MAX_SIZE);
if (songName[0] == '~' )
{
check = false ;
}
else
{
cout << songName << endl;
list.addSong(songName);
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <iostream>
#include <string>
using namespace ::std;
class songList
{
public :
songList();
~songList();
void addSong(char * songName);
void addReview();
void printSongList() const ;
void printSongListReview() const ;
private :
struct songListStr
{
char * song;
songListStr * review;
songListStr * next;
};
struct reviewListStr
{
char * review;
reviewListStr * next;
};
songListStr * songHead;
reviewListStr * reviewHead;
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
void songList::addSong(char * songName)
{
songListStr * nextSong = new songListStr;
songListStr * preSong = new songListStr;
songListStr * newSong = new songListStr;
songListStr * tempSong = new songListStr;
newSong->next = NULL;
newSong->review = NULL;
newSong->song = songName;
if (songHead == NULL)
{
songHead = newSong;
return ;
}
else if (songHead->next == NULL && strcmp(songName,songHead->song) < 0) //needs to be inserted before the next node
{
newSong->next = songHead;
songHead = newSong;
return ;
}
else
{
tempSong = songHead;
while ( tempSong->next != NULL && strcmp(songName,tempSong->song) > 0 ) //finds a position to be inserted a new node
{
preSong = tempSong;
tempSong = tempSong->next;
nextSong = tempSong;
}
if (tempSong->next == NULL)
{
tempSong->next = newSong;
}
else
{
preSong->next = newSong;
newSong->next = nextSong;
}
}
}
Last edited on Apr 27, 2008 at 7:01pm UTC
Apr 28, 2008 at 2:33pm UTC
What values are being entered by the user?
Your code maintains a sorted list. Consequently, if the user enters values that are essentially sorted in the reverse order, every element will be inserted at the front of the list and therefore the head pointer will change every time.
Note also (although irrelevant to what you are asking) that songList::addSong() has a memory leak in that you don't actually want to create 4 new songListStr elements; you only want to create one.
1 2 3 4
songListStr* nextSong = 0;
songListStr* preSong = 0;
songListStr* tempSong = 0;
songListStr* newSong = new songListStr;
Apr 28, 2008 at 9:20pm UTC
The problem is that you don't create deep copies of the input string, you only copy the char pointer:
newSong->song = songName;
You'll need to change it to
strcpy(newSong->song, songName);
and also change char * song in songListStr to char[MAX_SIZE].
But you'll do yourself a big favour if you use std::string instead of char arrays.
Topic archived. No new replies allowed.