| | |
card game: just need to know why its not compiling...
![]() |
•
•
Join Date: Oct 2008
Posts: 11
Reputation:
Solved Threads: 0
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...
can someone please help me.
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...
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<cstdlib> using namespace std; class Card { public: enum Suite { diamond, club, heart, spade }; int Value; Suite Type; }; class Deck { public: Card deck[52]; Deck() //default constructor { for(int i=0;i<52;i++) { int q = i/13; int r = i%13; deck[i].Value = r; if(q==0) deck[i].Type = diamond; else if(q==1) deck[i].Type = club; else if(q==2) deck[i].Type = heart; else if(q==3) deck[i].Type = spade; } } void shuffle() { //shuffle elements by going through the deck and swapping with another random position. public: for(int i=0;i<52;i++) { int r = i + (rand() % (52-i)); //random remaining positions deck temp = deck[i]; //swap the values deck[i] = deck[r]; deck[r] = deck temp; } } void deal() { int t=0,p=0; public: const int N = 6; Card hand1[N/2]; Card hand2[N/2]; private: for(int i=0;i<N;i++) { 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++; } } } void showdeck() { cout<<"Player one has:\n"; while(s<(N/2)) { cout<<hand1[s].Value; cout<<hand1[s].Type\n; s++; } cout<<"Player two has:\n"; while(s<(N/2)) { cout<<hand2[s].Value; cout<<hand2[s].Type\n; s++; } } }; void main() { Deck a; a.shuffle; //4 tests to show 4 outputs of dealing through the whole card process. a.deal; a.showdeck; a.shuffle; a.deal; a.showdeck; a.shuffle; a.deal; a.showdeck; a.shuffle; a.deal; a.showdeck; }
Last edited by Ancient Dragon; Oct 9th, 2008 at 8:04 am. Reason: add code tags
There are loads of errors:
- public / private blocks shouldn't be inside methods, only inside classes ( separating variables / methods ).
- function calls look like this
- the showdeck method seems to be dependant on the variables in deal, that's not going to work.
- its
- this:
should probably be:
or better yet:
- You need to qualify access to the enum values in Card, e.g.:
- You have "\n" in places where it's not supposed to be:
perhaps you mean:
- 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:
C++ Syntax (Toggle Plain Text)
deck temp = deck[i]; //swap the values deck[i] = deck[r]; deck[r] = deck temp;
C++ Syntax (Toggle Plain Text)
Card temp = deck[i]; //swap the values deck[i] = deck[r]; deck[r] = temp;
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:
C++ Syntax (Toggle Plain Text)
cout<<hand2[s].Type\n;
C++ Syntax (Toggle Plain Text)
cout<<hand2[s].Type << "\n";
Last edited by MattEvans; Oct 9th, 2008 at 4:51 am.
Plato forgot the nullahedron..
•
•
Join Date: Oct 2008
Posts: 11
Reputation:
Solved Threads: 0
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?
C++ Syntax (Toggle Plain Text)
#include "stdafx.h" #include<iostream> #include<cstdlib> using namespace std; class Card { public: enum Suite { diamond, club, heart, spade }; int Value; Suite Type; }; class Deck { public: Card deck[52]; Deck() //default constructor { for(int i=0;i<52;i++) { int q = i/13; int r = i%13; deck[i].Value = r; if(q==0) deck[i].Type = Card::diamond; else if(q==1) deck[i].Type = Card::club; else if(q==2) deck[i].Type = Card::heart; else if(q==3) deck[i].Type = Card::spade; } } void shuffle() { //shuffle elements by going through the deck and swapping with another random position. for(int i=0;i<52;i++) { int r = i + (rand() % (52-i)); //random remaining positions std::swap(deck[i],deck[r]); //swap values } } 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++) { 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[]); } void showdeck(Card hand1[], Card hand2[]) { cout<<"Player one has:\n"; while(s<(N/2)) { cout<<hand1[s].Value; cout<<hand1[s].Type<<endl; s++; } cout<<"Player two has:\n"; while(s<(N/2)) { cout<<hand2[s].Value; cout<<hand2[s].Type<<endl; s++; } } }; int main(void) { Deck a; a.shuffle(); //4 tests to show 4 outputs of dealing through the whole card process. a.deal(); a.shuffle(); a.deal(); a.shuffle(); a.deal(); a.shuffle(); a.deal(); }
Last edited by cscgal; Oct 10th, 2008 at 11:24 am. Reason: Added code tags
•
•
Join Date: Oct 2008
Posts: 11
Reputation:
Solved Threads: 0
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.
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.
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.
Please use code tags ( http://www.daniweb.com/forums/misc-explaincode.html ) whenever posting code here.
Plato forgot the nullahedron..
•
•
Join Date: Oct 2008
Posts: 11
Reputation:
Solved Threads: 0
This is my code
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)"
C++ Syntax (Toggle Plain Text)
[#include<iostream> #include<cstdlib> using namespace std; class Card { public: enum Suite { diamond, club, heart, spade }; int Value; Suite Type; }; class Deck { public: Card deck[52]; Deck() //default constructor { for(int i=0;i<52;i++) { int q = i/13; int r = i%13; deck[i].Value = r; if(q==0) deck[i].Type = Card::diamond; else if(q==1) deck[i].Type = Card::club; else if(q==2) deck[i].Type = Card::heart; else if(q==3) deck[i].Type = Card::spade; } } void shuffle() { //shuffle elements by going through the deck and swapping with another random position. for(int i=0;i<52;i++) { int r = i + (rand() % (52-i)); //random remaining positions std::swap(deck[i],deck[r]); //swap values } } 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++) { 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++; } cout<<"Player two has:\n"; while(s<(N/2)) { cout<<hand2[s].Value; cout<<hand2[s].Type<<endl; s++; } } }; int main(void) { Deck a; a.shuffle(); //4 tests to show 4 outputs of dealing through the whole card process. a.deal(); a.shuffle(); a.deal(); a.shuffle(); a.deal(); a.shuffle(); a.deal(); }
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
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..
•
•
Join Date: Oct 2008
Posts: 11
Reputation:
Solved Threads: 0
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.
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<cstdlib> using namespace std; class Card { public: enum Suite { diamond, club, heart, spade }; int Value; Suite Type; }; class Deck { public: Card deck[52]; Deck() //default constructor { for(int i=0;i<52;i++) { int q = i/13; int r = i%13; deck[i].Value = r; if(q==0) deck[i].Type = Card::diamond; else if(q==1) deck[i].Type = Card::club; else if(q==2) deck[i].Type = Card::heart; else if(q==3) deck[i].Type = Card::spade; } } void shuffle() { //shuffle elements by going through the deck and swapping with another random position. for(int i=0;i<52;i++) { int r = i + (rand() % (52-i)); //random remaining positions std::swap(deck[i],deck[r]); //swap values } } 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++) { if((i%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++; } s = 0; cout<<"Player two has:\n"; while(s<(N/2)) { cout<<hand2[s].Value; cout<<hand2[s].Type<<endl; s++; } } }; int main(void) { Deck a; a.shuffle(); //4 tests to show 4 outputs of dealing through the whole card process. a.deal(); a.shuffle(); a.deal(); a.shuffle(); a.deal(); a.shuffle(); a.deal(); }
![]() |
Similar Threads
- card game (Java)
- Playing cards (C++)
Other Threads in the C++ Forum
- Previous Thread: RichEdit subclassing problem, can't call Default Proc.
- Next Thread: Number Guesssing Game Help
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






