Thread: Issue with points

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    4

    Issue with points

    I believe my problem has to do with points. The whole story is I am trying to create a blackjack program for my class. I planned on creating 2 linked lists, one empty one with the deck of cards. As cards are dealt they are put into the second linked list and taken from the first so that they are not repeated.

    After creating what I thought was a working linked list I wrote the code to populate the linked list. Card is a structure with data members. One int 0-3 for suit and another int 0-12 denoting rank. It compiles, however upon debugging I get an error saying

    First-chance exception at 0x00411964 in BlackJack.exe: 0xC0000005: Access violation reading location 0x00000008.
    Unhandled exception at 0x00411964 in BlackJack.exe: 0xC0000005: Access violation reading location 0x00000008.

    The program breaks at line 60, q->next=start->next
    Code:
    void LinkedList::insert(Card C1)
    {
       Node *q;
    	
        q = new Node;
    	q->next=start->next;
    	q->C=C1;
    	start->next=q;
    
    }
    I'm fairly sure this is an issue with my usage of pointers as I'm very new to them.

    The entire code is as follows
    Code:
    #include <conio.h>
    #include <iostream>
    using namespace std;
    
    
    
    
    
    struct Card
    {
    	int suit;
    	int rank;
    
    };
    
    ///////////////////////////////////////Begining of Linked List/////////////////
    class LinkedList
    {
    private:
    	struct Node
    		{
    			Card C;
    			Node *next;
    		}*start,*start2,*end;
    	
    	
    
    public:
    	LinkedList();
    	
    	void insert(Card C1);
    	Card fetch(int N);
    	void shuffle();
    	
    };
    
    LinkedList::LinkedList()
    {
        start = NULL;
        start2=NULL;
    	
    	
    }
    
    ////////////////////////////////Insert/////////////////////////
    void LinkedList::insert(Card C1)
    {
       Node *q;
    	
        q = new Node;
    	q->next=start->next;
    	q->C=C1;
    	start->next=q;
    
    }
    
    
    ////////////////////////////////////////////////Fetch//////////////////////////
    Card LinkedList::fetch(int N)
    {
    	int counter=-1;	
        Node *q,*t;
        q = start;
       
       while (counter!=N)
       {
    		t=q;
    		q=q->next;
       }
       if(q->next!=NULL)
    	{   start2->next=q;
    		t->next = q->next;
    		return q->C;
    	}
       else
       {   
    		start2->next=q;
    		t->next=NULL;
    		return q->C;
       }
    }
    ////////////////////////////Remake Deck
    void LinkedList::shuffle()
    {
    	LinkedList empty;
    	while (start2->next!=NULL)
    	{
    		empty.insert(start2->next->C);
    		start2=start2->next;
    	}
    }
    
    
    int main()
    {
    	Card c,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,D1,D2,D3,D4,D5,D6,D7;
    	LinkedList l1;
    	int counter1=0,counter2=0;
    	///////////////populate list///////////////
    	
    	while (counter1!=4)
    	{
    		while (counter2!=13)
    		{
    			c.suit=counter1;
    			c.rank=counter2;
    			l1.insert(c);
    			++counter2;
    		}
    		counter2=0;
    		++counter1;
    	}
    
    	P1=l1.fetch(2);
    	cout<<P1.suit<<endl<<P1.rank;
    	cout<<"Hello world";
    	_getche();
    }

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Do you ever allocate start? All I see is it getting set to NULL. Accessing it (as you're doing) would certainly cause a crash.

    Are you aware that C++ provides a linked list implementation, std::list<T> ?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    Nothing like the internet to make you feel humble. Thanks, I'll look into the std::list and yeah that is why it was crashing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help it won't compile!!!!!
    By esbo in forum C Programming
    Replies: 58
    Last Post: 01-04-2009, 03:22 PM
  2. Replies: 8
    Last Post: 11-03-2008, 09:48 PM
  3. Replies: 1
    Last Post: 11-27-2007, 07:41 AM
  4. Yahtzee C++ programme help
    By kenneth_888 in forum C++ Programming
    Replies: 13
    Last Post: 09-05-2007, 02:14 PM
  5. CProg Fantasy Football version pi
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 155
    Last Post: 12-26-2006, 04:30 PM