Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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;
    }

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •