Segmentation Fault

So, for part of our final, we have to achieve a linked list of classes. I am receiving a segmentation fault in part of my program.
Essentially, this is the shell for picking up and dropping weapons in a text-based RPG. One of my functions is "Insert weapon" which essentially puts a weapon into the inventory in its correct alphabetical location. However, I am unsure about why this is giving me a segmentation fault.

I believe the error is located in the "Insert Weapon" function of weaponlist.cpp
I will chop down that file to only include that part.
WEAPON.H
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
46
47
48
49
50
51
52
 
#ifndef WEAPON_H
#define WEAPON_H

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


//////////////////////////////////
// Weapon Class
//////////////////////////////////
class Weapon
{
		friend class WeaponList;
		///////////////////////////
		// Private part of Class
		///////////////////////////
		
		private:
				string weaponName;
				int weaponStr;
				string weaponElement;
				bool weaponExtra;
				Weapon* next;
		///////////////////////////
		// Public part of Class
		///////////////////////////
		
		public :
				//constructors
				Weapon();
				Weapon(string, int, string, bool, Weapon* nextIN = NULL);
				
				//displays
				void display2() const;

				//Getter
				string getName() const;
				int getStr() const;
				string getElement() const;
				bool getExtra() const;

				//Setter
				void setName(string);
				void setStr(int);
				void setElement(string);
				void setExtra(bool);
};

#endif 



WEAPON.CPP
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
 
#include "weapon.h"
///////////////////////////////////////////////////////
// Default Constructor for Weapon. 
// Fills in this data as default when not overloaded.
///////////////////////////////////////////////////////

Weapon::Weapon()
{
	weaponName = "ERROR";
	weaponStr = 0;
	weaponElement = "Neutral";
	weaponExtra = 0;
	next = NULL;
}

////////////////////////////////////////////////////////
// Overloaded Constructor for Weapon.
// Puts the data into the object with the values given.
////////////////////////////////////////////////////////

Weapon::Weapon(string tempName, int tempStr, string tempElement, bool tempExtra, Weapon* nextIN)
{
	weaponName = tempName;
	weaponStr = tempStr;
	weaponElement = tempElement;
	weaponExtra = tempExtra;
	nextIN = NULL;
}

///////////////////////////////////////////////
// Display 2
// Shows an entire weapon in a table.
///////////////////////////////////////////////


void Weapon::display2() const
{
	cout << "*" << setw(11) << weaponName << setw(13) << weaponStr << setw(16) << weaponElement << setw(11) << weaponExtra << setw(9) << "*" << endl
		 << "*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*" << endl;
}
		
		
///////////////////////////////////////////////
// Getters
// Gets the data from whatever it is set to.
///////////////////////////////////////////////

string Weapon::getName() const
{
	return weaponName;
}

int Weapon::getStr() const
{
	return weaponStr;
}

string Weapon::getElement() const
{
	return weaponElement;
}

bool Weapon::getExtra() const
{
	return weaponExtra;
}


///////////////////////////////////////////////////////
// Setters
// Sets the data to whatever you want it to be set to.
///////////////////////////////////////////////////////


void Weapon::setName(string tempName)
{
	weaponName = tempName;
}

void Weapon::setStr(int tempStr)
{
	weaponStr = tempStr;
}

void Weapon::setElement(string tempElement)
{
	weaponElement = tempElement;
}

void Weapon::setExtra(bool tempExtra)
{
	weaponExtra = tempExtra;
}



WEAPONLIST.H
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

#ifndef WEAPONLIST_H
#define WEAPONLIST_H

#include <iostream>
#include <string>
#include "weapon.h"

/////////////////////////////////////////////
// weapon class
// details the new class: weapon
/////////////////////////////////////////////
class WeaponList
{
	////////////////////////////////////////////////////////
	// private
	// uses the private part of the class too start the list
	////////////////////////////////////////////////////////
	private:
		Weapon* start;	
	////////////////////////////////////////////////////////
	// public
	// sets up the public functions
	////////////////////////////////////////////////////////
	public:
		WeaponList();	
		~WeaponList(); 
		void addWeapon(string, int, string, bool);
		void insertWeapon(string, int, string, bool);
		void removeWeapon(string);
		void showWeapons();
};

#endif 


WEAPONLIST.CPP
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
  #include <iostream> 
#include "weaponList.h"
using namespace std;

///////////////////////////////////////////////////////////
// Constructor
// Starts pointer at null.
///////////////////////////////////////////////////////////
WeaponList::WeaponList()
{
	start = NULL;
}
/////////////////////////////////////////////////////////////////////////
// insertWeapon allows you to place a weapon anywhere. This probably
// won't be used in my final product as it isn't very applicable in it.
/////////////////////////////////////////////////////////////////////////
void WeaponList::insertWeapon(string name, int str, string element, bool extra)
{
   Weapon* nodePtr;
   Weapon* previous;

   if (start == NULL || start->weaponName >= name)
   {
      start = new Weapon(name, str, element, extra, start);
   }
   else
   {
      previous = start;
      nodePtr = nodePtr->next;

      while (nodePtr != NULL && nodePtr->weaponName <  name)
      {
         previous = nodePtr;
         nodePtr = nodePtr->next;
      }

      previous->next = new Weapon(name, str, element, extra, nodePtr);
   }
}


WEAPONDRIVER.CPP
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
#include <iostream>
#include <string>
#include "weapon.h"
#include "weaponList.h"
using namespace std;

int main()
{

	WeaponList weaponList;

	cout << "First, we're going to add an axe to the list with the 'Add Weapon Function' and then display it in a table. Press enter when you are done with looking at it." << endl;
	weaponList.addWeapon("Axe", 10, "Neutral", 0);
	weaponList.showWeapons();
	cout << "----------------------------------------------------------------------------------------------" << endl;
	cin.ignore();
	cout << "Now I will add in a sword and a katana with the same function, and show you the new table. Press enter when you are done." << endl;
 	weaponList.addWeapon("Katana", 20, "Ice", 1);
 	weaponList.addWeapon("Sword", 50, "Fire", 1);
 	weaponList.showWeapons();
 	cout << "----------------------------------------------------------------------------------------------" << endl;
 	cin.ignore();
 	cout << "In the next part, I will insert a weapon based on its name, using the 'Insert Weapon Function'. Press enter when you are finished." << endl;
 	weaponList.insertWeapon("Pistol", 5, "Neutral", 0);
 	weaponList.showWeapons();
	cout << "----------------------------------------------------------------------------------------------" << endl;
	cin.ignore();
	cout << "Lastly, I will remove the katana from the list using the 'Remove Weapon Function'." << endl;
 	weaponList.removeWeapon("Katana");
 	weaponList.showWeapons();

	return 0;
}
WEAPON.CPP:28: Maybe you want to assign nextIN to next?

WEAPONLIST.CPP:29: nodePtr isn't initialized. Dereferencing it may in fact crash your process.

Its only a short view on your code. So I'm sorry, but my hint list may be not complete.
I'm not fully sure how to solve the problem, but I came across this page that shows an example of setting up a linked list:

http://www.cplusplus.com/articles/LACRko23/

Within this example, it uses templates. You can ignore this by removing the template and replacing "T" with Weapon; since that is the variable being used.
WEAPONLIST.CPP:28..37:

1
2
3
4
5
6
7
8
9
10
11
12
// PRE: (start != NULL) && (start->weaponName < name)

previous = start;
nodePtr = start->next;

while ((nodePtr != NULL) && (nodePtr->weaponName < name))
{
    previous = nodePtr;
    nodePtr = nodePtr->next;
}

previous->next = new Weapon(name, str, element, extra, nodePtr);
Topic archived. No new replies allowed.