int main()
{
srand (time (0));
int counter=1;
string w[4]={"Hearts","Spades","Clubs","Diamonds"};
AAA:
int a=rand()%13+1;
int b=rand()%4;
counter=counter+1;
cout<<a<<" "<<w[b]<<endl;
if (counter<=52)
{
goto AAA;
}
else
{
goto BBB;
}
BBB:
return 0;
}
//the program does all i want but full of mistakes. there should be only 13 spades, 13 diamaonds, 13 clubs and 13 Hearts. because i can not limit the number of cards ( like there should be only 13 hearts && it should not repeat itself. here in output i ve many of the same card.
i m learning and now just began to read about classes. i need some simple help, not using pointers and structures. can it be done?
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main()
{
int a, b;
srand( time( 0 ) );
string w[4] = { "Hearts", "Spades", "Clubs", "Diamonds" };
bool taken[13][4]; // keep an array to determine which cards have been picked (initialise as false, i.e. not picked)
for ( a = 0; a < 13; a++ )
{
for ( b = 0; b < 4; b++ ) taken[a][b] = false;
}
int numCards;
cout << "How many cards do you want? ";
cin >> numCards;
for ( int n = 1; n <= numCards; n++ )
{
bool ok = false;
while ( !ok )
{
a = rand() % 13; // correct this below
b = rand() % 4;
ok = !taken[a][b];
}
taken[a][b] = true;
a++; // correction to start from 1, not 0
cout << setw( 2 ) << a << " of " << w[b] << endl;
}
return 0;
}
No pointers, no structures ... but you do use array, so arrays are allowed?
Lets say that numbers 1..13 are hearts, 101..113 are spades, 201..213 are clubs, ...
You have array int deck[52];
It has those 52 unique numbers.
You have shuffled the deck.
1 2 3
int card = deck[i];
int suit = card / 100;
int rank = card % 100;
What can you tell about the suit and rank?
What kind of numbers should the array contain, if we use them like this?
1 2 3
int card = deck[i];
int suit = card / 13;
int rank = card % 13 + 1;
thank you lastchance, coder777 and keskiverto for sharing time for my problem. lastchance really helped. i think coder777's program has a part missing which i cant correct. because it begins to shuffle from 1 then 2 then 3.............. its not really randomizingthe numbers i think. keskiverto ; this a nice viewpoint for mei thanks for the reply.
hmm ok. ıt is decreasing as (13-reamining[b]. now i got it. another way to do this. ıIt doesnt have to shuffle numbers , you are right. but in real a deck of cards usually do it. anyway thanks for the help, your program gave me some ideas as well. thank you coder777