GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Feb-2007, 03:39
eureka360 eureka360 is offline
New Member
 
Join Date: Feb 2007
Posts: 2
eureka360 is on a distinguished road

game problem using linkedListType.h and random function


hi everyone: im having problem on how to use the header file named linkedListType.h and used it as a game. its a guessing game like(hangman)..make the user guess what's the word by cin a character.. when the character match with one of the char in the word.. then the letter shows up until the word complete otherwise the user only have to make 5 errors to make.. but the program will not stop.. my question is.. how can i do that in the main? how can i generate a random function? if my linkedListType.h goes something like this..

CPP / C++ / C Code:
#ifndef H_LinkedListType
#define H_LinkedListType


//Definition of the node

template<class Type>
struct nodeType
{
       Type info;
       nodeType<Type> *link;
};

template<class Type>
class linkedListType
{
      public:
             const linkedListType<Type>& operator=
             (
                   const linkedListType<Type>&);
             
             //overload the assignment operator
      void initializeList();
      //Initialize the list to an empty state
      //Post: first=NULL, last=NULL     
      
      bool isEmptyList();
      //Function returns teh true if the list is empty;
      //Otherwise, it returns false
      
      bool isfullList();
      //Function returns true if the list is full;
      //Otherwise, it returns false
      
      void print();
      //Output the data contained in each node
      //Post: none
      
      int length();
      //Return the number of elements in the list
      
      void destroyList();
      //Delete all nodes from the list
      //Post: first=NULL, last=NULL
      
      void retrieveFirst (Type& firstElement);
      //Return the data contained in the first node of the list 
      //Post: firstElement=first element of the list
      
      void search (const Type& searchItem);
      //Outputs "Item is found in the list" if searchItem is in 
      //the list; otherwise, outputs "Item is not in the list"
      
      void insertFirst (const Type& searchItem);
      //NewItem is inserted in the list
      //Post: first points to the new list and
      //      newItem is inserted at the beginning of the list
      
      void insertLast(const Type& newItem);
      //NewItem is inserted in the list
      //Post: first points to the new list,
      //      newItem is inserted at teh end of teh list and 
      //      last points to the last node in the list
      
      void deleteNode (const Type& deletItem);
      //If find, the node containging deleteItem is deleted
      //from the list
      //Post: first points to the first node and 
      //      last points to the last node of teh updated list
      
      linkedListType();
      //default constructor 
      //Initializes the list to an empty state
      //Post: first=NULL,last=NULL
      
      linkedListType(const linkedListType<Type>& otherList);
      //copy constructor
      
      ~linkedListType();
      //destructor
      //deletes all nodes from the list 
      //Post:list object is destroyed
      
protected:
      nodeType<Type> *first; //pointer to the first node of the list
      nodeType<Type> *last; //pointer to the last node of the list
      
};

template <class Type>
bool linkedListType<Type>::isEmptyList()
{return (first==NULL);}

template <class Type>
bool linkedListType<Type>::isFullList()
{return false;}

template<class Type>
linkedListType<Type>::linkedListType()	//default constructor
{
  first= NULL;
  last= NULL;
}

template<class Type>
void linkedListType<Type>::destroyList()
{
 nodeType<Type> *temp;                  //pointer to deallocatee the memory
                                        //occupied by the node
 while (first !=NULL)                   //while there are nodes in the list
 {
  temp=first;                           //set temp to the current node
  first= first->link;                   //advance first to the next node
  delete temp;                          //deallocate the memory occupied by temp
 }
last=NULL;       //initialize last to NULL; first has already 
                 //been set to NULL by the while loop
}

template<class Type>
void linkedListType<Type>::initializeList()
{
 destroyList();     //if the list has any nodes, delete them
}

template<class Type>
void linkedListType<Type>::print() 
{
  nodeType<Type> *current;         //pointer to traverse the list
  current= first;                  //set current so that it points to 
                                   // the first node
  while (current!=NULL)            //while there is more data to print
  {
   cout<<current->info<<" ";
   current= current->link;
  }
}//end print

template<class Type>
int linkedListType<Type>::length()                   
{
 int count=0;
 nodeType<Type> *current;       //pointer to traverse the list
 current=first;
 while(current!=NULL)
 {
  count++;
  current= current->link;
 }
 return count;
}//end length

template<class Type>
void linkedListType<Type>::retrieveFirst(Type& firstElement) 
{
 firstElement= first->info;     //copy the info of the first node
} //end retrieveFirst

