CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2009
    Posts
    9

    Class Troubles...

    Okay so the deal is, our instructor wrote what the class is supposed to look like (will be included with the code) and also gave us the Input, Preconditions, Process, Output, and Postcondtions of each function that is supposed to be used. I'm having trouble with an insert, delete, and clear functions dealing with the array. There is no USER input, we're just supposed to test the functions within the main function. Here's the code I've written thus far. Any help would be much appreciated!! Thanks for your time.

    Code:
    #include <iostream>
    using namespace std;
    #define DataType int
    #define Boolean bool
    #define ARRAYSIZE 0
    
    class SeqList
    {
    private:
    	int size;
    	int listitem[ARRAYSIZE];
    	
    	
    public:
    	SeqList(void);
    
    	int ListSize(void)const;
    	Boolean ListEmpty(void) const;
    	Boolean Find(DataType& item) const;
    	DataType GetData(int pos) const;
    
    	void Insert(const DataType& item);
    	void Delete(const DataType& item);
    	DataType DeleteFront(void);
    	void ClearList(void);
    };
    
    SeqList::SeqList()
    {
    	size = 0;
    }
    
    int SeqList::ListSize() const
    {
    	return size;
    }
    
    Boolean SeqList::ListEmpty() const
    {
    	if(size == 0)
    		return true;
    	else
    		return false;
    }
    
    Boolean SeqList::Find(DataType& item) const
    {
    if(item == listitem[size])
    {
    return true;
    }
    else 
    return false;
    }
    
    void SeqList::Insert(const int& item)//Input: Item to insert in the list, no preconditions, Process: //Add the item at the rear of the list., No output, Postconditions: The list has a new item; its size //is increased by one
    {
    	size++;
    	listitem[size] = item;
    			
    }
    DataType SeqList::GetData(int pos) const
    {
    	if(pos >= size) 
    		abort();
    	else
    		return pos;
    }
    void SeqList::Delete(const int& item)//Input: Value to delete from the list, No preconditions, 
    //Process: Scan the list and remove the first occurrence of the item in the list. Take no action if //the item is not in the list, no output, Post conditions: If a match occurs, the list has one fewer //items
    {
    	if(item != listitem[size])
    	{
    		abort();
    	}
    	else
    		for(int i = 0; i < size; i++)
    		{
    			listitem[size]=listitem[size-1];
    		}
    
    }
    
    //DataType DeleteFront(void)//No input, Preconditions: List must not be empty, Process: //Remove the first item  from the list, output: Return the value of the item that is removed
    //Post conditions: The list has one fewer items
    //{
    //**Code for delete only with the index listitem[0]
    //}
    void ClearList(void)//No input, no preconditions, process: Remove all elements from the list and //set the list size to 0
    {
    
    }
    int main()
    {
    	SeqList A;
    	A.Insert(12);
    	
    	cout << A.ListSize()<<endl;
    	if(A.ListEmpty()) cout<< "The List Is Empty" << endl;
    	else 
    		cout<< "The List Is Not Empty" << endl;
    
    	return 0;
    }
    Each time I try to write the delete function, I get a runtime error. Stack around A is corrupt. Not sure what to do.

    SA

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Class Troubles...

    I'm not really sure why you are trying to increase the size of a fixed size array. Your instructor should have told you that arrays are of a fixed size.. did they say you were supposed to use dynamic arrays? Also, I get a warning with my compiler:
    Code:
    warning C4200: nonstandard extension used : zero-sized array in struct/union	main.cpp	11
    That should have alerted you to the fact that your array is of size 0 and hence any attempts at accessing its non-existant elements will be undefined behaviour.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured