hello,
pls find out why code is not work for solitaire game...
Thanks in advance.:rolleyes:
code:

#include<iostream>
using namespace std;
/*****CLASS PLAYING CARD*****/
class PlayingCard
{
private:
int rank;//integer 1-13
int suit;//integer0-3
char color;//red('r') or black('b')
public:
PlayingCard(int,int);
PlayingCard();
void display();
~PlayingCard();
const static int diamond;
const static int heart;
const static int spade;
const static int club;
};
/***** CLASS MAKING PILE OF CARDS*****/
class PileofCards
{
private:
PlayingCard *pile;//pointer to array of playing cards
int top;//last element added to array
int size;//no. of cards in pile 
int position;//position of pile amongst others
public:
PileofCards(int,int);
~PileofCards();
PlayingCard Peek();
PlayingCard Remove();
void display();
void Add(PlayingCard);
bool IsEmpty();
bool IsFull();
};

/***** CLASS DECK OF CARDS*****/
class Deck
{
private:
PlayingCard *deck[52];
int size;
public:
Deck();
int getSize();
bool IsEmpty();
PlayingCard getCard(int i);
void Display();
PlayingCard removeCard(int i);
~Deck();
};
/*****CLASS SOLITIRE*****/
class Solitire{

private: 
Deck deckofCards;
PileofCards shuffled;
public: 
Solitire();
void shuffle();
void display();
}; 
Solitire::Solitire():shuffled(52,1)
{}

/*****INITIALIZING*****/
const int PlayingCard::diamond=0;
const int PlayingCard::heart=1;
const int PlayingCard::spade=2;
const int PlayingCard::club=3;
/*****CONSTRUCTOR PLAYING CARD*****/
PlayingCard::PlayingCard(int X,int Y)
{
rank=X;
suit=Y;
if(Y==0 || Y==1)
{
color='r';
}
else if(Y==2 || Y==3)
{
color='b';
}
else
{ 
cout<<"invalid suit,object not created"<<endl;
}
}
/*****CONSTRUCTOR OVERLOADED*****/
PlayingCard::PlayingCard()
{
}
/*****destructor*****/
PlayingCard::~PlayingCard()
{
}

/*****CONSTRUCTOR PILE OF CARDS*****/
PileofCards::PileofCards(int X,int Y)
{
size=X;
position=Y;
pile= new PlayingCard[size];
top=-1;//empty array
}
/*****DESTRUCTOR*****/
PileofCards::~PileofCards()
{
delete [] pile;
}
/*****IS EMPTY*****/
bool PileofCards::IsEmpty()
{
if(top==-1)
{
cout<<"pile empty"<<endl;
return true;
}
else
{
return false;
}
}
/*****IS FULL*****/
bool PileofCards::IsFull()
{
if(top==size-1)
{
cout<<"sorry cannot add since pile is full"<<endl;
return true;
}
else 
{
return false;
}


}
/*****ADDING PLAYING CARD TO THE PILE*****/
void PileofCards::Add(PlayingCard X)
{
if(!IsFull())
{
pile[top+1]=X;
top++;
}
}
/*****REMOVING CARD FROM THE PILE*****/
PlayingCard PileofCards::Remove()
{
if(!IsEmpty())
{
top--;
}
return pile[top+1];
}
/*****PEEKING TOP OF THE PILE*****/
PlayingCard PileofCards::Peek()
{
return pile [top];
}
/*****DISPLAY....PLAYING CARD*****/
void PlayingCard::display()
{
cout<<rank<<" "<<suit<<" "<<color<<endl;
}
/*****CONSTRUCTOR DECK*****/
Deck::deck()
{ 
int j=0;
for(int i=1;i<=13;i++)
{
deck[j]=new PlayingCard(i,PlayingCard::spade);
j++;
}

for(int k=1;k<=13;k++)
{
deck[j]=new PlayingCard(k,PlayingCard::diamond);
j++;
}

for(int l=1;l<=13;l++)
{
deck[j]=new PlayingCard(l,PlayingCard::heart);
j++;
}

for(int m=1;m<=13;m++)
{
deck[j]=new PlayingCard(m,PlayingCard::club);
j++;
}
int size=52;
}
/*****DESTRUCTOR DECK*****/

Deck::~Deck()
{
if(!IsEmpty())
{
for(int i=0;i<size;i++)
{
if(deck[i]!=NULL)
{
delete deck[i];
}
}
}
else 
{
delete [] deck;
}
}
/*****GET SIZE*****/
int Deck::getSize()
{
return size;
}
/*****IS EMPTY....DECK*****/
bool Deck::IsEmpty()
{
if(getSize()==0)
{
return true;
}
else 
return false;
}
/*****GETCARD*****/
PlayingCard Deck::getCard(int j)
{
return *deck[j];
}
/*****DISPLAY....DECK*****/
void Deck:display()
{
for(int i=0;i<size;i++)
{
deck[i]->display();
}

}
/*****REMOVE...DECK*****/
PlayingCard Deck::removeCard(int i)
{

PlayingCard temp;
temp=getCard(i);

delete deck[i];
if(!IsEmpty())
{
for(int j=i;j<size;j++)
{
deck[j]=deck[j+1];
}

}

size--;

return temp;
}
/*****SHUFFLE*****/
void Solitire::shuffle()
{
int i; 
while (!deckofCards.IsEmpty())
{
i = rand()%deckofCards.getSize(); 
// cout<<"Remove Card \n";
shuffled.Add(deckofCards.removeCard(i));

}

}
/*****DISPLAY...SOLITIRE*****/
void Solitire::display()
{

if(!shuffled.IsEmpty())
{
shuffled.display();
}

}
/*****DISPLAY...PILE OF CARDS*****/
void PileofCards::display()
{ 
int i=0;
while(i<=top)
{
pile->display();
i++;
pile++;
}
cout<<i<<endl;
}
/*****MAIN*****/
int main()
{
PlayingCard A(3,PlayingCard::spade);
PlayingCard B(2,PlayingCard::spade);
PlayingCard C(4,PlayingCard::heart);
PlayingCard D(5,PlayingCard::club);
PlayingCard E(3,PlayingCard::diamond);

PlayingCard temp (3,PlayingCard::heart);

PileofCards pile1(5,1);

//cout<<"blah"<<endl;

pile1.Add(A);
pile1.Add(B);
pile1.Add(C);
pile1.Add(D);
pile1.Add(E);

pile1.Add(temp);//displays error

//cout<<"blah"<<endl;
temp=pile1.Remove();
temp.display();

temp=pile1.Remove();
temp.display();

temp=pile1.Remove();
temp.display();

temp=pile1.Remove();
temp.display();

temp=pile1.Remove();
temp.display();

temp=pile1.Remove();//displays error

Deck D1;
PlayingCard F(1,1);
cout<<"original deck"<<endl;
D1.Display(); // Original Deck- 52 cards should be displayed

cout<<"removed cards"<<endl;//index from 0-51
F = D1.removeCard(1); 
F.display(); 
F = D1.removeCard(1);
F.display(); 
F = D1.removeCard(1);
F.display(); // three cards removed

cout<<"final deck after removal"<<endl;
D1.Display(); //49 cards should be displayed now. 3 cards are already removed
cout<<"loop empties deck"<<endl;
int i=0;
while(!D1.IsEmpty())
{ 
A = D1.removeCard(i);
A.display(); 
}
cout<<"emptied"<<endl;
Solitire S; 
S.display(); //shuffled pile is empty- nothing should be displayed
S.shuffle(); 
S.display(); //Shuffled pile now contains 52 shuffled cards. 

return 0;
}

Not without telling us what is it's doing wrong. Yuo have to tell us what the ptogram is doing, and what it should be doing instead. And don't forget to read the announcement above about BBCodes so you can post the code readably.

Sorry for code... it make confuse to findout why..
when I ran that code .. I got three error.. might be it from Deck..

Error 1 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 2 error C2063: 'Deck::deck' : not a function
Warning 3 warning C4154: deletion of an array expression; conversion to pointer supplied

pls help me to get it work.. thanks in advance.

Error 1 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Use int function() or void function(), not just function().

Which line is this on?

Warning 3 warning C4154: deletion of an array expression; conversion to pointer supplied

[edit] I get more errors than that.

MINGW

sourceFile.cpp:171: ISO 
   C++ forbids declaration of `deck' with no type
sourceFile.cpp:171: no `
   int Deck::deck()' member function declared in class `Deck'
sourceFile.cpp:238: syntax 
   error before `:' token
sourceFile.cpp:240: `size' 
   was not declared in this scope
sourceFile.cpp:240: parse 
   error before `;' token
sourceFile.cpp:240: syntax 
   error before `++' token
sourceFile.cpp:372:2: warning: no newline at end of file

MSVC

sourceFile.cpp
sourceFile.cpp(171) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
sourceFile.cpp(171) : error C2063: 'Deck::deck' : not a function
sourceFile.cpp(214) : warning C4154: deletion of an array expression; conversion to pointer supplied
sourceFile.cpp(238) : error C2470: 'Deck' : looks like a function definition, but there is no parameter list; skipping apparent body

[/edit]

hey thanks a lot for help me..
warning is on line 214

I don't have a line-numbering editor on this computer, and I'm not counting to 214. Post the actual line that is causing the error.

Okay, here's your problem.

Deck::deck()

deck() doesn't match the class name Deck so it isn't a constructor. C++ is case-sensitive. Use a constructor name that matches the class name exactly -- in this case, Deck.

hey thanks a lot for help me..
warning is on line 214

but Deck is constructor..I cnt out anytype or void at there..but if i let it like that error again .. with not a function like before.. :confused:
code:

Deck::deck()
{
int j=0;
for(int i=1;i<=13;i++)
{
deck[j]=new PlayingCard(i,PlayingCard::spade);
j++;
}

for(int k=1;k<=13;k++)
{
deck[j]=new PlayingCard(k,PlayingCard::diamond);
j++;
}

for(int l=1;l<=13;l++)
{
deck[j]=new PlayingCard(l,PlayingCard::heart);
j++;
}

for(int m=1;m<=13;m++)
{
deck[j]=new PlayingCard(m,PlayingCard::club);
j++;
}
int size=52;
}
/*****DESTRUCTOR DECK*****/

Deck::~Deck()
{
if(!IsEmpty())
{
for(int i=0;i<size;i++)
{
if(deck!=NULL)
{
delete deck;
}
}
}
else
{
delete [] deck;
}
}
/*****GET SIZE*****/

There's actually only 2 or 3 typos in the whole program - even if the compilers report more. Here's what I changed to make it compile:

/*****CONSTRUCTOR DECK*****/
Deck::deck()   // you previously declared it as Deck::Deck
{ 
int j=0;
for(int i=1;i<=13;i++)
{
deck[j]=new PlayingCard(i,PlayingCard::spade);
j++;
}

Same problem here:

/*****DISPLAY....DECK*****/
void Deck:display()   // missing capitalization, needs extra ':'
{
for(int i=0;i<size;i++)
{
deck[i]->display();
}

But even though it compiles, you've got a few bugs in there, too.

Hope this helps

See this line?

Deck::deck()

It should look like this.

Deck::Deck()

[edit] Too slow. [/edit]

I think you've got some sort of bug in Deck , because when you initalize D1 , Deck::size never gets initalized - leading to an endless loop here:

while(!D1.IsEmpty())
{ 
A = D1.removeCard(i);
A.display(); 
}

thanks again .. now i know 'D' and 'd'...but when i ran it i got two errors again,,
code:

void Deck:display()
{
for(int i=0;i<size;i++)
{
deck[i]->display();
}

//Error    2    error C2470: 'Deck' : looks like a function definition, but there is no parameter list; skipping apparent body

and other warning..
code:

Deck::~Deck()
{
if(!IsEmpty())
{
for(int i=0;i<size;i++)
{
if(deck[i]!=NULL)
{
delete deck[i];
}
}
}
else 
{
delete [] deck;//warning ..here
}
}    

//Warning    1    warning C4154: deletion of an array expression; conversion to pointer supplied
void Deck:display()
{
for(int i=0;i<size;i++)
{
deck[i]->display();
}

You still haven't added a second ':' to the function, and you need to capitalize the 'd'; this is how it should look:

void Deck::Display()
 {
 for(int i=0;i<size;i++)
 {
 deck[i]->display();
 }

-edit- PLEASE use code tags! There's some info about them in my signature.

Thank you very much!
Now everything is ok!No error anymore and I can run the program.
But it not work coz of warning.And if I debug the program,it said "Debug Assertion Failed!"and with "Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)"

Here is part of code:

Deck::~Deck()
{
if(!IsEmpty())
{
for(int i=0;i<size;i++)
{
if(deck[i]!=NULL)
{
delete deck[i];
}
}
}
else 
{
 delete [] deck;//warning from here..
}
}

No need of checking for NULL while using C++ delete, the implications of this are taken care by the standard C++ implementation.

Given the message, I think somethign is getting deleted twice here. Better set the pointer to 0 after deleting it.

And btw, just post your entire code so we can have a look at your class declarations and all that.

Now everything is ok!No error anymore and I can run the program.
But it not work coz of warning.And if I debug the program,it said "Debug Assertion Failed!"and with "Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)" :mad:

Here is part of code:

Deck::~Deck()
{
if(!IsEmpty())
{
for(int i=0;i{
if(deck[i]!=NULL)
{
delete deck[i];
}
}
}
else 
{
Code:
delete [] deck;//warning from here..(complier point out here)
}
}

Can you post the entire code, not just the part that you're having problems with? That way someone can try running and debugging the program.

Give the program's output when run, give what you expected to see, and show any possible places that may have bugs/errors.

"Debug Assertion Failed!"and with "Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)"

When you get that it means that somehow you corrupted your memory and the allocator isn't getting what it expects to free it. The bug is almost never near the delete operator, and it could be anywhere. In the case of memory bugs, post all of your code after stripping it down as much as you can. :)

Thanks for u guys for helping me .:)
here is the code:

#include<iostream>
using namespace std;
/*****CLASS PLAYING CARD*****/
class PlayingCard
{
private:
int rank;//integer 1-13
int suit;//integer0-3
char color;//red('r') or black('b')
public:
PlayingCard(int,int);
PlayingCard();
void display();
~PlayingCard();
const static int diamond;
const static int heart;
const static int spade;
const static int club;
};
/***** CLASS MAKING PILE OF CARDS*****/
class PileofCards
{
private:
PlayingCard *pile;//pointer to array of playing cards
int top;//last element added to array
int size;//no. of cards in pile 
int position;//position of pile amongst others
public:
PileofCards(int,int);
~PileofCards();
PlayingCard Peek();
PlayingCard Remove();
void display();
void Add(PlayingCard);
bool IsEmpty();
bool IsFull();
};

/***** CLASS DECK OF CARDS*****/
class Deck
{
private:
PlayingCard *deck[52];
int size;
public:
Deck();
int getSize();
bool IsEmpty();
PlayingCard getCard(int i);
void Display();
PlayingCard removeCard(int i);
~Deck();
};
/*****CLASS SOLITIRE*****/
class Solitire{

private: 
Deck deckofCards;
PileofCards shuffled;
public: 
Solitire();
void shuffle();
void display();
}; 
Solitire::Solitire():shuffled(52,1)
{}

/*****INITIALIZING*****/
const int PlayingCard::diamond=0;
const int PlayingCard::heart=1;
const int PlayingCard::spade=2;
const int PlayingCard::club=3;
/*****CONSTRUCTOR PLAYING CARD*****/
PlayingCard::PlayingCard(int X,int Y)
{
rank=X;
suit=Y;
if(Y==0 || Y==1)
{
color='r';
}
else if(Y==2 || Y==3)
{
color='b';
}
else
{ 
cout<<"invalid suit,object not created"<<endl;
}
}
/*****CONSTRUCTOR OVERLOADED*****/
PlayingCard::PlayingCard()
{
}
/*****destructor*****/
PlayingCard::~PlayingCard()
{
}

/*****CONSTRUCTOR PILE OF CARDS*****/
PileofCards::PileofCards(int X,int Y)
{
size=X;
position=Y;
pile= new PlayingCard[size];
top=-1;//empty array
}
/*****DESTRUCTOR*****/
PileofCards::~PileofCards()
{
delete [] pile;
}
/*****IS EMPTY*****/
bool PileofCards::IsEmpty()
{
if(top==-1)
{
cout<<"pile empty"<<endl;
return true;
}
else
{
return false;
}
}
/*****IS FULL*****/
bool PileofCards::IsFull()
{
if(top==size-1)
{
cout<<"sorry cannot add since pile is full"<<endl;
return true;
}
else 
{
return false;
}


}
/*****ADDING PLAYING CARD TO THE PILE*****/
void PileofCards::Add(PlayingCard X)
{
if(!IsFull())
{
pile[top+1]=X;
top++;
}
}
/*****REMOVING CARD FROM THE PILE*****/
PlayingCard PileofCards::Remove()
{
if(!IsEmpty())
{
top--;
}
return pile[top+1];
}
/*****PEEKING TOP OF THE PILE*****/
PlayingCard PileofCards::Peek()
{
return pile [top];
}
/*****DISPLAY....PLAYING CARD*****/
void PlayingCard::display()
{
cout<<rank<<" "<<suit<<" "<<color<<endl;
}
/*****CONSTRUCTOR DECK*****/
Deck::Deck()
{ 
int j=0;
for(int i=1;i<=13;i++)
{
deck[j]=new PlayingCard(i,PlayingCard::spade);
j++;
}

for(int k=1;k<=13;k++)
{
deck[j]=new PlayingCard(k,PlayingCard::diamond);
j++;
}

for(int l=1;l<=13;l++)
{
deck[j]=new PlayingCard(l,PlayingCard::heart);
j++;
}

for(int m=1;m<=13;m++)
{
deck[j]=new PlayingCard(m,PlayingCard::club);
j++;
}
int size=52;
}
/*****DESTRUCTOR DECK*****/

Deck::~Deck()
{
if(!IsEmpty())
{
for(int i=0;i<size;i++)
{
if(deck[i]!=NULL)
{
delete deck[i];
}
}
}
else 
{
delete [] deck;
}
}
/*****GET SIZE*****/
int Deck::getSize()
{
return size;
}
/*****IS EMPTY....DECK*****/
bool Deck::IsEmpty()
{
if(getSize()==0)
{
return true;
}
else 
return false;
}
/*****GETCARD*****/
PlayingCard Deck::getCard(int j)
{
return *deck[j];
}
/*****DISPLAY....DECK*****/
void Deck::Display()
{
for(int i=0;i<size;i++)
{
deck[i]->display();
}

}
/*****REMOVE...DECK*****/
PlayingCard Deck::removeCard(int i)
{

PlayingCard temp;
temp=getCard(i);

delete deck[i];
if(!IsEmpty())
{
for(int j=i;j<size;j++)
{
deck[j]=deck[j+1];
}

}

size--;

return temp;
}
/*****SHUFFLE*****/
void Solitire::shuffle()
{
int i; 
while (!deckofCards.IsEmpty())
{
i = rand()%deckofCards.getSize(); 
// cout<<"Remove Card \n";
shuffled.Add(deckofCards.removeCard(i));

}

}
/*****DISPLAY...SOLITIRE*****/
void Solitire::display()
{

if(!shuffled.IsEmpty())
{
shuffled.display();
}

}
/*****DISPLAY...PILE OF CARDS*****/
void PileofCards::display()
{ 
int i=0;
while(i<=top)
{
pile->display();
i++;
pile++;
}
cout<<i<<endl;
}
/*****MAIN*****/
int main()
{
PlayingCard A(3,PlayingCard::spade);
PlayingCard B(2,PlayingCard::spade);
PlayingCard C(4,PlayingCard::heart);
PlayingCard D(5,PlayingCard::club);
PlayingCard E(3,PlayingCard::diamond);

PlayingCard temp (3,PlayingCard::heart);

PileofCards pile1(5,1);

//cout<<"blah"<<endl;

pile1.Add(A);
pile1.Add(B);
pile1.Add(C);
pile1.Add(D);
pile1.Add(E);

pile1.Add(temp);//displays error

//cout<<"blah"<<endl;
temp=pile1.Remove();
temp.display();

temp=pile1.Remove();
temp.display();

temp=pile1.Remove();
temp.display();

temp=pile1.Remove();
temp.display();

temp=pile1.Remove();
temp.display();

temp=pile1.Remove();//displays error

Deck D1;
PlayingCard F(1,1);
cout<<"original deck"<<endl;
D1.Display(); // Original Deck- 52 cards should be displayed

cout<<"removed cards"<<endl;//index from 0-51
F = D1.removeCard(1); 
F.display(); 
F = D1.removeCard(1);
F.display(); 
F = D1.removeCard(1);
F.display(); // three cards removed

cout<<"final deck after removal"<<endl;
D1.Display(); //49 cards should be displayed now. 3 cards are already removed
cout<<"loop empties deck"<<endl;
int i=0;
while(!D1.IsEmpty())
{ 
A = D1.removeCard(i);
A.display(); 
}
cout<<"emptied"<<endl;
Solitire S; 
S.display(); //shuffled pile is empty- nothing should be displayed
S.shuffle(); 
S.display(); //Shuffled pile now contains 52 shuffled cards. 

return 0;
}

When I run the code, I get a lot of deallocation errors:

[Session started at 2007-01-01 15:03:41 -0800.]
sorry cannot add since pile is full
3 0 r
5 3 b
4 1 r
2 2 b
3 2 b
pile empty
original deck
removed cards
2 2 b
TotalCharges(649) malloc: *** error for object 0x300410: double free
TotalCharges(649) malloc: *** set a breakpoint in szone_error to debug
2 2 b
TotalCharges(649) malloc: *** error for object 0x300410: double free
TotalCharges(649) malloc: *** set a breakpoint in szone_error to debug
2 2 b
final deck after removal
loop empties deck
1 2 b
TotalCharges(649) malloc: *** error for object 0x300400: double free
TotalCharges(649) malloc: *** set a breakpoint in szone_error to debug
1 2 b
TotalCharges(649) malloc: *** error for object 0x300400: double free
TotalCharges(649) malloc: *** set a breakpoint in szone_error to debug
1 2 b

This happens to be from these lines of code:

[B]Deck D1;[/B]
PlayingCard F(1,1);
cout<<"original deck"<<endl;
D1.Display(); // Original Deck- 52 cards should be displayed

cout<<"removed cards"<<endl;//index from 0-51
 F = D1.removeCard(1); 
F.display(); 
 F = D1.removeCard(1);
F.display(); 
 F = D1.removeCard(1);
F.display(); // three cards removed

cout<<"final deck after removal"<<endl;
D1.Display(); //49 cards should be displayed now. 3 cards are already removed
cout<<"loop empties deck"<<endl;
int i=0;
while(!D1.IsEmpty())
{ 
 A = D1.removeCard(i);
A.display(); 
}

Now, what's happening is that Deck 's got some serious problems. Do you realize that Deck::size never gets set in all that code? Look in Deck's functions if you don't believe me. That means that D1.size is set to some random integer, in my case it was negative, thus creating a never-ending loop at the end because the deck's size never hit 0 (it was always below 0).

Also, because of this, Deck::~Deck causes even more errors were the program to ever quit because it's supposed to loop through all the cards, but if the size variable is messed up, it will go into another endless loop.

I've also noticed that everytime removeCard() is called, the malloc error is displayed when it trys to delete itself. My theory is that something isn't getting stored right in Deck's constructor when the cards are created. You should look at it more carefully and make sure everything is happening there as you intended.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.