card game: just need to know why its not compiling...

Reply

Join Date: Oct 2008
Posts: 11
Reputation: avillachandok is an unknown quantity at this point 
Solved Threads: 0
avillachandok avillachandok is offline Offline
Newbie Poster

card game: just need to know why its not compiling...

 
0
  #1
Oct 9th, 2008
firstly hi all,
i''d like to intrroduce myself. Im a new C++ programmer..hardly..but trying..so yes its frustrating with the error and syntax mainly. Anywayz, this is a card game with 2 classes and simple functions but im having a problem compiling it. I compile it on visual studio so i hope you use the same one.so this is my problem:
not sure with the deal() and the showdeck() method and can't solve the errors...
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4.  
  5. class Card
  6. {
  7. public: enum Suite
  8. {
  9. diamond,
  10. club,
  11. heart,
  12. spade
  13. };
  14. int Value;
  15. Suite Type;
  16. };
  17. class Deck
  18. {
  19. public:
  20. Card deck[52];
  21. Deck() //default constructor
  22. {
  23. for(int i=0;i<52;i++)
  24. {
  25. int q = i/13;
  26. int r = i%13;
  27. deck[i].Value = r;
  28. if(q==0)
  29. deck[i].Type = diamond;
  30. else if(q==1)
  31. deck[i].Type = club;
  32. else if(q==2)
  33. deck[i].Type = heart;
  34. else if(q==3)
  35. deck[i].Type = spade;
  36. }
  37. }
  38. void shuffle()
  39. {
  40. //shuffle elements by going through the deck and swapping with another random position.
  41. public:
  42. for(int i=0;i<52;i++)
  43. {
  44. int r = i + (rand() % (52-i)); //random remaining positions
  45. deck temp = deck[i]; //swap the values
  46. deck[i] = deck[r];
  47. deck[r] = deck temp;
  48. }
  49. }
  50. void deal()
  51. {
  52. int t=0,p=0;
  53. public:
  54. const int N = 6;
  55. Card hand1[N/2];
  56. Card hand2[N/2];
  57. private:
  58. for(int i=0;i<N;i++)
  59. {
  60. if((N%2)==0)
  61. {
  62. hand1[t]=deck[i]; //even values of the deck are distributed to hand 1.
  63. t++;
  64. }
  65. else
  66. {
  67. hand2[p]=deck[i]; //odd values of the deck are distributed to hand 2.
  68. p++;
  69. }
  70. }
  71. }
  72. void showdeck()
  73. {
  74. cout<<"Player one has:\n";
  75. while(s<(N/2))
  76. {
  77. cout<<hand1[s].Value;
  78. cout<<hand1[s].Type\n;
  79. s++;
  80. }
  81. cout<<"Player two has:\n";
  82. while(s<(N/2))
  83. {
  84. cout<<hand2[s].Value;
  85. cout<<hand2[s].Type\n;
  86. s++;
  87. }
  88. }
  89. };
  90.  
  91. void main()
  92. {
  93. Deck a;
  94. a.shuffle; //4 tests to show 4 outputs of dealing through the whole card process.
  95. a.deal;
  96. a.showdeck;
  97. a.shuffle;
  98. a.deal;
  99. a.showdeck;
  100. a.shuffle;
  101. a.deal;
  102. a.showdeck;
  103. a.shuffle;
  104. a.deal;
  105. a.showdeck;
  106. }
can someone please help me.
Last edited by Ancient Dragon; Oct 9th, 2008 at 8:04 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: card game: just need to know why its not compiling...

 
0
  #2
Oct 9th, 2008
There are loads of errors:

- public / private blocks shouldn't be inside methods, only inside classes ( separating variables / methods ).

- function calls look like this a.shuffle() , not like this a.shuffle .

- the showdeck method seems to be dependant on the variables in deal, that's not going to work.

- its int main() , int main( void ) or int main( int argc, char * argv[ ] ) . not void main() .

- this:
  1. deck temp = deck[i]; //swap the values
  2. deck[i] = deck[r];
  3. deck[r] = deck temp;
should probably be:
  1. Card temp = deck[i]; //swap the values
  2. deck[i] = deck[r];
  3. deck[r] = temp;
or better yet: std::swap( deck[i], deck[r] ); .