template<class Type>
void linkedListType<Type>::search(const Type& item)
{
  nodeType<Type> *current;         //pointer to traverse the list
  bool found;
  
  if (first==NULL)                 //list is empty
     cout<<"Cannot search an empty list. "<<endl;
  else
  {
   current=first;                  //set current to point to the first 
                                   //node in the list
   found= false;                   //set found to false
   while (!found&& current !=NULL) //search the list
     if(current->info==item)       //item is found
       found= true;
     else
       current=current->link;      //make current point to   
                                   // the next node
  if (found)
     cout<<"Item is found in the list. "<<endl;
  else
      cout<<"Item is not in the list. "<<endl;
 } //end else
} //end search

template<class Type>
void linkedListType<Type>::insertFirst(const Type& newItem)
{
 nodeType<Type> *newNode;       //pointer to create the new node
 
 newNode= new nodeType<Type>;   //create the new node
 newNode->info= newItem;        //store the new item in the node
 newNode->link=first;           //insert newNode before the first
 first= newNode;                //make first point to the 
                                //actual first node
                                
 if (last ==NULL)               //if the list was empty, newNode is also
                                // the last node in the list
  last=newNode;
}

template<class Type>
void linkedListType<Type>::insertLast(const Type& newItem)
{
 nodeType<Type> *newNode;       //pointer to create the new node
 newNode= new nodeType<Type>;   //create the new node
 newNode->info= newItem;        //store the new item in the node
 newNode->link=NULL;            //set the link field of newNode to NULL
 
 if (first==NULL)               //if the list was empty, newNode is
                                //both the first and last node
 {
  first=newNode;
  last=newNode;
 }
 else     //the list is not empty, insert newNode after last
 {
  last->link=newNode;   //insert newNode after last
  last=newNode;         //make last point to the actual last node
 }
} //end inserLast

template<class Type>
void linkedListType<Type>::deleteNode(const Type& deleteItem)
{
 nodeType<Type> *current;          //pointer to traverse teh list
 nodeType<Type> *trailcurrent;     //pointer just before teh current 
 bool found;
 
 if first ==NULL)                  //case1: list is empty
    cout<<"Cannot delete from an empty list.\n;
 else
 {
  if (first->info==deleteItem)   //case2
  {
   current=first;
   first=first->link;
   if (first==NULL)              //lis had only one node
      last=NULL;
   delete current;
  }
 else  //search the list for the node with the given info
 {
  found=false;
  trailcurrent=first;    //set trailcurrent to point to
                         // the first node
  current= first->link;  //set current to point to the 
                         //second node
  while ((!found)&&(current!=NULL))
  {
   if(current->info!=deleteItem)
   {
    trailcurrent=current;
    current=current->link;
   }
   else
       found=true;
  } //end while
  if (found) //case3: if found, delete the node
  {
   trailcurrent->link= current->link;
  
   if (last==current)                //node to be deleted was
                                    //the last node
     last=trailcurrent;              //update the value of last
   delete current;                   //delete the node from the list
  }
  else
     cout<<"Item to be deleted is not in the list. "<<endl;
 
 } //end else
} //end else
} //end delete node

template<class Type>
linkedListType<Type>::~linkedListType()	//default destructor
{
  nodeType<Type> *temp;  
   while(first!=NULL)    //while there are nodes left in the list
   {
    temp=first;          //set temp to point to the current node
    first= first->link;  //advance first to the next node
    delete temp;         //deallocate memory occupied by temp
   } //end while
   last=NULL;   //initialized last to NULL; first is already NULL
} //end destructor

template<class Type>
linkedListType<Type>::linkedListType(const linkedListType<Type>& otherList)
{
 nodeType<Type>	*newNode;  //pointer to create a node
 nodeType<Type>*current;  //pointer to traverse the list
 if(otherList.first==NULL)          //otherList is empty
 {
  first=NULL;
  last=NULL;
 }
 else
 {
  current=otherList.first;          //current points to the list to be copied
  
    //copy teh first node
  first=new nodeType<Type>;         //create teh node
  first->info=current->info;        //copy the info
  first->link=NULL;                 //set teh link field of 
                                    //the node to NULL
  last=first;                       //make last point to the first node
  current=current->link;            //make current point to the next node
  
    //copy the remaining list
  while(current!=NULL)
  {
   newNode=new nodeType<Type>;      //create a node
   newNode->info=current->info;     //copy the info
   newNode->link=NULL;              //set the link of newNode to NULL
   last->link=newNode;             //attach newNode after last
   last=newNode;                   //make lst point to the actual last node
   current=current->link;          // make current point to the next node
  } //end while
 } //end else
} // end copy constructor

