944,099 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3542
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 30th, 2006
0

help for error

Expand Post »
hello,
pls find out why code is not work for solitaire game...
Thanks in advance.:rolleyes:
code:
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. using namespace std;
  3. /*****CLASS PLAYING CARD*****/
  4. class PlayingCard
  5. {
  6. private:
  7. int rank;//integer 1-13
  8. int suit;//integer0-3
  9. char color;//red('r') or black('b')
  10. public:
  11. PlayingCard(int,int);
  12. PlayingCard();
  13. void display();
  14. ~PlayingCard();
  15. const static int diamond;
  16. const static int heart;
  17. const static int spade;
  18. const static int club;
  19. };
  20. /***** CLASS MAKING PILE OF CARDS*****/
  21. class PileofCards
  22. {
  23. private:
  24. PlayingCard *pile;//pointer to array of playing cards
  25. int top;//last element added to array
  26. int size;//no. of cards in pile
  27. int position;//position of pile amongst others
  28. public:
  29. PileofCards(int,int);
  30. ~PileofCards();
  31. PlayingCard Peek();
  32. PlayingCard Remove();
  33. void display();
  34. void Add(PlayingCard);
  35. bool IsEmpty();
  36. bool IsFull();
  37. };
  38.  
  39. /***** CLASS DECK OF CARDS*****/
  40. class Deck
  41. {
  42. private:
  43. PlayingCard *deck[52];
  44. int size;
  45. public:
  46. Deck();
  47. int getSize();
  48. bool IsEmpty();
  49. PlayingCard getCard(int i);
  50. void Display();
  51. PlayingCard removeCard(int i);
  52. ~Deck();
  53. };
  54. /*****CLASS SOLITIRE*****/
  55. class Solitire{
  56.  
  57. private:
  58. Deck deckofCards;
  59. PileofCards shuffled;
  60. public:
  61. Solitire();
  62. void shuffle();
  63. void display();
  64. };
  65. Solitire::Solitire():shuffled(52,1)
  66. {}
  67.  
  68. /*****INITIALIZING*****/
  69. const int PlayingCard::diamond=0;
  70. const int PlayingCard::heart=1;
  71. const int PlayingCard::spade=2;
  72. const int PlayingCard::club=3;
  73. /*****CONSTRUCTOR PLAYING CARD*****/
  74. PlayingCard::PlayingCard(int X,int Y)
  75. {
  76. rank=X;
  77. suit=Y;
  78. if(Y==0 || Y==1)
  79. {
  80. color='r';
  81. }
  82. else if(Y==2 || Y==3)
  83. {
  84. color='b';
  85. }
  86. else
  87. {
  88. cout<<"invalid suit,object not created"<<endl;
  89. }
  90. }
  91. /*****CONSTRUCTOR OVERLOADED*****/
  92. PlayingCard::PlayingCard()
  93. {
  94. }
  95. /*****destructor*****/
  96. PlayingCard::~PlayingCard()
  97. {
  98. }
  99.  
  100. /*****CONSTRUCTOR PILE OF CARDS*****/
  101. PileofCards::PileofCards(int X,int Y)
  102. {
  103. size=X;
  104. position=Y;
  105. pile= new PlayingCard[size];
  106. top=-1;//empty array
  107. }
  108. /*****DESTRUCTOR*****/
  109. PileofCards::~PileofCards()
  110. {
  111. delete [] pile;
  112. }
  113. /*****IS EMPTY*****/
  114. bool PileofCards::IsEmpty()
  115. {
  116. if(top==-1)
  117. {
  118. cout<<"pile empty"<<endl;
  119. return true;
  120. }
  121. else
  122. {
  123. return false;
  124. }
  125. }
  126. /*****IS FULL*****/
  127. bool PileofCards::IsFull()
  128. {
  129. if(top==size-1)
  130. {
  131. cout<<"sorry cannot add since pile is full"<<endl;
  132. return true;
  133. }
  134. else
  135. {
  136. return false;
  137. }
  138.  
  139.  
  140. }
  141. /*****ADDING PLAYING CARD TO THE PILE*****/
  142. void PileofCards::Add(PlayingCard X)
  143. {
  144. if(!IsFull())
  145. {
  146. pile[top+1]=X;
  147. top++;
  148. }
  149. }
  150. /*****REMOVING CARD FROM THE PILE*****/
  151. PlayingCard PileofCards::Remove()
  152. {
  153. if(!IsEmpty())
  154. {
  155. top--;
  156. }
  157. return pile[top+1];
  158. }
  159. /*****PEEKING TOP OF THE PILE*****/
  160. PlayingCard PileofCards::Peek()
  161. {
  162. return pile [top];
  163. }
  164. /*****DISPLAY....PLAYING CARD*****/
  165. void PlayingCard::display()
  166. {
  167. cout<<rank<<" "<<suit<<" "<<color<<endl;
  168. }
  169. /*****CONSTRUCTOR DECK*****/
  170. Deck::deck()
  171. {
  172. int j=0;
  173. for(int i=1;i<=13;i++)
  174. {
  175. deck[j]=new PlayingCard(i,PlayingCard::spade);
  176. j++;
  177. }
  178.  
  179. for(int k=1;k<=13;k++)
  180. {
  181. deck[j]=new PlayingCard(k,PlayingCard::diamond);
  182. j++;
  183. }
  184.  
  185. for(int l=1;l<=13;l++)
  186. {
  187. deck[j]=new PlayingCard(l,PlayingCard::heart);
  188. j++;
  189. }
  190.  
  191. for(int m=1;m<=13;m++)
  192. {
  193. deck[j]=new PlayingCard(m,PlayingCard::club);
  194. j++;
  195. }
  196. int size=52;
  197. }
  198. /*****DESTRUCTOR DECK*****/
  199.  
  200. Deck::~Deck()
  201. {
  202. if(!IsEmpty())
  203. {
  204. for(int i=0;i<size;i++)
  205. {
  206. if(deck[i]!=NULL)
  207. {
  208. delete deck[i];
  209. }
  210. }
  211. }
  212. else
  213. {
  214. delete [] deck;
  215. }
  216. }
  217. /*****GET SIZE*****/
  218. int Deck::getSize()
  219. {
  220. return size;
  221. }
  222. /*****IS EMPTY....DECK*****/
  223. bool Deck::IsEmpty()
  224. {
  225. if(getSize()==0)
  226. {
  227. return true;
  228. }
  229. else
  230. return false;
  231. }
  232. /*****GETCARD*****/
  233. PlayingCard Deck::getCard(int j)
  234. {
  235. return *deck[j];
  236. }
  237. /*****DISPLAY....DECK*****/
  238. void Deck:display()
  239. {
  240. for(int i=0;i<size;i++)
  241. {
  242. deck[i]->display();
  243. }
  244.  
  245. }
  246. /*****REMOVE...DECK*****/
  247. PlayingCard Deck::removeCard(int i)
  248. {
  249.  
  250. PlayingCard temp;
  251. temp=getCard(i);
  252.  
  253. delete deck[i];
  254. if(!IsEmpty())
  255. {
  256. for(int j=i;j<size;j++)
  257. {
  258. deck[j]=deck[j+1];
  259. }
  260.  
  261. }
  262.  
  263. size--;
  264.  
  265. return temp;
  266. }
  267. /*****SHUFFLE*****/
  268. void Solitire::shuffle()
  269. {
  270. int i;
  271. while (!deckofCards.IsEmpty())
  272. {
  273. i = rand()%deckofCards.getSize();
  274. // cout<<"Remove Card \n";
  275. shuffled.Add(deckofCards.removeCard(i));
  276.  
  277. }
  278.  
  279. }
  280. /*****DISPLAY...SOLITIRE*****/
  281. void Solitire::display()
  282. {
  283.  
  284. if(!shuffled.IsEmpty())
  285. {
  286. shuffled.display();
  287. }
  288.  
  289. }
  290. /*****DISPLAY...PILE OF CARDS*****/
  291. void PileofCards::display()
  292. {
  293. int i=0;
  294. while(i<=top)
  295. {
  296. pile->display();
  297. i++;
  298. pile++;
  299. }
  300. cout<<i<<endl;
  301. }
  302. /*****MAIN*****/
  303. int main()
  304. {
  305. PlayingCard A(3,PlayingCard::spade);
  306. PlayingCard B(2,PlayingCard::spade);
  307. PlayingCard C(4,PlayingCard::heart);
  308. PlayingCard D(5,PlayingCard::club);
  309. PlayingCard E(3,PlayingCard::diamond);
  310.  
  311. PlayingCard temp (3,PlayingCard::heart);
  312.  
  313. PileofCards pile1(5,1);
  314.  
  315. //cout<<"blah"<<endl;
  316.  
  317. pile1.Add(A);
  318. pile1.Add(B);
  319. pile1.Add(C);
  320. pile1.Add(D);
  321. pile1.Add(E);
  322.  
  323. pile1.Add(temp);//displays error
  324.  
  325. //cout<<"blah"<<endl;
  326. temp=pile1.Remove();
  327. temp.display();
  328.  
  329. temp=pile1.Remove();
  330. temp.display();
  331.  
  332. temp=pile1.Remove();
  333. temp.display();
  334.  
  335. temp=pile1.Remove();
  336. temp.display();
  337.  
  338. temp=pile1.Remove();
  339. temp.display();
  340.  
  341. temp=pile1.Remove();//displays error
  342.  
  343. Deck D1;
  344. PlayingCard F(1,1);
  345. cout<<"original deck"<<endl;
  346. D1.Display(); // Original Deck- 52 cards should be displayed
  347.  
  348. cout<<"removed cards"<<endl;//index from 0-51
  349. F = D1.removeCard(1);
  350. F.display();
  351. F = D1.removeCard(1);
  352. F.display();
  353. F = D1.removeCard(1);
  354. F.display(); // three cards removed
  355.  
  356. cout<<"final deck after removal"<<endl;
  357. D1.Display(); //49 cards should be displayed now. 3 cards are already removed
  358. cout<<"loop empties deck"<<endl;
  359. int i=0;
  360. while(!D1.IsEmpty())
  361. {
  362. A = D1.removeCard(i);
  363. A.display();
  364. }
  365. cout<<"emptied"<<endl;
  366. Solitire S;
  367. S.display(); //shuffled pile is empty- nothing should be displayed
  368. S.shuffle();
  369. S.display(); //Shuffled pile now contains 52 shuffled cards.
  370.  
  371. return 0;
  372. }
Last edited by ~s.o.s~; Dec 30th, 2006 at 4:42 pm. Reason: Fixed code tags, learn to use them.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
sTorM is offline Offline
25 posts
since Nov 2006
Dec 30th, 2006
0

Re: help for error

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.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Dec 30th, 2006
0

Re: help for error

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
sTorM is offline Offline
25 posts
since Nov 2006
Dec 30th, 2006
0

Re: help for error

C++ Syntax (Toggle Plain Text)
  1. 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?
C++ Syntax (Toggle Plain Text)
  1. Warning 3 warning C4154: deletion of an array expression; conversion to pointer supplied

[edit] I get more errors than that.

MINGW
C++ Syntax (Toggle Plain Text)
  1. sourceFile.cpp:171: ISO
  2. C++ forbids declaration of `deck' with no type
  3. sourceFile.cpp:171: no `
  4. int Deck::deck()' member function declared in class `Deck'
  5. sourceFile.cpp:238: syntax
  6. error before `:' token
  7. sourceFile.cpp:240: `size'
  8. was not declared in this scope
  9. sourceFile.cpp:240: parse
  10. error before `;' token
  11. sourceFile.cpp:240: syntax
  12. error before `++' token
  13. sourceFile.cpp:372:2: warning: no newline at end of file
  14.  

