Mizuki
07-31-2010, 08:42 PM
// ok this program is suppose to simulate drawing 52 cards randomly from a deck. i wrote this program before using only array indexing. now i'm trying to
write it with pointers. but every time i try to run it points to the return value of function pick_available_card(int n) and sais invalid conversion from int* to int.
#include <iostream>
#include <math.h>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;
string suits[4] = {"hearts", "diamonds", "spades", "clubs"};
string ranks[13] = {"ace", "two", "three", "four", "five",
"six", "seven", "eight", "nine",
"ten", "jack", "queen", "king" };
int card_remaining = 52;
int picked_cards [52]; // this is where the cards will be stored
int rand_0toN1(int n);
int pick_available_card(int n);
void get_a_card(int n);
int rand_0toN1(int n) // this is the function that picks random numbers
{
return rand() % n;
}
int pick_available_card(int n) // supposed to store the cards in pick_cards
{
int *z;
z = picked_cards;
while (*z == true) // if at the start of the array there are cards that
{ // were drawn already, it will skip it.
z++;
}
while (n-- >0) // will execute the loop n times
{
z++;
while (*z == true) // will skip card that are already drawn
{
z++;
}
}
*z = true; // suppose to set where it stops to true
return z; <---- this is the issue
}
void get_a_card()
{
int r, s,n, card;
n = rand_0toN1(card_remaining--); // assign a random number to n
card = pick_available_card(n);
r = card % 13; // give a number between 0-12
s = card / 13; // gives a number between 0-4
cout << ranks[r]<< " of "<< suits[s]<< endl;
}
int main()
{
int n,i;
cout << "this program will simulate picking 52 cards randomly" << endl;
while(1)
{
cout<< " Enter the amount of card you want to be picked then press ENTER (enter zero(0) to quit :";
cin >> n;
if (n == 0)
{
break;
}
for (i=0;i<n; i++)
{
get_a_card();
}
}
return 0;
}
write it with pointers. but every time i try to run it points to the return value of function pick_available_card(int n) and sais invalid conversion from int* to int.
#include <iostream>
#include <math.h>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;
string suits[4] = {"hearts", "diamonds", "spades", "clubs"};
string ranks[13] = {"ace", "two", "three", "four", "five",
"six", "seven", "eight", "nine",
"ten", "jack", "queen", "king" };
int card_remaining = 52;
int picked_cards [52]; // this is where the cards will be stored
int rand_0toN1(int n);
int pick_available_card(int n);
void get_a_card(int n);
int rand_0toN1(int n) // this is the function that picks random numbers
{
return rand() % n;
}
int pick_available_card(int n) // supposed to store the cards in pick_cards
{
int *z;
z = picked_cards;
while (*z == true) // if at the start of the array there are cards that
{ // were drawn already, it will skip it.
z++;
}
while (n-- >0) // will execute the loop n times
{
z++;
while (*z == true) // will skip card that are already drawn
{
z++;
}
}
*z = true; // suppose to set where it stops to true
return z; <---- this is the issue
}
void get_a_card()
{
int r, s,n, card;
n = rand_0toN1(card_remaining--); // assign a random number to n
card = pick_available_card(n);
r = card % 13; // give a number between 0-12
s = card / 13; // gives a number between 0-4
cout << ranks[r]<< " of "<< suits[s]<< endl;
}
int main()
{
int n,i;
cout << "this program will simulate picking 52 cards randomly" << endl;
while(1)
{
cout<< " Enter the amount of card you want to be picked then press ENTER (enter zero(0) to quit :";
cin >> n;
if (n == 0)
{
break;
}
for (i=0;i<n; i++)
{
get_a_card();
}
}
return 0;
}