| | |
array output came many times (char,int array)
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2006
Posts: 25
Reputation:
Solved Threads: 0
Hi all,
How can make the output not to repeat again for many players. here is part of code.
ex of output is (1 player): D9 ,H2,SQ....
(2 player) : CX,H8,D9...
D9 came to second time again!!! pls help me :eek:
How can make the output not to repeat again for many players. here is part of code.
C++ Syntax (Toggle Plain Text)
struct { int face; unsigned int value; } card[13] = { {'2', 2},{'2', 3},{'2', 4}, {'5', 5},{'6', 6},{'7', 7}, {'8', 8},{'9', 9},{'X', 10}, {'J', 13},{'Q', 12},{'K', 11}, {'A', 1} }; char suit[4]={'D','C','H','S'};
ex of output is (1 player): D9 ,H2,SQ....
(2 player) : CX,H8,D9...
D9 came to second time again!!! pls help me :eek:
I would create more of a deck than you have right now, and treat it as a stack. You would have 52 cards, each card would have a suit, a face, and a value, and you can shuffle and deal from it easily.
C++ Syntax (Toggle Plain Text)
#include <algorithm> #include <iostream> #include <string> struct { char suit; std::string face; int value; } deck[52]; struct { const char *face; int value; } cardValues[] = { {"2", 2},{"3", 3},{"4", 4}, {"5", 5},{"6", 6},{"7", 7}, {"8", 8},{"9", 9},{"10", 10}, {"J", 13},{"Q", 12},{"K", 11}, {"A", 1} }; const char cardSuits[] = {'D','C','H','S'}; int main() { using namespace std; int i = 0; // Build the deck for ( int suit = 0; suit < 4; suit++ ) { for ( int face = 0; face < 13; face++ ) { deck[i].face = cardValues[face].face; deck[i].value = cardValues[face].value; deck[i].suit = cardSuits[suit]; ++i; } } // Shuffle the deck random_shuffle( deck, deck + 52 ); // Pop off hands of at most 5 until the deck is empty int n = 1; for ( i = 0; i < 52; ) { cout << "Hand #" << n++ << '\n'; for ( int j = 0; i < 52 && j < 5; i++, j++ ) cout << deck[i].suit << " -- " << deck[i].face << " -- " << deck[i].value << '\n'; cout << '\n'; } return 0; }
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
•
•
Join Date: Nov 2006
Posts: 25
Reputation:
Solved Threads: 0
Thank you! But when everytime i ran the program, output came the same. for example:
like that for Hand 1 for everytime.
And other hands too.:rolleyes:
C++ Syntax (Toggle Plain Text)
Hand #1 D --A--1 D--3--3 D--J--13 S--Q--12 D--2--2
like that for Hand 1 for everytime.
And other hands too.:rolleyes:
You're welcome! 
Your random_shuffle() uses rand() to generate the shuffle and rand() is always seeded by default to 1. You can just change the seed each time and get something different.

Your random_shuffle() uses rand() to generate the shuffle and rand() is always seeded by default to 1. You can just change the seed each time and get something different.
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <ctime> // Other includes... // Deck definitions... int main() { using namespace std; // Build the deck... // Shuffle the deck srand( (unsigned int)time( 0 ) ); random_shuffle( deck, deck + 52 ); // Display the hands... return 0; }
Last edited by Ravalon; Jan 2nd, 2007 at 12:48 pm.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
•
•
•
•
Your random_shuffle() uses rand() to generate the shuffle and rand() is always seeded by default to 1. You can just change the seed each time and get something different.
Be careful with how you phrase that bit of advice. Seeding rand with time is a good idea, however, the call to srand() should only occur once - usually at the beginning of the program will do, in order to use a different seed whenever rand() is called.
Multiple calls to srand() will generally have the opposite effect
Last edited by Bench; Jan 2nd, 2007 at 1:06 pm.
¿umop apisdn upside down? •
•
•
•
Be careful with how you phrase that bit of advice. Seeding rand with time is a good idea, however, the call to srand() should only occur once - usually at the beginning of the program will do, in order to use a different seed whenever rand() is called.
Multiple calls to srand() will generally have the opposite effect
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
•
•
Join Date: Nov 2006
Posts: 25
Reputation:
Solved Threads: 0
Oh ! now it comes real. I do appreicate of that .
But can i ask one more Q?
I want to know what is following ths:
it seem to be function call. but no....:rolleyes:
And other one is when I ran with rand() I got one message..
But can i ask one more Q?
I want to know what is following ths:
C++ Syntax (Toggle Plain Text)
random_shuffle( deck, deck + 52 );
it seem to be function call. but no....:rolleyes:
And other one is when I ran with rand() I got one message..
C++ Syntax (Toggle Plain Text)
Error 1 error C2661: 'rand' : no overloaded function takes 1 arguments
Last edited by sTorM; Jan 2nd, 2007 at 1:43 pm. Reason: error
•
•
•
•
Oh ! now it comes real. I do appreicate of that .
But can i ask one more Q?
I want to know what is following ths:
C++ Syntax (Toggle Plain Text)
random_shuffle( deck, deck + 52 );
it seem to be function call. but no....:rolleyes:
If you really wanted to, you could fake random_shuffle() manually. C++ Syntax (Toggle Plain Text)
namespace Raye { template <class T> inline void swap( T& a, T& b ) { T temp = a; a = b; b = temp; } template <class T> inline void random_shuffle( T *first, T *last ) { if ( first != last) { for ( T *i = first + 1; i != last; i++ ) { T *b = first + rand() % ( i - first + 1 ); T temp = *i; *i = *b; *b = temp; } } } }

•
•
•
•
And other one is when I ran with rand() I got one message..
C++ Syntax (Toggle Plain Text)
Error 1 error C2661: 'rand' : no overloaded function takes 1 arguments
C++ Syntax (Toggle Plain Text)
// This does not give you a random number from 0 to 5 rand( 5 ); // This does give you a random number from 0 to 5 rand() % 5; // This does too, and it's theoretically better, but more cryptic rand() * ( 1.0 / ( RAND_MAX + 1.0 ) ) * 5;
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
•
•
•
•
Originally Posted by Ravalon
c Syntax (Toggle Plain Text)
struct { char suit; std::string face; int value; } deck[52];
•
•
•
•
It is a function call. random_shuffle() is a standard C++ function from the <algorithm> header. It saves you the trouble of writing your own shuffling code.
•
•
•
•
namespace Raye { template <class T> inline void swap( T& a, T& b ) { T temp = a; a = b; b = temp; } template <class T> inline void random_shuffle( T *first, T *last ) { if ( first != last) { for ( T *i = first + 1; i != last; i++ ) { T *b = first + rand() % ( i - first + 1 ); T temp = *i; *i = *b; *b = temp; } } } }
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
•
•
Ravalon, have you enabled all the warnings in your compiler settings (-Wall) since the above flags a warning saying "error: no matching function for call to `random_shuffle(<anonymous struct>[52], <anonymous struct>*)' ". You better name the struct just for standard's sake, its not good to let warnings hang around in your programs.
The best part is that I originally had the structs named and went out of my way to remove them. 
Subconsciously making the snippet look more complex than it needed to be I guess.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
![]() |
Other Threads in the C++ Forum
Views: 2693 | Replies: 11
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