template<class Type>
const linkedListType<Type>& linkedListType<Type>::operator=
                            (const linkedListType<Type>& otherlist)
{
 nodeType<Type>	*newNode;  //pointer to create a node
 nodeType<Type>*current;  //pointer to traverse the list
  if (this != &otherlist) //avoid self-copy
  {
   if (first !=NULL)      //if the list is not empty, destroy the list
      destroyList();
   if (otherList.first== NULL) //otherlist is empty
   {
    first=NULL;
    last=NULL;
   }
   else
   {
    current=otherList.first;   //current points to the list to be copied
    
      //copy the first element
    first=new nodeType<Type>; //create the node
    first->info=current->info;        //copy the info
    first->link = NULL;               //set the link field of the node to NULL
    last=first;                       //make last point to the first node
    current=current->link;            //make current point to the next
                                      //node of the list being copied
      //copy teh remaining list
    while(current!=NULL)
    {
     newNode= new nodeType<Type>;
     newNode->info= current->info;
     newNode->link=NULL;
     last->link=newNode;
     last= newNode;
     current= current->link;
    }//end while
   }//end else
 }// end else
return *this;
}

is this header file really works with the kind of problem i have?
i really dont know where to start.. im having trouble with the string and the char data type.. like a string in an array, make a random function, and with so many function in the header file,which one will i declare first in the main first.. please help me..
  #2  
Old 14-Feb-2007, 14:53
killzone killzone is offline
Junior Member
 
Join Date: Nov 2006
Posts: 66
killzone is an unknown quantity at this point

Re: game problem using linkedListType.h and random function


You have a slight error in the file
This
CPP / C++ / C Code:
if first ==NULL)                  //case1: list is empty
    cout<<"Cannot delete from an empty list.\n;

needs to be changed to
CPP / C++ / C Code:
if (first == NULL){
     cout<<"Cannot Delete from empty list";
}

Maybe that solves your problem

It's also good programming practice to seperate your functions and the declarations into different files
such as

Code:
Header : linkedListType.h Functions : linkedListType.cpp

Then in the linkedListType.h | insert an #Include to "linkedListType.cpp"
Do the same for the linkedListType.cpp. It's also good programming practice to add #ifdef to it so you dont end up including the same file twice
So then in your main file all you have to do is add one of them preferably the .h one.

That solves part of your problem

...Now for the other part...Hmm
  #3  
Old 14-Feb-2007, 23:40
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: game problem using linkedListType.h and random function


Quote:
Originally Posted by killzone
Then in the linkedListType.h | insert an #Include to "linkedListType.cpp"
Do the same for the linkedListType.cpp. It's also good programming practice to add #ifdef to it so you dont end up including the same file twice
So then in your main file all you have to do is add one of them preferably the .h one.
Actually including a CPP file into an H file is an extremely bad practice. H files should not have executable code, and they should be included in CPP files, not the other way around.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #4  
Old 15-Feb-2007, 10:36
killzone killzone is offline
Junior Member
 
Join Date: Nov 2006
Posts: 66
killzone is an unknown quantity at this point

Re: game problem using linkedListType.h and random function


Oh yeah
I forgot [/cough]
  #5  
Old 15-Feb-2007, 19:22
pRiscilla_pEezY pRiscilla_pEezY is offline
New Member
 
Join Date: Feb 2007
Posts: 1
pRiscilla_pEezY is on a distinguished road

Re: game problem using linkedListType.h and random function


using the same header file, how will you create a game? uhmmm like hangman?...
i got some questions in making up the game...
in an array library of words, how can i get access to those words and separating the characters of the words?... and then how can i put it in the linked list?... gawd im having a hard time...
  #6  
Old 16-Feb-2007, 10:48
killzone killzone is offline
Junior Member
 
Join Date: Nov 2006
Posts: 66
killzone is an unknown quantity at this point

Re: game problem using linkedListType.h and random function


Maybe save it to a file
so then the user can edit the words
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
tricky random gen problem corruptjedi C++ Forum 2 06-Feb-2007 13:37
random access file read problem wmmccoy0910 C Programming Language 13 19-Aug-2006 02:02
Help on Basic Design C++ maliquenavidad C++ Forum 4 28-Nov-2005 21:24
Nested for loop with function Tori C++ Forum 11 08-Nov-2004 13:02
urgent needs :apache + random function jack Apache Web Server Forum 0 19-Jan-2004 17:41

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:09.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.