MSVC
C++ Syntax (Toggle Plain Text)
  1. sourceFile.cpp
  2. sourceFile.cpp(171) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  3. sourceFile.cpp(171) : error C2063: 'Deck::deck' : not a function
  4. sourceFile.cpp(214) : warning C4154: deletion of an array expression; conversion to pointer supplied
  5. sourceFile.cpp(238) : error C2470: 'Deck' : looks like a function definition, but there is no parameter list; skipping apparent body
[/edit]
Last edited by dwks; Dec 30th, 2006 at 7:09 pm.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Dec 30th, 2006
0

Re: help for error

hey thanks a lot for help me..
warning is on line 214
Reputation Points: 10
Solved Threads: 0
Light Poster
sTorM is offline Offline
25 posts
since Nov 2006
Dec 30th, 2006
0

Re: help for error

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.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Dec 30th, 2006
0

Re: help for error

Okay, here's your problem.
C++ Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Dec 30th, 2006
0

Re: help for error

Click to Expand / Collapse  Quote originally posted by sTorM ...
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..
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[i]!=NULL)
{
delete deck[i];
}
}
}
else
{
delete [] deck;
}
}
/*****GET SIZE*****/
Reputation Points: 10
Solved Threads: 0
Light Poster
sTorM is offline Offline
25 posts
since Nov 2006
Dec 30th, 2006
0

Re: help for error

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
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Dec 30th, 2006
0

Re: help for error

See this line?
Deck::deck()
It should look like this.
Deck::Deck()

[edit] Too slow. [/edit]
Last edited by dwks; Dec 30th, 2006 at 7:33 pm.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: passing data to a program
Next Thread in C++ Forum Timeline: point me in the right direction - Exam/quiz etc result calculating programme





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC