So, we are making a card game for class using a deck of cards valued 1 to 52, using lists and stacks. It deals each player a card, compares the value, then awards a point to the winner and restarts. I am using stacks to keep track of points, and I am using a list for the deck. I have it so that it pulls a random number from the deck, but it does not erase after the number is revealed. Everything in the code is working except for the erase function. I will put up my List class (list.h), my erase function in the list.cpp file, and my main function where I have the player being dealt a card, and then trying to delete the card from the list.
list.h
#ifndef LIST_H
#define LIST_H
#include <iostream>
using namespace std;
typedef int datatype;
class list
{
class Node
{
public:
datatype value;
Node *next;
Node():next(0){}
Node(datatype val):value(val),next(0){}
} ;
int mySize;
Node *temp;
Node *first;
public:
list();
~list();
list(list &);// copy constructor
void insert(int pos , datatype &);
void erase (int pos);
Node * Index2Pointer(int pos);
bool empty();
int returnSize();
datatype readithValue(int pos);// read the ith value where is is passed as argument
// Overloaded operator
list & operator=(list &);// overloaded assignment operator
friend ostream & operator<< ( ostream & , list &);
};
#endif
erase function from list.cpp
void list::erase(int pos)
{
if (pos < 0 || pos > mySize){
cerr<< "Illegal location "<< endl;
return;
}
mySize--;
list::Node *ptr;
if (pos == 0){
ptr = first;
first = first->next;
delete ptr;
}
else{
list::Node *temp;
ptr = Index2Pointer(pos);
temp = ptr->next;
ptr->next=ptr->next->next;
delete temp;
}
}
Part of main.cpp which deals the card and calculates points
The name of my list class is deck
for (int count = 0; count < 27; count++)
cout << "Player 1, press y and enter to draw a card" << endl;
cin >> choice;
cout << endl;
card = rand() % 52 +1;
cout << " Player 1, your card is " << card << endl;
p1 = card;
pos = card;
pos++;
deck.erase(pos);
cout << "Player 2, press y and enter to draw a card" << endl;
cin >> choice;
cout << endl;
card = rand() % 52 + 1;
cout << "Player 2, your card is " << card << endl;
p2 = card;
pos = card;
pos++;
deck.erase(pos);
I will take this down if I figure out what is wrong with it, and it is probably something very simple, but I have run through it in my head for about an hour now, and I can't figure it out.
THank you for all the help in advance. :)