Sign in to follow this  

What do you think

This topic is 3310 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Still working on my BlackJack Program (realy need to set around more time), and i have well basicly this so far - just to get the two PlayerCArd values (will be adited to get rid of the tester stuff like the number (yes i need to figure out the debugger aswell). Was jsut wondering if this was a good way of doing this or if there are "Better" ways - i know that system pause isnt a good idea ill try and sort that lol. Probaly shoudl ahve my class with its own header and source: other than that give feedback please. #include "stdafx.h" #include <iostream> #include <ctime> #include "stdlib.h" using namespace std; int Random_Number; int Deck[52] = {1,2,3,4,5,6,7,8,9,10,10,10,10,1,2,3,4,5,6,7,8,9,10,10,10,10,1,2,3,4,5,6,7,8,9,10,10,10,10,1,2,3,4,5,6,7,8,9,10,10,10,10}; class Card { public: int Get_Value() { Value = Deck[Random_Number]; return Value; } int Value; }; int _tmain(int argc, _TCHAR* argv[]) { srand((unsigned)time(0)); Random_Number = (rand()%51); Card PlayerCard; Card *PointerPlayerCard; PointerPlayerCard = &PlayerCard; PlayerCard.Get_Value(); char(Choice); std::cout << "Your first Card Value is:\t" << PlayerCard.Value << endl; std::cout << "Your First Random_Number was:\t" << Random_Number << endl; std::cout << "Type y to continue" << endl; std::cin >> Choice; if (Choice == 121) { Random_Number = (rand()%51); Card PlayerCard2; Card *PointerPlayerCard2; PointerPlayerCard2 = &PlayerCard2; PlayerCard2.Get_Value(); std::cout << "Your second Card Value is:\t" << PlayerCard2.Value << endl; std::cout << "Your second Random_Number was:\t" << Random_Number << endl; } system ("PAUSE"); return 0; } cheers Catkill

Share this post


Link to post
Share on other sites
Hey, looks good. Here's afew things I noticed..

Deck[52] & Value probarbly belong in your card class as a private members, and then pass in Random_Number (which you can declare in main instead) as a parameter to GetValue, and store the return Value. like


class Card
{
public:
int Get_Value( int Random_Number ) //added param here
{
Value = Deck[Random_Number];
return Value;
}
private:
int Value; //make Value private instead of public
int Deck[52] = { .... }; //put Deck as private member of Card class
};



so in the original code Get_Value returns the Value of the card, but after you call it you don't store the return value anywhere, and then access Card.Value directly later to get the value. Now that it's private how about:


int _tmain(int argc, _TCHAR* argv[])
{
int Random_Number; //declared here instead of global
srand((unsigned)time(0));
Random_Number = (rand()%51);
Card PlayerCard;
Card *PointerPlayerCard; // } no idea what this is for??
PointerPlayerCard = &PlayerCard; // }
int cardValue = PlayerCard.Get_Value(); //store the return value


char(Choice);
std::cout << "Your first Card Value is:\t" << cardValue << endl;
std::cout << "Your First Random_Number was:\t" << Random_Number << endl;
std::cout << "Type y to continue" << endl;
std::cin >> Choice;


if (Choice == 'y') //use the char 'y' instead of 121
{
Random_Number = (rand()%51);
Card PlayerCard2;
Card *PointerPlayerCard2;
PointerPlayerCard2 = &PlayerCard2;
int cardValue2 = PlayerCard2.Get_Value();


etc...
}



So changing it around like this means 1) no more global variables, which are generally considered bad practice 2) we've 'encapsulated' data inside the Card class, which is a key concept of C++.

Share this post


Link to post
Share on other sites
When i add my Deck array into the Card class it doesnt compile (undeclered identifier i think). Any ideas? Also can anyone explain basicly how to call the classes easier as you say you dont knwo what all those pointers and stuff are for , me neither to be honest , i just saw it in an example - it worked when the other way i tried didnt lol

Cheers
Catkill

Share this post


Link to post
Share on other sites
Ok well answer your first querstion then attempt a brief answer to the second.

First off the undeclared iddentifer deppending on what line fo code it is on is because where the scope where the error is ther eis no such variable ro class delaration eg



//A function
void Do()
{
int Test=0; //This variable only exists in this function
}

//So if wtryed to go like this

Test = 40; //We would get your error

//Also if we define a varable after we try and use like so

int Test1 = Test2;
int Test2 = 5;

//The same error would occur because the test2 variable would not exist yet.

//In the case of class if you try and access a varable like the deck varable
//like so

class Card
{
int Deck[52];
}

//You cannot access it like this because that variable does not exist
Deck

//This is how you could access it

Card Cards;
Cards.Deck[12] = 10;

//Bascialy scope is important just go to the line where the error is and check
//To see if you are trying to access somethign that is not there




As for the second question thats abit more of a borad question but ill give you a very basica low down.

A pointer is exactly as it sounds it point to a memorie location in your computer where a variable is stored. This is represented by the * symbol

A refrence is essentaly the samething but is used in arguments generaly

I wont go in anymore depth then that but it is very important that you read and reread pointers ether via tutorials or better yet an actualy articule tell yyou understand them they ARE that IMPORTANT and if used inccorectly hazerdas and can in some very rare instances do damamage

Best Regards Jouei.

Share this post


Link to post
Share on other sites

This topic is 3310 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this