New programmer having a problem with calsses
I am writing a card program for my C++ class that uses two classes. It creates a deck of cards and shuffles them. A user picks a card and the computer picks a card. If the user picks a higher card number he wins and if not he loses. I can create the deck and shuffle it fine.
I am trying to make main as small as possible by utilizing the two classes one for cards and the other for the game. I am struggling on having the two classes work together to display the card the user and computer picks and passing the chosen cards to member functions of the game class. I am using an array of objects for my deck and accessing them and sending them to Card::Display().
Many of the compile errors deal with accessing private data between the two classes. I know I either do not have my game class set up right or I am passing data the wrong way. If anyone could point me in the right direction I would really appreciate it. Thanks.
the fallowing is my code. I hope is is easy to understand. It compiles correctly but does not display the card.
#include <iostream.h>
#include <stdlib.h>
#include <ctime>
using namespace std;
//Creating number system for cards
const int Jack = 11;
const int Queen = 12;
const int King = 13;
const int Ace = 14;
enum Suit {Spades, Hearts, Diamonds, Clubs};
//Individual Card
class Card
{
private:
int number;
Suit suit;
public:
Card()
{}
void setcard(int card, Suit s)//Sets card into Deck
{number = card; suit = s;}
Card CreatDeck(Card TempDeck[52]);
void Display();
};
class Game //Class for game
{
private:
public:
Game()
{ }
int CardChoice(int u, int c)//Aloows a user to choose a card
{
do{cout << "Pick a card from 0 to 52: ";
cin >> u;}
while(u < 0 || u > 52);
do{c = rand() % 52;}
while(c == u);
return u, c;
}
};
Card Card::CreatDeck(Card TempDeck[52])//creates deck and shuffles the deck
{
int a;
for (a=0; a<52; a++)//Ordered Deck
{
int c = (a%13)+2;
Suit s = Suit(a/13);
TempDeck[a].setcard(c, s);
}
for (a=0; a<52; a++)//Shuffled Deck
{
int t = rand() % 52;
Card temp = TempDeck[a];
TempDeck[a] = TempDeck[t];
TempDeck[t] = temp;
}
return TempDeck[52];
};
void Card::Display()//display the card
{
if( number >= 2 && number <= 10 )
cout << number;
else
switch(number)
{
case Jack: cout << "J"; break;
case Queen: cout << "Q"; break;
case King: cout << "K"; break;
case Ace: cout << "A"; break;
}
switch(suit)
{
case Clubs: cout << static_cast<char>(5); break;
case Diamonds: cout << static_cast<char>(4); break;
case Hearts: cout << static_cast<char>(3); break;
case Spades: cout << static_cast<char>(6); break;
}
}
int main()
{
int User, Computer;
User = 0; Computer = 0;
srand(time(NULL)); //Initalization of Random Number Generator
Card Deck[52]; //Creats array of Card objects
Game HighCard; //Creats Game
Card CreatDeck(Deck[52]);//Function Call
HighCard.CardChoice(User, Computer);
Deck[User].Display();
Deck[Computer].Display();
system("PAUSE");
return 0;
}
# 1 Re: New programmer having a problem with calsses
It compiles correctlyNo it doesn't:
#include <iostream.h>
The correct header is <iostream>, not <iostream.h>. An ANSI compliant C++ compiler will not compile even this line, since there is no such header as <iostream.h> (for example, the later versions of Visual C++ do not have <iostream.h>).
Hopefully this is a typo, and you were not taught to include this header.
Regards,
Paul McKenzie
# 2 Re: New programmer having a problem with calsses
1)
return u, c;
What are you intending to do here? Return two values? Or do you really know what the comma operator does, and intend to do this? More to the point, what do you think that line of code does?
2) In your Card Card::CreatDeck function, don't end your function with a semicolon. Even though it may compile, it looks amatuerish to end functions with semicolons.
3)
Card CreatDeck(Deck[52]);//Function Call
Deck is an array of 52. The legal indices are then 0 - 51, so sending Deck[52] is an error, since there is no Deck[52]. But I have to ask again, what are you intending to do with that line of code? Better yet, I'll have you explain what that line does.
Similarly, you have this:
return TempDeck[52];
TempDeck is an array of 52, with indices numbered 0 - 51. There is no TempDeck[52].
4) When saying you have a compiler error, please including the compiler and version of the compiler that you're using. There are many compilers and they will or will not produce the same errors or warnings that you're seeing. Also, post the compiler errors that you're getting.
Overall, I believe you should read up more on arrays and how they are passed to functions. You have many things wrong with the way you're declaring and returning arrays. Even though your code will compile at some point, it won't run properly.
Regards,
Paul McKenzie
# 3 Re: New programmer having a problem with calsses
You compute user's and computer's card, but you never assign the values to Deck[user] or Deck[computer]. Thus Display has nothing to display because Card's number and suit are uninitialized.
A simple debug session could have shown you ;-)
# 4 Re: New programmer having a problem with calsses
1)
return u, c;
What are you intending to do here? Return two values? Or do you really know what the comma operator does, and intend to do this? More to the point, what do you think that line of code does?
2) In your Card Card::CreatDeck function, don't end your function with a semicolon. Even though it may compile, it looks amatuerish to end functions with semicolons.
3)
Card CreatDeck(Deck[52]);//Function Call
Deck is an array of 52. The legal indices are then 0 - 51, so sending Deck[52] is an error, since there is no Deck[52]. But I have to ask again, what are you intending to do with that line of code? Better yet, I'll have you explain what that line does.
Similarly, you have this:
return TempDeck[52];
TempDeck is an array of 52, with indices numbered 0 - 51. There is no TempDeck[52].
4) When saying you have a compiler error, please including the compiler and version of the compiler that you're using. There are many compilers and they will or will not produce the same errors or warnings that you're seeing. Also, post the compiler errors that you're getting.
Overall, I believe you should read up more on arrays and how they are passed to functions. You have many things wrong with the way you're declaring and returning arrays. Even though your code will compile at some point, it won't run properly.
Regards,
Paul McKenzie
1. I wish to return the users an computers choice of a card back to main so I can send those particular objects to Dispaly().
HighCard.CardChoice(User, Computer);
Deck[User].Display();
Deck[Computer].Display();
3. I am trying to send my Deck array to CreateDeck() so it can make the individual cards and then shuffle those cards and then return Deck back to main. I read up on arrays and I cannot seem to grasp passing the objects in my array to memeber functions.
4. My compiler is Dev-C++
# 5 Re: New programmer having a problem with calsses
You need to change your header files, as already mentioned.
The correct syntax is
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
# 6 Re: New programmer having a problem with calsses
I have went back and rewrote my code for my program. I am still stuck on comparing the two cards that the user and computer select. It is much easier to fallow now.
#include <iostream>
#include <stdlib.h>
#include <ctime>
//Creating number system for cards
const int Jack = 11;
const int Queen = 12;
const int King = 13;
const int Ace = 14;
enum Suit {Spades, Hearts, Diamonds, Clubs};
class Card //Indivdual cards
{
private:
int number;
Suit suit;
public:
Card()
{ }
void setcard(int card, Suit s)//Sets card to Deck
{number = card; suit = s;}
void Display();
};
class Game //Here is the functions for running the game
{
private:
Card Deck[52];
Card Ucard, Ccard;
public:
void ArrangeDeck();
void SelectCards();
};
//------------------------
void Game::ArrangeDeck()//Makes a Deck of Cards and Shuffles them
{
int a;
for (a=0; a<52; a++)//Ordered Deck
{
int c = (a%13)+2;
Suit s = Suit(a/13);
Deck[a].setcard(c, s);
}
for (a=0; a<52; a++)//Shuffled Deck
{
int t = rand() % 52;
Card temp = Deck[a];
Deck[a] = Deck[t];
Deck[t] = temp;
}
}
//------------------------
void Game::SelectCards()//Lets the User and Computer sleect a card to play
{
int u, c;
do{//User Selects his card
cout << "Please pick a Card from 0 to 52: ";
cin >> u;
}while(u<0 || u>52);
do{//Computer Selects his card
c=rand()%52;
}while(c == u);
Ucard = Deck[u];
Ccard = Deck[c];
Ucard.Display();
Ccard.Display();
//For Some reason I can display the cards, but not compare the 'number' data memeber
}
//------------------------
void Card::Display()//Displays the Cards slected by the User and Computer
{
if( number >= 2 && number <= 10 )
cout << number;
else
switch(number)
{
case Jack: cout << "J"; break;
case Queen: cout << "Q"; break;
case King: cout << "K"; break;
case Ace: cout << "A"; break;
}
switch(suit)
{
case Clubs: cout << static_cast<char>(5); break;
case Diamonds: cout << static_cast<char>(4); break;
case Hearts: cout << static_cast<char>(3); break;
case Spades: cout << static_cast<char>(6); break;
}
}
int main()
{
srand(time(NULL));
Game Play;
Play.ArrangeDeck();
Play.SelectCards();
system("PAUSE");
return 0;
}
If the class Game contains an object of type card (e.g. uCard) Can 'Ucard' access the private data inside the 'Card' class since it is of class 'Card'?
I would appreciate anyhelp.
# 7 Re: New programmer having a problem with calsses
I hate to beat a dead horse with a stick, but the headers still aren't right.
Don't even see how this could compile, you didn't even use the std namespace.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//Creating number system for cards
const int Jack = 11;
const int Queen = 12;
const int King = 13;
const int Ace = 14;
enum Suit {Spades, Hearts, Diamonds, Clubs};
class Card //Indivdual cards
{
private:
int number;
Suit suit;
public:
Card()
{ }
void setcard(int card, Suit s)//Sets card to Deck
{number = card; suit = s;}
void Display();
};
class Game //Here is the functions for running the game
{
private:
Card Deck[52];
Card Ucard, Ccard;
public:
void ArrangeDeck();
void SelectCards();
};
//------------------------
void Game::ArrangeDeck()//Makes a Deck of Cards and Shuffles them
{
int a;
for (a=0; a<52; a++)//Ordered Deck
{
int c = (a%13)+2;
Suit s = Suit(a/13);
Deck[a].setcard(c, s);
}
for (a=0; a<52; a++)//Shuffled Deck
{
int t = rand() % 52;
Card temp = Deck[a];
Deck[a] = Deck[t];
Deck[t] = temp;
}
}
//------------------------
void Game::SelectCards()//Lets the User and Computer sleect a card to play
{
int u, c;
do{//User Selects his card
cout << "Please pick a Card from 0 to 52: ";
cin >> u;
}while(u<0 || u>52);
do{//Computer Selects his card
c=rand()%52;
}while(c == u);
Ucard = Deck[u];
Ccard = Deck[c];
Ucard.Display();
Ccard.Display();
//For Some reason I can display the cards, but not compare the 'number' data memeber
}
//------------------------
void Card::Display()//Displays the Cards slected by the User and Computer
{
if( number >= 2 && number <= 10 )
cout << number;
else
switch(number)
{
case Jack: cout << "J"; break;
case Queen: cout << "Q"; break;
case King: cout << "K"; break;
case Ace: cout << "A"; break;
}
switch(suit)
{
case Clubs: cout << static_cast<char>(5); break;
case Diamonds: cout << static_cast<char>(4); break;
case Hearts: cout << static_cast<char>(3); break;
case Spades: cout << static_cast<char>(6); break;
}
}
int main()
{
srand(time(NULL));
Game Play;
Play.ArrangeDeck();
Play.SelectCards();
system("PAUSE");
return 0;
}
If the class Game contains an object of type card (e.g. uCard) Can 'Ucard' access the private data inside the 'Card' class since it is of class 'Card'?
I would appreciate anyhelp.
It depends on what you mean by access.
If you mean "Can you directly access the private data of class card?"
No, it could not. Only public member functions ( or friends ) of class Card
can directly access it's private data members.
So in your game class, you could not do this...
Card someObject;
someObject.number = 1;
someObject.suit = Clubs;
You would need to use the setcard method...
Card someObject;
someObject.setcard( 1, Clubs );
# 8 Re: New programmer having a problem with calsses
Finally I am done. Thank you everyone for your help today. I really appreciate you helping a guy who can't get his headers straight. Although in my defense Dev-C++ the compiler I am using compiled the code fine that is why I didn't realize it the first time. The second time was my bad though. Thanks dcjr84 for giving me an example to build on.