simple list and driver program help

This is a discussion on simple list and driver program help within the C++ Programming forums, part of the General Programming Boards category; this lil piece of code has been driving me nuts, it's just a simple list that can insert and remove ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    9

    simple list and driver program help

    this lil piece of code has been driving me nuts, it's just a simple list that can insert and remove strings into an array. please someone tell me where a stupid error is becuase i can't find it.

    in case your wondering about the DEL declaration when i tried to set an element of myArray to NULL in teh removeString method it gave me an ambious overload error of =.
    Code:
    #ifndef LIST
    #define LIST
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    const int CAPACITY = 10;
    typedef string ElementType;
    const string DEL = NULL;
    class List
    {
     public:
       List();
       /*----------------------------------------------------------------------
         Construct a List object.
    
         Precondition:  None
         Postcondition: An empty List object has been constructed.
       -----------------------------------------------------------------------*/
    
       bool empty() const;
       /*----------------------------------------------------------------------
         Check if a list is empty.
    
         Precondition:  None
         Postcondition: true is returned if the list is empty, false if not.
       -----------------------------------------------------------------------*/
    
       int sizeOf() const;
       /*----------------------------------------------------------------------
         Find the number of items in a list.
    #include "List.h"
    
    using namespace std;
    
    int main () {
    	List myList;
    	try {
    	myList.insertString("DATA STRUCTURES");
    	cout <<" 1 "<< endl;
    	myList.insertString("CONSTRUCTORS");
    	myList.insertString("DESTURCTORS");
    	myList.insertString("TEMPLATES");
    	myList.insertString("COPY CONSTRUCTOR");
    	myList.insertString("EXPLICIT VALUE CONSTRUCTOR");
    	myList.insertString("MEMORY LEAKS");
    	myList.insertString("A QUICK BROWN FOX");
    	myList.insertString("JUMPS OVER THE LAZY DOG");
    	}
    	catch (string error) {
    		cout << error << endl;
    	}
    	cout << myList;
    	return 0;
    	}
    
         Precondition:  None
         Postcondition: returns the number of items in the list.
       -----------------------------------------------------------------------*/
    
       void insertString(ElementType item) throw (string);
       /*----------------------------------------------------------------------
         Insert an item into the list at a given position.
    
         Precondition:  item is the value to be inserted; there is room in
             the list; pos is >= 0 and <= sizeOf()the list.
         Postcondition: item has been inserted into the list at the position
             determined by pos (provided there is room and pos is a legal
             position).
       -----------------------------------------------------------------------*/
    
       void removeString(ElementType item) throw(string);
       /*----------------------------------------------------------------------
         Remove an item from the list at a given position.
    
         Precondition:  The list is not empty; pos is >= 0 and < sizeOf() the list.
         Postcondition: item at the position determined by pos has been
             removed (provided pos is a legal position).
       ----------------------------------------------------------------------*/
    
    	friend std::ostream & operator << (std::ostream& out, const List & f);
    	/*---------------------------------------------------------------
    	Overloaded Output Operator.
    		
    	Precondition:	The ostream out is open.
    	Postcondition:	The fraction represented by this Fraction
    				object has been placed into out and reference
    				to out is returned.
    	---------------------------------------------------------------*/
    
    
     private:
       int mySize;                     // current size of list stored in myArray
       ElementType myArray[CAPACITY];  // array to store list items
    
    }; //--- end of List class
    
    #endif
    Code:
    #include "List.h"
    
    using namespace std;
    
    List::List(): mySize(0) {}
    
    bool List::empty() const {
       return mySize == 0;
       }
    
    int List::sizeOf() const {
       return mySize;
       }
    
    void List::insertString(ElementType item) throw (string) {
    	cout << "hi" << endl;
       string errorString;
       if (mySize == CAPACITY) {
          errorString = "The List capacity has been reached.  Cannot add new elements.";
          throw errorString;
       }
       int i = 0;
       while (item.size() > myArray[i].size()) {
    	i ++;
    	}
       for(int x = mySize; x > i; x --) {
    	myArray[x+1] = myArray[x];
    	}
       myArray[i] = item;
       mySize++;
    }
    
    void List::removeString(ElementType item) throw(string)
    {
       string errorString;
       if (mySize == 0)
       {
         errorString = "The List is empty.  Cannot remove string.";
         throw errorString;
       }
       int i = 0;
       while (item.size() > myArray[i].size()) {
    	i ++;
    	}
       for(int x = i; x < mySize; x ++) {
           myArray[i] = myArray[i + 1];
    	}
       myArray[mySize] = DEL;
        mySize--;
    }
    
    ostream & operator << (ostream & out, const List & temp)  {
    	if (temp.mySize == 0) {
    		out << "List is empty" << endl;
    		}
    	else {
    		for (int x = 0 ; x < temp.mySize ; x ++) {
    			out << (x + 1) << ")  " << temp.myArray[x] << endl;
    			}
    		}
    	out << endl;
    	return out;
    	}
    Code:
    #include "List.h"
    
    using namespace std;
    
    int main () {
    	List myList;
    	try {
    	myList.insertString("DATA STRUCTURES");
    	cout <<" 1 "<< endl;
    	myList.insertString("CONSTRUCTORS");
    	myList.insertString("DESTURCTORS");
    	myList.insertString("TEMPLATES");
    	myList.insertString("COPY CONSTRUCTOR");
    	myList.insertString("EXPLICIT VALUE CONSTRUCTOR");
    	myList.insertString("MEMORY LEAKS");
    	myList.insertString("A QUICK BROWN FOX");
    	myList.insertString("JUMPS OVER THE LAZY DOG");
    	}
    	catch (string error) {
    		cout << error << endl;
    	}
    	cout << myList;
    	return 0;
    	}
    makefile if you need it
    Code:
    #specify the compiler
    GXX=g++ -g
    
    # Specifiy the target
    all: ListDriver.exe
    
    # Specify the object files that the target depends on
    # Also specify the object files needed to create the executable
    ListDriver.exe: ListDriver.o List.o 
    	$(GXX)  ListDriver.o List.o -o ListDriver.exe
    
    # Specify how the object files should be created from source files
    ListDriver.o: ListDriver.cpp
    	$(GXX)  -c  ListDriver.cpp
    
    List.o: List.cpp
    	$(GXX)  -c  List.cpp
    
    # Specify the object files and executables that are generated
    # and need to be removed to re-compile the whole thing
    clean:
    	rm -f *.o *~ core ListDriver.exe

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    What is the problem?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    9
    when i compile on cygwin i get a core dump and on bash in debian it just aborts

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    For one thing, why do you try and set strings to null? You could just set it to a blank string, or simply ignore the string in question since you are recording the size of the array in use. Also, according to the comments, insertion and removal are done with respect to some position, but it appears that your member functions do not allow for any position to be specified.

    A few other suggestions:

    1. Remove the "using namespace std;" from list.h and fully qualify the names instead.
    2. Rename insertString() to insert(), and removeString() to remove().
    3. Instead of throwing exceptions, you could make it such that an action either succeeds or has no effect. Still, if you really do want to throw exceptions, instead of throwing a string as an exception, throw standard exceptions (or subclass them) such as std::length_error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    What doe sthe following bit of code do?

    Code:
       int i = 0;
       while (item.size() > myArray[i].size())
       {
           i++;
       }
    if i is >= CAPACITY ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using linked list templates
    By Todd88 in forum C++ Programming
    Replies: 6
    Last Post: 11-24-2008, 01:32 PM
  2. Help with doubly linked list
    By Furbiesandbeans in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2008, 10:41 AM
  3. Creating a global linked list?
    By TwistedJester in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 02:23 AM
  4. Linked List confusion
    By airj56 in forum C Programming
    Replies: 4
    Last Post: 09-15-2006, 04:52 AM
  5. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 08:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21