LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-23-2006, 11:20 PM   #1
TurtleFace
Member
 
Registered: Jul 2005
Distribution: Mandrake LE 2005
Posts: 51

Rep: Reputation: 15
simple list and driver program


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
 
Old 10-24-2006, 12:37 AM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
Quote:
please someone tell me where a stupid error is becuase i can't find it.
without looking at the code, whats the error when you compile it?
 
Old 10-24-2006, 12:44 AM   #3
TurtleFace
Member
 
Registered: Jul 2005
Distribution: Mandrake LE 2005
Posts: 51

Original Poster
Rep: Reputation: 15
when compiled on cygwin i get errors like
Code:
8 [main] ListDriver 468 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)
Segmentation fault (core dumped)
and on a bash shell in debian it jsut aborts
 
Old 10-24-2006, 09:33 AM   #4
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
Compiled fine on my linux system. Gave the following output when running:

Code:
aconole@orgcandman:~/cvs/cpptest> ./ListDriver.exe
terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_S_construct NULL not valid
Aborted
g++ version:
Code:
aconole@orgcandman:~/cvs/cpptest> g++ -v
Using built-in specs.
Target: i586-suse-linux
Configured with: ../configure --enable-threads=posix --prefix=/usr --with-local-prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib --libexecdir=/usr/lib --enable-languages=c,c++,objc,f95,java,ada --disable-checking --with-gxx-include-dir=/usr/include/c++/4.0.2 --enable-java-awt=gtk --disable-libjava-multilib --with-slibdir=/lib --with-system-zlib --enable-shared --enable-__cxa_atexit --without-system-libunwind --host=i586-suse-linux
Thread model: posix
gcc version 4.0.2 20050901 (prerelease) (SUSE Linux)
 
Old 10-24-2006, 12:07 PM   #5
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
instead of setting strings to NULL (DEL), cant you just set them to ""?

Last edited by nadroj; 10-24-2006 at 12:08 PM.
 
Old 10-24-2006, 12:40 PM   #6
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
I have a few more insights.

Firstly, why are you doing the following loop?

Code:
   int i = 0;
   while (item.size() > myArray[i].size()) {
	i ++;
	}
Also, you can't set myArray[i] = 0, since they're not pointers but actual objects.

Are you trying to have a fifo or lifo list?

My guess is fifo, in which case, you could just do it as a linked list. If you really want to go the array route, you can keep 2 integers, or an integer and a pointer.

have one be the tracking of where the insertion is going on. Have another track where the deletion is going on.

Optimize from there.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Program to automatically list on eBay Oxagast Programming 1 09-09-2006 03:31 PM
How to rm a list of files returned from another program? neo_in_matrix Linux - Newbie 2 04-12-2005 11:24 PM
need simple program to manage a mailing list linuxlimbo Linux - Software 0 12-21-2004 05:30 AM
Help!How can I get the file name list in C program? wuzhong Programming 6 09-22-2004 11:56 AM
Simple link list problem karlan Programming 4 01-22-2004 01:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration