Thread: Circular array list

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    81

    Circular array list

    Hi everyone!

    I would like to ask for some ideas for the following code. It works well in general, but I doesn't work all the time. I have just been working on it for so long that I am out of ideas.

    Basically it removes the elements from the array without any problems until I play with it too much, then it just takes my remove command and says that it has removed remove! And then everything falls apart.

    Tee hee, I am a beginner, so please don't laugh to much at my horrible coding ways I am sure no one will like the last line of code, but it was the only way I could make this thing work.. I am certain there are better ways to do this.

    I also am wondering how I would go about removing the array completely. I can erase the elements inside the array, but the array is still there, with no elements inside...

    I thank you all for your time in advance.

    Code:
    void CircleList::remove(string remove)		// Returns Name that will be removed from the List
    {
    	int j = 0;
    	int loops=1;				// To count number of loops
    	int shift=1;				// To count how many to shift in array
    
    	if (isEmpty())				// If no Players in List
    	{
    		cout << "There are no players to remove.";
    	}
    
    	for (int i=last_player; i <= 10; i++)	// Checking from current player to end of array
    	{
    		if (p[i].name==remove)				// If a match
    		{
    			temp[i].name = p[i].name;		// Put the name in temporary array
    			loops=i;
    			shift = loops - count;			// Count the number of shifts to make
    			break;							// Get out of loop in case of double entry
    		}
    	}
    
    	for (i=last_player; i >= 0; i--)		// Checking from current player to beggining of array
    	{
    		if (p[i].name==remove)				// If a match
    		{
    			temp[i].name = p[i].name;		// Put name in temporary array
    			loops=i;
    			shift = count - loops;			// Count the number of shifts to make
    			break;							// Get out of loop in case of double entry
    		}
    	}
    
    		cout << p[i].name << " has been deleted." << endl;
    
    	while (j !=shift)						// While there are still shifts to do
    	{
    		p[i].name=p[i+1].name;				// Shift up
    		i++;
    		j++;
    	}
    
    	count=count-1;							// Decrease the number of players by 1
    
    	for (i=0; i<count; i++)					// As long as there are players
    	{
    		temp[i].name=p[i].name;				// Put them in temporary array
    	}
    
    	p[i].name=temp[i].name;					// Switch temporary array to array
    
    	last_player = last_player-1;			// This is the new current player
    
    	if (last_player == -1)					// If last player "points" below 0
    	{
    		last_player = count -1 ;			// Bring it back to last element in array
    	}
    
    	p[10].name="";							// Ensures nothing is in index 10
    }
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    Can't anyone give me any advice?

    I know I'm bumping up my post, but I'm not asking for anyone to write any code for me.

    How the hell am I supposed to get better when I submit my stuff and get an A because it works, when I KNOW it's not right to begin with?
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Hard to say without you post more information, like what is actually in the class.

    > p[10].name="";
    If all you have is
    string p[10];
    in your class, then several places in the code you have posted step off the end of your array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    Wow, first I want to say I am sorry if I sounded bitter. It was not my intention.

    SALEM, that is what I have in my class. I was under the impression that if you declared the array like that, it wouldn't pass 10 players. I know that it steps off in my for loops, but I don't know how else to search for the name of the player to remove.

    Code:
    class CircleList
    {
    	public:
    		CircleList();				// Constructor, default
    		~CircleList();				// Destructor
    
    		void add(string name);		// Adds name to List
    		void play();				// Plays one player at a time
    		void playround();			// Plays all the players in List
    		void remove(string name);	// Removes name from List
    		void quit();				// Ends program
    
    		void PlayGame();			// Interface function
    		
    		bool isEmpty(){return count==0;}	// Returns count = 0
    		bool isFull(){return count==10;}	// Returns count = 10
    
    	private:
    
    		void incrementLastPlayer();	
    
    
    		Player p[10];
    		Player temp[10];
    		int last_player;		// To keep track of the last player. Like a pointer
    		int count;				// To count how many players
    
    
    
    
    class CircleList;			// Forward declaration
    
    class Player
    {
    	private:
    
    		friend CircleList;	// Make CircleList a friend
    		
    		Player(){name="";}	// Constructor, creates name
    		~Player(){}			// Destructor
    
    		string name;		// String declaration to store the name of the player
    };
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do i reverse a circular double linked list
    By kobra_swe in forum C Programming
    Replies: 13
    Last Post: 04-08-2008, 04:20 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM