Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-31-2010, 08:42 PM   PM User | #1
Mizuki
New to the CF scene

 
Join Date: Jul 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Mizuki is an unknown quantity at this point
invalid conversion from int* to int. how do i fix that

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


Code:
#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;
}
Mizuki is offline   Reply With Quote
Old 08-01-2010, 04:13 AM   PM User | #2
Beagle
Senior Coder

 
Join Date: Jul 2005
Location: New York, NY
Posts: 1,084
Thanks: 4
Thanked 19 Times in 19 Posts
Beagle is an unknown quantity at this point
Well, in that function, z is a pointer to an int. But your function is expecting to return an int, and not a pointer to an int. So, if you want to return the int, dereference z. if you want to return the pointer, change your function signature to return an int pointer instead of an int.
Beagle is offline   Reply With Quote
Old 08-02-2010, 05:38 PM   PM User | #3
Mizuki
New to the CF scene

 
Join Date: Jul 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Mizuki is an unknown quantity at this point
Quote:
Originally Posted by Beagle View Post
Well, in that function, z is a pointer to an int. But your function is expecting to return an int, and not a pointer to an int. So, if you want to return the int, dereference z. if you want to return the pointer, change your function signature to return an int pointer instead of an int.
i tried that i set the the declaration and the definition of the funtion pick_available_card to
Code:
pick_available_card(int *n)
when i do that it goes to the function get_a_card() and point

void get_a_card()
{
int r, s,n, card;
n = rand_0toN1(card_remaining--);
card = pick_available_card(n); <---- this and states the same error has before
r = card % 13;
s = card / 13;
cout << ranks[r]<< " of "<< suits[s]<< endl;
}


it also still points to the same error in pick_available_card(int *n)

if i change
Code:
card= pick_available_card(n)
to
Code:
card = pick_available_card(&n)
, that error gets solved but the
Code:
return z
error int pick_available_card(int *n) function is still there.
Mizuki is offline   Reply With Quote
Reply

Bookmarks

Tags
c++, code, random function, simulation

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:11 AM.


Advertisement
Log in to turn off these ads.