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...

#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;
}

can someone please help me.

Recommended Answers

All 11 Replies

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:

deck temp = deck[i]; //swap the values
deck[i] = deck[r];
deck[r] = deck temp;

should probably be:

Card temp = deck[i]; //swap the values
deck[i] = deck[r];
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:

cout<<hand2[s].Type\n;

perhaps you mean:

cout<<hand2[s].Type << "\n";

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?

#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();
}

Remove #include "stdafx.h" .

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.

This is my code

[#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();
}

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)"

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]

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++;
  }
}

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.

#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();
}

Oh. I think the output of your program is confusing you... When you print an enum value ( using iostream operator<< ) it prints the integer value of the enum.. so when your program outputs:

Player one has:
31

this is a 3, and a 1 stuck together: not a 31.

so that's the 3 of clubs... output a space inbetween the two numbers =).. eg.

cout<<hand1[s].Value<<" ";
cout<<hand1[s].Type<<endl;

to print an enums "tag", you pretty much have to do:

switch ( value )
{
  case Card::diamond: 
    std::cout << "diamond";
    break;
  case Card::club:
    ...etc..
}

ok. i got the program to run finally.
now i've this extention to the program to make an algorithm(written)..
so this is the question:

Create an Object Oriented design for a two player card game.
Repeat 26 times(each time is a turn)
play one card from each hand to the table.
compare the two cards
if they have the same no return them to the bottom of each hand
if one is larger take both cards and put them on the bottom of the hand from which the larger card came.

declare the hand with the most cards the winner
deal with ties
end game.

so for the design i will change the array into a linked list..or some other data structure in which i can access the top of the pile and still be able to count the size of the stack.
make a turn method in the deck class and pass in an int parameter so the loop inside knows how many times to run.
in the loop compare the top values of the two player lists and accordingly perform moving both cads back in their piles(if tied), move both the compared cars (to the pile which had teh bigger card) etc...
at the end of the loop return to the main function and count the length and declare a winner or deal again until the lenth of the array is unequal. and then declare winner.

so my extention to the program is:
Add at the start of the game the ability for theuser to play two different ways:
1. the method described above(so i'll put all of the above in a switch format)
2. the following changes/additions are made:
Cards are played on to the table out of the players hands.
That instead of 26 turns, the game continues until at any point in the game if a player has less than 5 cards in their hand they are declared the loser, or one of the players concedes.
If during a turn there is a tie, take three additional cards from each hand and put them on the table, and compare the last of the three with the larger number getting all of the cards played during that turn. This may have to be repeated if the last of the three in each hand is the same number.

So my problem is how do i design this part? specifically what do they mean cards are played on to the table of the the players hand??do i need a player class??
can I use the same turn method or do i have to make another one..
really need help thanks.

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.