I have attached the files.
I think, there's a problem with listarr.cpp.
I'm not yet good at c++ terminologies, please take time to download and see the files.
I use dev-c.
aladar04 0 Light Poster
#include <iostream>
#include "listarr.h"
using namespace std;
//--------------------------------------------------------------------
List:: List ( int maxNumber )
// Creates an empty list. Allocates enough memory for maxNumber
// data items (defaults to defMaxListSize).
: maxSize(maxNumber),
size(0),
cursor(-1);
{
dataItems = new DataType[maxSize];
}
//--------------------------------------------------------------------
List:: ~List ()
// Frees the memory used by a list.
{
delete [] dataItems;
}
//--------------------------------------------------------------------
void List:: insert ( const DataType &newDataItem )
throw ( logic_error )
// Inserts newDataItem after the cursor. If the list is empty, then
// newDataItem is inserted as the first (and only) data items in the
// list. In either case, moves the cursor to newDataItem.
{
int j; // Loop counter
// Requires that list is not full
if ( size >= maxSize )
throw logic_error("list is full\n");
for ( j = size ; j > cursor+1 ; j-- )
dataItems[j] = dataItems[j-1];
size++;
dataItems[++cursor] = newDataItem;
}
//--------------------------------------------------------------------
void List:: remove () throw ( logic_error )
// Removes the data items marked by the cursor from a list. Moves the
// cursor to the next data item item in the list. Assumes that the
// first list data items "follows" the last list data item.
{
int j; // Loop counter
// Requires that the list is not empty
if ( size == 0 )
throw logic_error("size == 0\n");
for ( j = cursor ; j < size-1 ; j++ )
dataItems[j] = dataItems[j+1];
size--;
if ( size == 0 )
cursor = -1;
else if ( size == cursor )
cursor = 0;
}
//--------------------------------------------------------------------
void List:: replace ( const DataType &newDataItem )
throw ( logic_error )
// Replaces the item marked by the cursor with newDataItem and
// leaves the cursor at newDataItem.
{
// Requires that the list is not empty
if ( size == 0 )
throw logic_error("size == 0\n");
dataItems[cursor] = newDataItem;
}
//--------------------------------------------------------------------
void List:: clear ()
// Removes all the data items from a list.
{
size = 0;
cursor = -1;
}
//--------------------------------------------------------------------
bool List:: isEmpty () const
// Returns 1 if a list is empty. Otherwise, returns 0.
{
return ( size == 0 );
}
//--------------------------------------------------------------------
bool List:: isFull () const
// Returns 1 if a list is full. Otherwise, returns 0.
{
return ( size == maxSize );
}
//--------------------------------------------------------------------
void List:: gotoBeginning () throw ( logic_error )
// Moves the cursor to the beginning of the list.
{
if ( size == 0 )
throw logic_error("list is empty");
cursor = 0;
}
//--------------------------------------------------------------------
void List:: gotoEnd () throw ( logic_error )
// Moves the cursor to the end of the list.
{
if ( size == 0 )
throw logic_error("list is empty");
cursor = size - 1;
}
//--------------------------------------------------------------------
bool List:: gotoNext () throw ( logic_error )
// If the cursor is not at the end of a list, then moves the
// cursor to the next item in the list and returns true. Otherwise,
// returns false.
{
if ( size == 0 )
throw logic_error("list is empty");
if ( cursor != size-1 )
{
cursor++;
return true;
}
else
return false;
}
//--------------------------------------------------------------------
bool List:: gotoPrior () throw ( logic_error )
// If the cursor is not at the beginning of a list, then moves the
// cursor to the preceeding item in the list and returns true.
// Otherwise, returns false.
{
if ( size == 0 )
throw logic_error("list is empty");
if ( cursor != 0 )
{
cursor--;
return true;
}
else
return false;
}
//--------------------------------------------------------------------
DataType List:: getCursor () const throw ( logic_error )
// Returns the item marked by the cursor.
{
if ( size == 0 )
throw logic_error("list is empty\n"); // Requires that the list is not
empty
return dataItems[cursor];
}
//--------------------------------------------------------------------
void List:: showStructure () const
// Outputs the data items in a list. If the list is empty, outputs
// "Empty list". This operation is intended for testing/debugging
// purposes only.
{
int j; // Loop counter
if ( size == 0 )
cout << "Empty list" << endl;
else
{
cout << "size = " << size
<< " cursor = " << cursor << endl;
for ( j = 0 ; j < maxSize ; j++ )
cout << j << "\t";
cout << endl;
for ( j = 0 ; j < size ; j++ )
cout << dataItems[j] << "\t";
cout << endl;
}
}
#include <iostream>
#include <stdexcept>
using namespace std;
const int defMaxListSize = 10; // Default maximum list size
typedef int DataType;
class List
{
public:
// Constructor
List ( int maxNumber = defMaxListSize ) throw ( bad_alloc );
// Destructor
~List ();
// List manipulation operations
void insert ( const DataType &newDataItem ) // Insert after cursor
throw ( logic_error );
void remove () throw ( logic_error ); // Remove data item
void replace ( const DataType &newDataItem ) // Replace data item
throw ( logic_error );
void clear (); // Clear list
// List status operations
bool isEmpty () const; // List is empty
bool isFull () const; // List is full
// List iteration operations
void gotoBeginning () throw ( logic_error ); // Go to beginning
void gotoEnd () throw ( logic_error ); // Go to end
bool gotoNext () throw ( logic_error ); // Go to next data item
bool gotoPrior () throw ( logic_error ); // Go to prior data item
DataType getCursor () const throw ( logic_error ); // Return data item
// Output the list structure used in testing/debugging
void showStructure () const;
private:
// Data members
int maxSize,
size, // Actual number of data item in the list
cursor; // Cursor array index
DataType *dataItems; // Array containing the list data item
};
// For this example, set DataType to int in listarr.h,
#include <iostream>
#include "listarr.h" // Include the class declaration file
using namespace std;
int main ()
{
List samples(100); // Set of samples
int newSample, // Input sample
total = 0; // Sum of the input samples
// Read in a set of samples from the keyboard.
cout << "Enter list of samples (end with eof) : ";
while ( cin >> newSample )
samples.insert(newSample);
// Sum the samples and output the result.
if ( !samples.isEmpty() ) // Verify that list has data
{
samples.gotoBeginning(); // Go to beginning of list
do
total += samples.getCursor(); // Add element to running sum
while ( samples.gotoNext() ); // Go to next element (if any)
}
cout << "Sum is " << total << endl;
}
cgcgames -1 Junior Poster in Training
hmm well im not sure what the program is trying to do. havent looked hard enough at it. but you have a few decleration errors. you put empty in there and thats not not c++ code and if its ment to be a veriable you didnt declare it.
you also have a few sytax errors that shouldnt be there so you messed somthing up in your class stuff. tbh it all looks a bit messy. thats mostly becuase it didnt open tabed out so its all on the baseline :) making it hard to read.
but what i can gather is the main problem is your throws, im not sure what the problem is but there not working in the code. if i understood more about what the program is ment to be doing maybe i can help a little bit better.
Edited by cgcgames because: n/a
aladar04 0 Light Poster
I hope this helps.
You can implement a list in many ways. Given that all the data items in a list are of the
same type, and that the list structure is linear, an array seems a natural choice. You could
declare the size of the array at compile-time, but your List ADT will be more flexible if you
specify the size of the array at run-time and dynamically allocate the memory required to store
it.
Memory allocation for the array is done by the constructor. The constructor is invoked
whenever a list declaration is encountered during the execution of a program. Once called, the
constructor allocates an array using C++’s new operator. The constructor outlined below, for
example, allocates an array of maxNumber data items and assigns the address of the array to
the pointer dataItems, where dataItems is of type DataType*.
List:: List ( int maxNumber )
{
. . .
dataItems = new DataType[maxNumber];
}
Whenever you allocate memory, you must ensure that it is deallocated when it is no
longer needed. The destructor is used to deallocate the memory storing the array. This function
is invoked whenever a list is destroyed—that is, if the function containing the corresponding list
declaration terminates or if the list is explicitly destroyed by the programmer. The fact that the
call to the destructor is made automatically eliminates the possibility of you forgetting to
deallocate the list. The destructor outlined below frees the memory used by the array that you
allocated above.
List:: ~List ()
{
delete [] dataItems;
}
Another significant implementation issue is what to do when a logbook function such as insert()
or remove() is called with parameters or preconditions that do not meet the stated
requirements. Using the remove() function as an example, it is a logic error to request removal
of an item from an empty list. Although there are many possible ways of dealing with this
situation, the standard C++ method for dealing with bad parameters and other difficult—or
impossible— situations is to throw an exception. Throwing an exception causes the currently
active function to stop execution and return to the calling function. Unless that function or one
of its callers takes special steps to handle the exception, the program will be halted. By using
the C++ try and catch instructions, callers can decide what to do when an exception is thrown.
The try instruction is used when trying something that might cause an exception. The catchinstruction is used to specify what to do if an exception did occur inside the preceding try code
block. Common catch responses to an exception include one or more of the following: 1) print
out a helpful explanation of what went wrong, 2) try to work around the problem, and 3) halt
the program. The empty list problem in the remove() function example can be dealt with by
using the following code snippet at the beginning of the function to enforce the “List is not
empty” requirement.
if ( size == 0 )
throw logic_error(“Remove: list is empty”);
The C++ exception handling mechanism is quite complicated. Note that for the purpose
of demonstrating the usefulness of exceptions, we have included catch statements in the
test.cpp program to handle any exceptions that are thrown. This is the only test program that
uses them heavily because they tend to clutter the code and make it less readable. We decided
not to sacrifice the readability of the rest of our small test programs by adding complete errorhandling
capabilities. However, these capabilities are mandatory in large complex programs.
Step 1: Implement the operations in the List ADT using an array to store the list data
items. Lists change in size; therefore, you need to store the maximum number of data items the
list can hold (maxSize) and the actual number of data items in the list (size), along with the list
data items themselves (dataItems). You also need to keep track of the array index (cursor).
Base your implementation on the following declarations from the file listarr.h.
aladar04 0 Light Poster
continuation....
Step 2: Save your implementation of the List ADT in the file listarr.cpp. Be sure to
document your code.
The following program (test.cpp) uses the array implementation of the operations in the List ADT to read in a list of integer samples and compute their sum.
Fbody 682 Posting Maven Featured Poster
Nice job of spouting off your assignment, but you really haven't given us anything about your expectations for your program or any information about what is/isn't working the way you expected. Did you even bother to read it yourself? Your code is full of throws, but there aren't any try or catch blocks as are required by the exception system, and mentioned in your wall of text. Also, according to your wall, there should be several catch blocks in your test.cpp file, I don't even see 1.
You actually have to try to do your own homework, and throw your arms out when you trip while trying, before we'll catch you.
Edited by Fbody because: n/a
aladar04 0 Light Poster
This is actually not an assignment it's a tutorial.
I'm new to ADT's and creating classes and exceptions?? that's why I posted the "wall of text". I was just looking for someone here who could help me determine the problem and suggest some reference sites I could visit to help me fix the problem. Anyway, your comment helps. Thanks a lot.
cgcgames -1 Junior Poster in Training
only just got up and was able to read through all this and Fbody is right. he pretty much said my first conclution. however i hardly ever use throws in my code. i genrally work around them :).
however you have used them with out any trys or catches which is expected. it even tells you in the error log. you have like 15 errors saying the throw doesnt work and like Fbody said use try and catch statments. jsut type in goodle try and catch statments in C++. that should get you somthing helpful if you havent already done so.
anyway good luck with it. sorry i couldnt be of more help
Edited by cgcgames because: n/a
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.