hello all.
Currently i am "trying" to create a linked list, but alas have failed, i have the insert node peice working and can add elements into the list.
The thing is i cant seem 2 delete these nodes, i'm tring to have a function where u can move through the emlements of the list and delete them at point.
Also i am trying to modify the values of the objects once created.
I have been working on this for about 30 hours and am really stuck.
I have read numerous tutorials but failed at every attepmt.
If you could please look at the code and tell me where i can acheive the above and how i could do it.
Pleas help me as i have really given this my best shot and think i would understand link lists alot better with the code.
Thankz very much.
here is main.cpp:
/*//////////////////////////////////////////////////////////////////////////////////
*********************************
purpose: Main.
*********************************
*///////////////////////////////////////////////////////////////////////////////////
//
////Header Files://////////////////////////////////////////////////////////////////
#include <iostream>
#include "Soldier.h"
#include "Node.h"
#include <string>
#include <stdlib.h>
#include "windows.h"
using namespace std;
void award_Sack();
//////////////////////////////////////////////////////////////////////////////////
//Method:
// Main Menu branch
//Description:
// After the heading loads, you are presendented with a choice, 1 to recurite team members
// and 2 to sack team members, this is the main part of the program and all other
// functions are dervived and accessed through the main function.
//////////////////////////////////////////
int main()
{
start:
cout << "*********************************************************************" << "\n";
cout << " ****************************************" << "\n";
cout << " *******************" << "\n";
cout << " *******" << "\n";
cout << "\t \t \t\ Welcome.. " << "\n"<< "\n";
cout << "*/\* Welcome To The Soldier Clan Manager. */\*" << "\n";
cout << "*********************************************************************" << "\n";
cout << "\n";
system("Color 81");
cout << "What Would You Like to Do?\n\n";
cout << "Press 1 to recurite a team member:\n\n";
cout << "Press 2 to Distribute Awards and Sack team members:\n\n";
cout << "Press 3 to exit this program\n\n";
cout << "Please make a selection: ";
int choice;
cin >> choice;
switch(choice){ // Switch statement to receive order.
case 1:
cout << "You have choosen to recruite a team member." << "\n";
cout << "Launching Request";
Sleep(700);
cout << ".......";
Sleep(500);
cout << ".......";
Sleep(300);
cout << ".....Done!" << "\n";
system("cls");
goto recurite;
// please note that i couldnt place this inside a function because it kept comming up with with a
//"non static error", and i couldnt fix it so this was the last restort.
// The reson for the static error is that i wasnt creating a new object, because an object shouldnt be created ;/
break;
case 2:
cout << "You have choosen to Awards and Sacking." << "\n";
cout << "Launching Request";
Sleep(700);
cout << ".......";
Sleep(500);
cout << ".......";
Sleep(500);
cout << ".....Done!" << "\n";
break;
case 3:
cout << "You have choosen to Exit." << "\n";
cout << "Launching Request";
Sleep(700);
cout << ".......";
Sleep(500);
cout << ".......";
Sleep(300);
cout << ".....Done!" << "\n";
return(1);
break;
default:
cout << "Please make a selection from the menu.\n";
putchar (7); //makes a beeping sound to reiterate a wrong button has been pressed
//goto start; //goto the start of the switch statement and get users input again.
}
recurite:
Node *pNode = 0; // create a pointer and initalize it to 0, or NULL
Soldier * pSoldier = new Soldier(); // initalizes pSoldier as pointer and stores enough memory for soldier to be created
//////////////////////////////////////////////////////////////////////////////////
//Method:
// Create tempory varibles that store users input and then pass these on to thier corrosponding functions
//Description:
// The current soldier that is being created needs to have its member data initalized
// to do this we need to create temporary local varibles that hold the users input and pass them
// onto thier corresponding functions in the soldier class
//////////////////////////////////////////
short int Kills = 0; // creates a short int for players kills
unsigned int Cash = 0;
string Name; // string for NAme Name
string EmailAddress; // string for email address
//////////////////////////////////////////////////////////////////////////////////
//Method:
// This is the create soldier part, all soldiers are initalized here and passed onto the Node and created as
// a linked list.
//Description:
// The user gets asked how many soldiers that they want to add this month, and a series of questions
// are displayed so that the user can enter in thier corresponding data.
//////////////////////////////////////////
Node *pHead = new Node(pSoldier); // creates a new Node and stores enough memory on the heap
system("cls"); // clears the screen, and clears the constructor message, that appears when creating pHead
cout << "*********************************************************************" << "\n";
cout << " ****************************************" << "\n";
cout << " *******************" << "\n";
cout << " *******" << "\n";
cout << "\t \t \t\ Welcome.. " << "\n"<< "\n";
cout << "*/\* Welcome To The Soldier Recurite Screen */\*" << "\n";
cout << "*********************************************************************" << "\n";
int choicer = 1; // initalize choicer to 1, *i find its always good, and makes
// it easier to understand when you decalre something
// always initalize it to something, whether it be NULL or something else.
while(choicer == 1) // while the user still wants to recurite team members.
{
cout << "How many soldiers do you want to create?: "; // simple question that initalizes how many soldier are to be created
// this number is then passed along to the for loop and repeats
// the process for as many soldiers that they wanted to create.
cin >> choicer; // stores the users choice for amount of soldiers to recurite.
for(int counter = 1; counter <= choicer; counter ++) // a for loop that repeats the process how ever many times choicer has been initalized to
{
//////// Soldiers Input://////////
//Soldiers Name
cout << "\nPlease Input Soldier Number " << counter << "'s Nickname: "; // counter is used to display what number the current soldier is.
cin >> Name; // stores Name in the tempory varible
cin.ignore(50,'\n'); // allows for a maximum of fifty characters to be entered and the space bar to be used, without stuffing up
// space bar is in a dos application the same as a return "\n", so if the user enters a space,without this
// then what ever is after the space gets passed on to the next varible, thus resulting
// in total chaos!!!!
pSoldier->SetNickame(Name); // Sets the current soldiers NickName, by passing on the tempory Name varible to it.
///////
//Soldiers Email Address
cout << "Please Input " << Name << "'s EmailAddress: "; //input soldiers email address, also the tempory varibles created
// come into use here as we actually state the name of the soldiers's email address
// to be created
// this greatly improves the user friendlyness. :/
cin >> EmailAddress; // stores in the temporary local varible
pSoldier->setEmailAddress(EmailAddress); // passes the local varible to be initalized as the soldiers email address.
///////
//Soldiers TotalAmount of Kils
cout << "Please Input " << Name << "'s Kills: "; // input soldiers kills question, temporary varible comes into play once again.
cin >> Kills; // Stores users input in the temporary varible.
pSoldier->SetKills(Kills); // passes temorary varible onto the soldiers setKills function.
//////
//Soldiers Rank:
pSoldier->setRank(Kills); //passes soldiers total kills onto setRankFunction
//////
//Solsiers Cash:
// If a soldier has already been given money.
// Ie the team paid the soldier to join thier team
cout << "Has " << Name << " been paid any money to join clan?\n";
cout << "If no money has been paid, please inset 0.\n";
cout << "If money has been paid then please enter in the amount: $";
cin >> Cash;
pSoldier->setCash(Cash);
//////Soldier creation in the list.
pSoldier = new Soldier(); // initalizes the pointer pSoldier's memory capacity
// to be able to store enough memory from the heap
// to accomadate a new Soldier object.
pNode = new Node(pSoldier); // passes the pointer soldier onto the Node
pHead->Insert(pNode);
system("CLS");
pHead->Display();
} // End of the for loop.
cout << "Do you wish to insert more soldiers?\n"; // Asks user if they want to insert any more soldiers
cout << " Press 1 to enter more soldiers.\n";
cout << " Press 2 to return to the main menu.\n";
cin >> choicer; //inputs users choice, and if choice ==1 then the loop gos again
// if two then the loop ends and code below gets complied.
system("CLS");
goto start; //gotos start
}
system("CLS");
pHead->Display(); //Display Soldiers that have been created.
delete pHead; // When program is finished then free up memory used and displays the deconstructor text
cout << "Exiting";
Sleep(700);
cout << ".......";
Sleep(500);
cout << ".......";
Sleep(500);
cout << ".....Done!" << "\n";
}
here is Node.cpp:
/*//////////////////////////////////////////////////////////////////////////////////
*********************************
purpose: Main.
*********************************
*///////////////////////////////////////////////////////////////////////////////////
//
////Header Files://////////////////////////////////////////////////////////////////
#include <iostream>
#include "Soldier.h"
#include "Node.h"
using namespace std; //saves us from typing std::x where x is the function, good idea to use this is theres many instances where an std function is requried
///////////////////////////////////////////////////////////////////////////////////
//Method:
// Constructor: & Deconstructor
//Description:
// Everytime an instance of the soldier object is created then the constrcutor is called,
// everytime an object is destroyed then the deconstructor is hailed.
//////////////////////////////////////////
Node::Node(Soldier * pSoldier):itsSoldier(pSoldier),itsNext(0)
{
}
Node::~Node()
{
cout << "Deleting Node..." << endl;
delete itsSoldier;
itsSoldier = 0;
delete itsNext;
itsNext = 0;
}
///////////////////////////////////////////////////////////////////////////////////
//Method:
// Insert a new Node;
//Description:
// This is the code that inserts the soldier object into the list,
// Also the code to sort the soldiers by rank is in here aswell.
// I was originally going to write a bubble sort function to sort the
// soldiers by rank but upon closer examination of some of the homework questions.
// it became evident that you can arrange them wheb insertig them.,
// We get the next soldiers Kills, the current soldiers kills and the new soldiers kills
// and we compare them and insert the new node in thier corresponding order.
//////////////////////////////////////////
void Node::Insert(Node * newNode) //insert function, where the pointerNewNode is passed as a parramater.
{
// If there is no elements in the list then:
if(!itsNext)
{
itsNext = newNode; // the reason we need this peice of code is that because when we
// are placeing an object in the list we want to insert it in order
// and as you can see below we initalize a few temporary varibles
// that go and get other soldiers kills.
// if there is no soldiers and these local varibles are initalized
// then the program will crash because the data doesnt exist.
// so by using this simple if statement we can check if there is a next node
// and if there isnt then create one, and omit the sorting code below
// because this will be the first node.
}
else // If this new node isnt the first then:
{
//Sorting Code local varibles;
int NextSoldiersKills = itsNext->GetSoldier()->GetKills(); // Stores the next soldiers kills
int NewKills = newNode->GetSoldier()->GetKills(); // gets the current soldiers kills
int ThisNodeKills = itsSoldier->GetKills(); // stroes the new soldiers kills
// The above local varibles are initalized so that they may go into the for loop below,
// This will insert the soldier in a sorted position and there is no need for any bubble sort functions.
// in the end this saves alot of time, you think a little at the start and...save alot of time ;).
if(NewKills > ThisNodeKills && NewKills < NextSoldiersKills) // if the current soldiers kills, is greater than the new soldiers kills and
// the current soldiers kills is lower than the next soldiers kills then:
{
newNode->setNext(itsNext); // set the newnode to next
itsNext = newNode; // initalize its next to newNode;
}
else
{
itsNext->Insert(newNode); //If the soldier doesnt need to be ranked and is allready in order then:
// insert the object into the current position
}
}//end if
}
///////////////////////////////////////////////////////////////////////////////////
//Method:
// Display Link List;
//Description:
// This function is used to display all objects in the linked list
// This function is the one that we use to see that all our hard work and
// effort has paid off.
///////////////////////////////////////////////////////////////////////////////////
void Node::Display() //Display function is from Node class so instance it.
{
if(itsSoldier->GetKills() > 0 && GetNext()) // stops the pHead from showing and gets the nextSoldier
{
cout << "**************************************\n";
cout << "Soldiers Nickname: " << itsSoldier->GetNickname() << "\n";
cout << "Soldiers Email Adddress: " << itsSoldier->getEmailAddress() << "\n";
cout << "Soldiers Total Kills: " << itsSoldier->GetKills() << "\n";
cout << "Soldiers Cash Balance: " << "$" << itsSoldier->getCash() << "\n";
cout << "Soldiers Rank: " << itsSoldier->getRank() << "\n";
cout << "**************************************\n\n";
}
if(itsNext)
{
itsNext->Display();
}
}
Node * Node::GetNext() const
{
return(Node::itsNext);
}
Soldier * Node::GetSoldier() const
{
return(Node::itsSoldier);
}
void Node::setNext(Node *node)
{
Node::itsNext = node;
}
here is soldier.cpp:
/*//////////////////////////////////////////////////////////////////////////////////
*********************************
purpose: Node Header File.
*********************************
*///////////////////////////////////////////////////////////////////////////////////
//Method:
// Header Files:
///////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include "Soldier.h"
#include "Node.h"
#include <stdlib.h>
#include "windows.h"
#include <string>
using namespace std;
///////////////////////////////////////////////////////////////////////////////////
//Method:
// Definition for Soldier Class
//////////////////////////////////////////////////////////////////////////////////
//Method:
// Get And Set Solders Kills
//////////////////////////////////////////
short int Soldier::GetKills() const
{
return(m_itsKills);
}
void Soldier::SetKills(short int Kills)
{
Soldier::m_itsKills = Kills;
}
//////////////////////////////////////////////////////////////////////////////////
//Method:
// Get And Set Solders NickName
//////////////////////////////////////////
std::string Soldier::GetNickname() const // const so it doesnt change
{
return(m_itsName); // returns the name
}
void Soldier::SetNickame(std::string Name)
{
m_itsName = Name; // initalize Soldier class's nickname with the Name parameter passed on
}
//////////////////////////////////////////////////////////////////////////////////
//Method:
// Get And Set Solders Email Adddress
//////////////////////////////////////////
std::string Soldier::getEmailAddress() const
{
return(m_emailAddress);
}
void Soldier::setEmailAddress(std::string eAddress)
{
m_emailAddress = eAddress;
}
//////////////////////////////////////////////////////////////////////////////////
//Method:
// Get And Set Solders Money
//////////////////////////////////////////
short int Soldier::getCash() const
{
return(m_Cash);
}
void Soldier::setCash(unsigned int przMoney)
{
m_Cash = przMoney;
}
//////////////////////////////////////////////////////////////////////////////////
//Method:
// Get And Set Solders Rank
//////////////////////////////////////////
std::string Soldier::getRank() const
{
return(m_Rank);
}
void Soldier::setRank(int totalKills)
{
if (totalKills >= 1000)
{
m_Rank = "General";
}
else if (totalKills >= 500)
{
m_Rank = "Captain";
}
else if (totalKills >= 100)
{
m_Rank = "Soldier";
}
else
{
m_Rank = "Trainee";
}
}
//////////////////////////////////////////////////////////////////////////////////
//Method:
// Construcot & Deconstructor for Soldier Class
//////////////////////////////////////////
Soldier::Soldier()
{
cout << "A soldier has been created.\n\n";
}
Soldier::~Soldier()
{
cout << "A soldier has been deleted.\n\n" << endl;
}
void Soldier::Sack_Award()
{
system("cls"); // clears the screen, and clears the constructor message, that appears when creating pHead
cout << "*********************************************************************" << "\n";
cout << " ****************************************" << "\n";
cout << " *******************" << "\n";
cout << " *******" << "\n";
cout << "\t \t \t\ Welcome.. " << "\n"<< "\n";
cout << "*/\* Welcome To The Awards and Sacking Menu */\*" << "\n";
cout << "*********************************************************************" << "\n";
int decision;
cout << "Do you want to distribute awards or Sack team Members?\n\n";
cout << "Please press 1 to Distribute awards.\n\n";
cout << "Please press 2 to Sack team member(s).\n\n";
cout << "Press 3 to return to main menu.\n";
cin >> decision;
switch (decision)
{
case 1:
cout << "You have choosen to Distribute awards.\n";
cout << "Launching Request";
Sleep(700);
cout << ".......";
Sleep(500);
cout << ".......";
Sleep(300);
cout << ".....Done!" << "\n";
break;
case 2:
break;
case 3:
break;
default:
cout << "Please make a selection from the menu.\n";
putchar (7); //makes a beeping sound to reiterate a wrong button has been pressed
system("cls");
}
}
here is Node.h:
/*//////////////////////////////////////////////////////////////////////////////////
*********************************
purpose: Node Header File.
*********************************
*///////////////////////////////////////////////////////////////////////////////////
//Method:
// Class Node set up.
//Description:
// here is the class for the Node definition, all functions are prototyped here
// and two private varbibles are created.
//////////////////////////////////////////
class Node
{
public:
//Constructor & Deconstructor::
Node(Soldier*); // constructor that passes a pointer to Soldier as a parameter
~Node(); // deconstruct0r
// member functions
void setNext(Node * node); // sets the next node along the list, accepts a pointer to node as a parameter
Node * GetNext()const; // gets the next soldier
Soldier* GetSoldier()const; // initalizes the getSoldier function to the pointer of soldier
void Insert(Node *); // insert function where a pointer to Node is accepted as a parramtrer
void Display(); // the display function
////
private:
Soldier *itsSoldier; // pointer to currentsoldier
Node * itsNext; // pointer to next soldier
};
///////////////////////////////////////////////////////////////////////////////////
here is soldier.h:
/*//////////////////////////////////////////////////////////////////////////////////
*********************************
purpose: Node Header File.
*********************************
*///////////////////////////////////////////////////////////////////////////////////
//
////Header Files://////////////////////////////////////////////////////////////////
#include <string>
///////////////////////////////////////////////////////////////////////////////////
////Class Soldier///////////////////////////////////////////////////////////////////
class Soldier
{
public:
//////// Constructor & Deconstructor
Soldier(); // constructor
~Soldier(); // deconstructor
////////Mutator & accessor for Set & get Kills:
void SetKills(short int Kills);
short int GetKills() const;
///////Mutator & accessor for Set & Get Name
void SetNickame(std::string Name);
std::string GetNickname() const;
///////Mutator & accessor for Set & Get Email Address
void setEmailAddress(std::string eAddress);
std::string getEmailAddress() const;
///////Mutator & accessor for Set & Get Money
void setCash(unsigned int przMoney);
short int getCash() const;
///////Mutator for Set & Get Rank
void setRank(int totalKills);
std::string getRank() const;
/////RecuriteSoldierFunction
void recuriteSoldier();
///// Sack&Award
void Sack_Award();
////Temporary Member data Accessor & Mutator
private:
//////Private Member Data:
short int m_itsKills; // current soldiers kills
std::string m_itsName; // current soldiers NickName
std::string m_emailAddress; // current soldiers email address
std::string m_Rank; // gets players rank
short int m_Cash; // cash current player has.
};
If you could pleas help me finish this code i would be really thankful
becuase its killn me.
I cant seem 2 go forward past this point.
thankz