- You need to qualify access to the enum values in Card, e.g.: deck[i].Type = diamond; should be deck[i].Type = Card::diamond; .

- You have "\n" in places where it's not supposed to be:
  1. cout<<hand2[s].Type\n;
perhaps you mean:
  1. cout<<hand2[s].Type << "\n";
Last edited by MattEvans; Oct 9th, 2008 at 4:51 am.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 11
Reputation: avillachandok is an unknown quantity at this point 
Solved Threads: 0
avillachandok avillachandok is offline Offline
Newbie Poster

Re: card game: just need to know why its not compiling...

 
0
  #3
Oct 9th, 2008
hi thanks for the help. I made all these changes and I think its fine but somehow it still isn't running. it gives me an error saying Error 1 fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory..tried looking it up..can't figure it out..im new to this but i made an empty project..with win32 console application.(thats the requirement for my class so can't change those settings). any idea what its hinting at?
  1. #include "stdafx.h"
  2. #include<iostream>
  3. #include<cstdlib>
  4. using namespace std;
  5.  
  6. class Card
  7. {
  8. public: enum Suite
  9. {
  10. diamond,
  11. club,
  12. heart,
  13. spade
  14. };
  15. int Value;
  16. Suite Type;
  17. };
  18. class Deck
  19. {
  20. public:
  21. Card deck[52];
  22. Deck() //default constructor
  23. {
  24. for(int i=0;i<52;i++)
  25. {
  26. int q = i/13;
  27. int r = i%13;
  28. deck[i].Value = r;
  29. if(q==0)
  30. deck[i].Type = Card::diamond;
  31. else if(q==1)
  32. deck[i].Type = Card::club;
  33. else if(q==2)
  34. deck[i].Type = Card::heart;
  35. else if(q==3)
  36. deck[i].Type = Card::spade;
  37. }
  38. }
  39. void shuffle()
  40. {
  41. //shuffle elements by going through the deck and swapping with another random position.
  42. for(int i=0;i<52;i++)
  43. {
  44. int r = i + (rand() % (52-i)); //random remaining positions
  45. std::swap(deck[i],deck[r]); //swap values
  46. }
  47. }
  48. void deal()
  49. {
  50. int t=0,p=0;
  51.  
  52. const int N = 6;
  53.  
  54. Card hand1[N/2];
  55. Card hand2[N/2];
  56. for(int i=0;i<N;i++)
  57. {
  58. if((N%2)==0)
  59. {
  60. hand1[t]=deck[i]; //even values of the deck are distributed to hand 1.
  61. t++;
  62. }
  63. else
  64. {
  65. hand2[p]=deck[i]; //odd values of the deck are distributed to hand 2.
  66. p++;
  67. }
  68. }
  69. showdeck(hand1[],hand2[]);
  70. }
  71. void showdeck(Card hand1[], Card hand2[])
  72. {
  73. cout<<"Player one has:\n";
  74. while(s<(N/2))
  75. {
  76. cout<<hand1[s].Value;
  77. cout<<hand1[s].Type<<endl;
  78. s++;
  79. }
  80. cout<<"Player two has:\n";
  81. while(s<(N/2))
  82. {
  83. cout<<hand2[s].Value;
  84. cout<<hand2[s].Type<<endl;
  85. s++;
  86. }
  87. }
  88. };
  89.  
  90. int main(void)
  91. {
  92. Deck a;
  93. a.shuffle(); //4 tests to show 4 outputs of dealing through the whole card process.
  94. a.deal();
  95. a.shuffle();
  96. a.deal();
  97. a.shuffle();
  98. a.deal();
  99. a.shuffle();
  100. a.deal();
  101. }
Last edited by cscgal; Oct 10th, 2008 at 11:24 am. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 35
Reputation: Evan M is an unknown quantity at this point 
Solved Threads: 4
Evan M's Avatar
Evan M Evan M is offline Offline
Light Poster

Re: card game: just need to know why its not compiling...

 
0
  #4
Oct 9th, 2008
Remove #include "stdafx.h" .
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 11
Reputation: avillachandok is an unknown quantity at this point 
Solved Threads: 0
avillachandok avillachandok is offline Offline
Newbie Poster

Re: card game: just need to know why its not compiling...

 
0
  #5
Oct 10th, 2008
ok i removed that "stdafx.h"
and fixed some other tiny declaration errors but and it complies now but i've a run time error saying that stack around hand1 was corrupted...and it shows me 3 values for player1 and one of them is 104...which is about 52..plus it doesn't show me the suite. what am i doing wrong now.
sorry to keep asking.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: card game: just need to know why its not compiling...

 
0
  #6
Oct 10th, 2008
Post the current ( compiling ) version of your code. Also post the error message you receive.

Please use code tags ( http://www.daniweb.com/forums/misc-explaincode.html ) whenever posting code here.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 11
Reputation: avillachandok is an unknown quantity at this point 
Solved Threads: 0
avillachandok avillachandok is offline Offline
Newbie Poster

Re: card game: just need to know why its not compiling...

 
0
  #7
Oct 10th, 2008
This is my code
  1. [#include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4.  
  5. class Card
  6. {
  7. public: enum Suite
  8. {
  9. diamond,
  10. club,
  11. heart,
  12. spade
  13. };
  14. int Value;
  15. Suite Type;
  16. };
  17. class Deck
  18. {
  19. public:
  20. Card deck[52];
  21. Deck() //default constructor
  22. {
  23. for(int i=0;i<52;i++)
  24. {
  25. int q = i/13;
  26. int r = i%13;
  27. deck[i].Value = r;
  28. if(q==0)
  29. deck[i].Type = Card::diamond;
  30. else if(q==1)
  31. deck[i].Type = Card::club;
  32. else if(q==2)
  33. deck[i].Type = Card::heart;
  34. else if(q==3)
  35. deck[i].Type = Card::spade;
  36. }
  37. }
  38. void shuffle()
  39. {
  40. //shuffle elements by going through the deck and swapping with another random position.
  41. for(int i=0;i<52;i++)
  42. {
  43. int r = i + (rand() % (52-i)); //random remaining positions
  44. std::swap(deck[i],deck[r]); //swap values
  45. }
  46. }
  47. void deal()
  48. {
  49. int t=0,p=0;
  50. const int N=6;
  51. Card hand1[(N/2)];
  52. Card hand2[(N/2)];
  53. for(int i=0;i<N;i++)
  54. {
  55. if((N%2)==0)
  56. {
  57. hand1[t]=deck[i]; //even values of the deck are distributed to hand 1.
  58. t++;
  59. }
  60. else
  61. {
  62. hand2[p]=deck[i]; //odd values of the deck are distributed to hand 2.
  63. p++;
  64. }
  65. }
  66. showdeck(hand1,hand2,N);
  67. }
  68. void showdeck(Card hand1[], Card hand2[],int N)
  69. {
  70. int s =0;
  71. cout<<"Player one has:\n";
  72. while(s<(N/2))
  73. {
  74. cout<<hand1[s].Value;
  75. cout<<hand1[s].Type<<endl;
  76. s++;
  77. }
  78. cout<<"Player two has:\n";
  79. while(s<(N/2))
  80. {
  81. cout<<hand2[s].Value;
  82. cout<<hand2[s].Type<<endl;
  83. s++;
  84. }
  85. }
  86. };
  87.  
  88. int main(void)
  89. {
  90. Deck a;
  91. a.shuffle(); //4 tests to show 4 outputs of dealing through the whole card process.
  92. a.deal();
  93. a.shuffle();
  94. a.deal();
  95. a.shuffle();
  96. a.deal();
  97. a.shuffle();
  98. a.deal();
  99. }
It shows me an error message saying "Debug Error!
Program:...ments/Visual Studio
2008\Project\cardgame\Debug\cardgame.exe
Module:...ments\Visual Studio
2008\Projects\cardgame\Debug\cardgame.exe
File:
Run-Time Check Failure#2 - Stack around the variable 'hand1' was corrupted.
(Press Retry to debug the application)"
Last edited by cscgal; Oct 10th, 2008 at 7:45 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,756
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: card game: just need to know why its not compiling...

 
0
  #8
Oct 10th, 2008
As previously mentioned, please use code tags. All formatting is stripped out otherwise.


[code]
// paste code here
[/code]


or C++-specific code tags, with line numbers and syntax highlighting:


[code=cplusplus]
// paste code here
[/code]
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: card game: just need to know why its not compiling...

 
0
  #9
Oct 10th, 2008
void deal()
{
  int t=0,p=0;
  const int N=6;
  Card hand1[(N/2)];
  Card hand2[(N/2)];
  for(int i=0;i<N;i++)
  {
    //! since N is a constant, (N%2) will always be constant ( zero ): thus 't' will constantly be incremented, and the stack will get screwed up as writes are attempted outside of the allocated space for 'hand1'. you probably mean (i%2).
    if((N%2)==0)
    {
      hand1[t]=deck[i]; //even values of the deck are distributed to hand 1.
      t++;
    }
    else
    {
      hand2[p]=deck[i]; //odd values of the deck are distributed to hand 2.
      p++;
    }
  }
  showdeck(hand1,hand2,N);
}
void showdeck(Card hand1[], Card hand2[],int N)
{
  int s =0;
  cout<<"Player one has:\n";
  while(s<(N/2))
  {
    cout<<hand1[s].Value;
    cout<<hand1[s].Type<<endl;
    s++;
  }
  //! you don't reset 's' to zero here, so the following loop will never start. 
  cout<<"Player two has:\n";
  while(s<(N/2))
  {
    cout<<hand2[s].Value;
    cout<<hand2[s].Type<<endl;
    s++;
  }
}
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 11
Reputation: avillachandok is an unknown quantity at this point 
Solved Threads: 0
avillachandok avillachandok is offline Offline
Newbie Poster

Re: card game: just need to know why its not compiling...

 
0
  #10
Oct 10th, 2008
ok now the program is running but the 3 numbers printed out per hand, some numbers still come above 52 which shouldnt' be the case. Also I'm trying to print the enum values with the numbers.
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4.  
  5. class Card
  6. {
  7. public: enum Suite
  8. {
  9. diamond,
  10. club,
  11. heart,
  12. spade
  13. };
  14. int Value;
  15. Suite Type;
  16. };
  17. class Deck
  18. {
  19. public:
  20. Card deck[52];
  21. Deck() //default constructor
  22. {
  23. for(int i=0;i<52;i++)
  24. {
  25. int q = i/13;
  26. int r = i%13;
  27. deck[i].Value = r;
  28. if(q==0)
  29. deck[i].Type = Card::diamond;
  30. else if(q==1)
  31. deck[i].Type = Card::club;
  32. else if(q==2)
  33. deck[i].Type = Card::heart;
  34. else if(q==3)
  35. deck[i].Type = Card::spade;
  36. }
  37. }
  38. void shuffle()
  39. {
  40. //shuffle elements by going through the deck and swapping with another random position.
  41. for(int i=0;i<52;i++)
  42. {
  43. int r = i + (rand() % (52-i)); //random remaining positions
  44. std::swap(deck[i],deck[r]); //swap values
  45. }
  46. }
  47. void deal()
  48. {
  49. int t=0,p=0;
  50. const int N=6;
  51. Card hand1[(N/2)];
  52. Card hand2[(N/2)];
  53. for(int i=0;i<N;i++)
  54. {
  55. if((i%2)==0)
  56. {
  57. hand1[t]=deck[i]; //even values of the deck are distributed to hand 1.
  58. t++;
  59. }
  60. else
  61. {
  62. hand2[p]=deck[i]; //odd values of the deck are distributed to hand 2.
  63. p++;
  64. }
  65. }
  66. showdeck(hand1,hand2,N);
  67. }
  68. void showdeck(Card hand1[], Card hand2[],int N)
  69. {
  70. int s =0;
  71. cout<<"Player one has:\n";
  72. while(s<(N/2))
  73. {
  74. cout<<hand1[s].Value;
  75. cout<<hand1[s].Type<<endl;
  76. s++;
  77. }
  78. s = 0;
  79. cout<<"Player two has:\n";
  80. while(s<(N/2))
  81. {
  82. cout<<hand2[s].Value;
  83. cout<<hand2[s].Type<<endl;
  84. s++;
  85. }
  86. }
  87. };
  88.  
  89. int main(void)
  90. {
  91. Deck a;
  92. a.shuffle(); //4 tests to show 4 outputs of dealing through the whole card process.
  93. a.deal();
  94. a.shuffle();
  95. a.deal();
  96. a.shuffle();
  97. a.deal();
  98. a.shuffle();
  99. a.deal();
  100. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC