473,325 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Name Of Argument


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

Sep 8 '05 #1
8 1230
A_*********@hotmail.com wrote in news:1126139692.801816.85110
@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?

Sep 8 '05 #2

Andre Kostur wrote:
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?

Sep 8 '05 #3

A_StClai...@hotmail.com wrote:
Andre Kostur wrote:
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?


For starters, take a look at the relevant FAQ entry:

http://www.parashift.com/c++-faq-lit....html#faq-15.8

Best regards,

Tom

Sep 8 '05 #4
A_*********@hotmail.com wrote:

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

Sep 8 '05 #5
<A_*********@hotmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.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 << "...";
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.

--
David Hilsee
Sep 8 '05 #6

David Hilsee wrote:
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.

--
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:
#include <iostream>

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

std::ostream& operator<< (std::ostream& 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();
}

Sep 8 '05 #7
<A_*********@hotmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...

David Hilsee wrote:
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.

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

--
David Hilsee
Sep 8 '05 #8

David Hilsee wrote:
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.

--
David Hilsee

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

Sep 9 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Todd Gardner | last post by:
Pardon my extremely ignorant newbie questions. Where can I go to find more information about the "self" argument? Is there something special about the word "self" or did Mr. Guido van Rossum...
10
by: Chris Green | last post by:
Good day, I've done a bit of searching in the language reference and a couple pages referring the behavior of super() but I can't find any discussion of why super needs the name of the class as...
4
by: Todd Perkins | last post by:
Hello all, surprisingly enough, this is my first newsgroup post, I usually rely on google. So I hope I have enough info contained. Thank you in advance for any help! Problem: I am getting...
12
by: guy lateur | last post by:
Hi all, Suppose you have this class: class foo: def bar(): Suppose you also have the strings "foo" and "bar". How can you obtain the function foo.bar()?
11
by: singlal | last post by:
Hi, I am currently using DAYOFWEEK function and putting CASE statement to get english day name. For example: SELECT CASE DAYOFWEEK(CURRENT DATE) WHEN 1 THEN 'SUNDAY' WHEN 2 THEN 'MONDAY'
1
by: Chris Fairles | last post by:
Take the following snip (pretend A its a std::pair if you wish ;) ) template <class T1,class T2> struct A { A(A const&){cout<<"A(const&) ";} A(A&&){cout<<"A(A&&) ";} A(T1 const&, T2...
2
by: Bennett Haselton | last post by:
How do I find the mapping between a PHP package name like HTTP_Request, and the "yum" command to install it on CentOS 4.4? For example when I needed to use LWP::UserAgent to use in Perl, the...
13
by: Jim Mackellan | last post by:
In my opinion, C not mungling its function names imposes unnecessary complexity on C++, requiring extern 'C' { ... } everywhere in headers. I believe that in the next version of the ISO standard,...
185
by: jacob navia | last post by:
Hi We are rewriting the libc for the 64 bit version of lcc-win and we have added a new field in the FILE structure: char *FileName; fopen() will save the file name and an accessor function will...
9
by: Erwin Moller | last post by:
Hi all, Is it possible (PHP5.2) to find the name of a variable used in the caller of a function from within the function itself? Or to be more clear: ...php code.. $result = foo($abc);...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.