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();
}