Name Of Argument

A

A_StClaire_

dudes,

say I have a Hand object named X and feed it into the function below.
how do I code it so the message "Dealing card to X..." comes up?
'cause I know the way it is now doesn't work.


void random_deal(Hand& H) {
srand((unsigned)time(0));
cout << "Dealing card to " << H << "...";
cin.get();
int random_int = 0;
int lowest = 0;
int highest = 51;
int range = highest - lowest + 1;

for(int index = 0; index < 20; index++) {
random_int = lowest + (range * rand() /
(RAND_MAX + 1));
}

vector <Card>::iterator pos = cards_in_deck.begin();
pos += random_int;
H.cards_in_hand.push_back(*pos);
cards_in_deck.erase(pos);
}


thx a lot
 
A

Andre Kostur

(e-mail address removed) wrote in @o13g2000cwo.googlegroups.com:
dudes,

say I have a Hand object named X and feed it into the function below.
how do I code it so the message "Dealing card to X..." comes up?
'cause I know the way it is now doesn't work.


void random_deal(Hand& H) {
srand((unsigned)time(0));
cout << "Dealing card to " << H << "...";

Overload the operator<< for the Hand class?
 
K

Kai-Uwe Bux

dudes,

say I have a Hand object named X and feed it into the function below.
how do I code it so the message "Dealing card to X..." comes up?
'cause I know the way it is now doesn't work.


void random_deal(Hand& H) {
srand((unsigned)time(0));

Do you really want to reseed by time the RNG every time you deal a card?
Usually, this will result in way less random looking behaviour.
cout << "Dealing card to " << H << "...";

do you have defined operator<< for the type Hand?
cin.get();
int random_int = 0;
int lowest = 0;
int highest = 51;
int range = highest - lowest + 1;

for(int index = 0; index < 20; index++) {
random_int = lowest + (range * rand() /
(RAND_MAX + 1));
}

a) this for() loop does not make random_int any more random.

b) this way of generating random integers in [lowest, highest] does not
quite exactly produce a uniform distribution (unless range happens to
divide RAND_MAX.

c) Are you certain that RAND_MAX+1 will not cause an arithmetic overflow?
vector <Card>::iterator pos = cards_in_deck.begin();
pos += random_int;
H.cards_in_hand.push_back(*pos);
cards_in_deck.erase(pos);
}

This approach is expensive. You might consider the following:
Before dealing the hands, shuffle the deck:

std::random_shuffle( cards_in_deck.begin(), cards_in_deck.end() );

Then, within the card dealing function, just deal the card

cards_in_deck.back();

and then get rid of it by

cards_in_deck.pop_back();



Best

Kai-Uwe Bux
 
A

A_StClaire_

Andre said:
Overload the operator<< for the Hand class?


err... plz think of me as ignorant and never as lazy. how exactly
would one go about doing that?
 
D

David Hilsee

dudes,

say I have a Hand object named X and feed it into the function below.
how do I code it so the message "Dealing card to X..." comes up?
'cause I know the way it is now doesn't work.


void random_deal(Hand& H) {
srand((unsigned)time(0));
cout << "Dealing card to " << H << "...";
cin.get();
int random_int = 0;
int lowest = 0;
int highest = 51;
int range = highest - lowest + 1;

for(int index = 0; index < 20; index++) {
random_int = lowest + (range * rand() /
(RAND_MAX + 1));
}

vector <Card>::iterator pos = cards_in_deck.begin();
pos += random_int;
H.cards_in_hand.push_back(*pos);
cards_in_deck.erase(pos);
}


thx a lot

If you wish for the following code

Hand X;
random_deal(X);

Hand Y;
random_deal(Y);

to result in "Dealing card to X..." and "Dealing card to Y...",
respectively, then the simple answer is that there is no standard way to do
that. Perhaps you should give the Hand class a "name" member to store its
name, so you could pass that name to its constructor and retrieve/display it
in random_deal.
 
A

A_StClaire_

David said:
If you wish for the following code

Hand X;
random_deal(X);

Hand Y;
random_deal(Y);

to result in "Dealing card to X..." and "Dealing card to Y...",
respectively, then the simple answer is that there is no standard way to do
that. Perhaps you should give the Hand class a "name" member to store its
name, so you could pass that name to its constructor and retrieve/display it
in random_deal.

that's exactly it, David. what I'm attempting.

thx for the very helpful replies, all.

I took a look at the link Thomas posted. can't really figure out how I
can adapt it to do what I need (i.e., to say "My Fred object: f").
right now it just reports the private int i_ which is something I must
say I already knew how to do:


#include <iostream>

class Fred {
public:
friend std::eek:stream& operator<< (std::eek:stream& o, const Fred& fred);
void set(int x) {
i_ = x;
}
private:
int i_; // Just for illustration
};

std::eek:stream& operator<< (std::eek:stream& o, const Fred& fred)
{
return o << fred.i_;
}

int main()
{
Fred f;
f.set(3);
std::cout << "My Fred object: " << f << "\n";
std::cin.get();
}
 
D

David Hilsee

that's exactly it, David. what I'm attempting.

thx for the very helpful replies, all.

I took a look at the link Thomas posted. can't really figure out how I
can adapt it to do what I need (i.e., to say "My Fred object: f").
right now it just reports the private int i_ which is something I must
say I already knew how to do:

I was thinking of something along the lines of (untested, uncompiled code
below)

class Hand {
std::string name;

public:
Hand(const std::string& aName) : name(aName){}

std::string getName() const {
return name;
}
};

void random_deal(Hand& X) {
// ...
cout << "Dealing card to " << X.getName() << "...";
// ...
}

void someFunc() {
Hand X("X");
random_deal(X);

Hand Y("Y");
random_deal(Y);
}

Of course, you could overload operator<< if you like. HTH.
 
A

A_StClaire_

David said:
I was thinking of something along the lines of (untested, uncompiled code
below)

class Hand {
std::string name;

public:
Hand(const std::string& aName) : name(aName){}

std::string getName() const {
return name;
}
};

void random_deal(Hand& X) {
// ...
cout << "Dealing card to " << X.getName() << "...";
// ...
}

void someFunc() {
Hand X("X");
random_deal(X);

Hand Y("Y");
random_deal(Y);
}

Of course, you could overload operator<< if you like. HTH.


ah. string argument and save the string. I'm stupid. thx a bunch,
David.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top