Segmentation Fault

So i'm currently doing an assignment using a linked list and pointers for creating a playlist. For a specific section, we need to create code that changes the position of each song. The specific direction:
"Implement the "Change position of song" menu option. Prompt the user for the current position of the song and the desired new position. Valid new positions are 1 - n (the number of nodes). If the user enters a new position that is less than 1, move the node to the position 1 (the head). If the user enters a new position greater than n, move the node to position n (the tail). 6 cases will be tested."
I have written this much so far but when I run it, there is a segmentation fault that I am unable to recognize to fix. I was wondering where it occurs.
*already declared
PlaylistNode* head = 0;
PlaylistNode* tail = 0;
and PlaylistNode is a class that has the member functions GetNext, SetNext, etc*

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
  if(choice == "c"){
            cout << "CHANGE POSITION OF SONG" << endl;
            cout << "Enter song's current position:" << endl;
            int curPos;
            cin >> curPos; 
            cout << "Enter new position for song:" << endl;
            int newPos;
            cin >> curPos;
            
            PlaylistNode* prevCurrentPosition = 0;
            PlaylistNode* currCurrentPosition = head;
            int counter = 1;
            while(currCurrentPosition != NULL && counter != curPos){
                prevCurrentPosition = currCurrentPosition;
                currCurrentPosition = currCurrentPosition -> GetNext();
                counter++;
            }
            
            PlaylistNode* prevNewPosition = 0;
            PlaylistNode* currNewPosition = head;
            counter = 1;
            while(currNewPosition != NULL && counter != newPos){
                prevNewPosition = currNewPosition;
                currNewPosition = currNewPosition -> GetNext();
                newPos++;
            }
            if(prevCurrentPosition != NULL){
                prevCurrentPosition -> SetNext(currNewPosition);
            }
            else{
                head = currNewPosition;
            }
            if(prevNewPosition != NULL){
                prevNewPosition -> SetNext(currCurrentPosition);
            }
            else{
                head = currCurrentPosition;
            }
            
            PlaylistNode* temp = currCurrentPosition -> GetNext();
            currCurrentPosition -> SetNext(currNewPosition -> GetNext());
            currNewPosition -> SetNext(temp);
            cout << "\"" << currCurrentPosition -> GetSongName() << "\"" << " moved to position " << newPos << endl <<endl;
        }



Thank you!
Line 8:
1
2
            int newPos;
            cin >> curPos;


Line 40 to 43: You use currCurrentPosition without checking whether it is valid. This would happen if head is null or the new position is not found (See line 8).

Note: instead of 0 or NULL better use the nullptr to initialize a pointer.
Topic archived. No new replies allowed.