0

just I want to ask how can I pass array size to throw function to set size of my game recoreds the only reason I am not using vector because I will base this recored to PMI lib and it doesn't support C++ Constrainers because it written in C that's why I use array

void playGame(int max_streak_length)
{
    int tracker =0 ;
    const int arrsize  = max_streak_length;

    int gameRecored[arrsize]={0};


    while( tracker < 4)
    {

        Craps games;

        if( games.Play()== true)
        {
            tracker++;
        }
        else
            if(tracker >0)
        {
            gameRecored[tracker-1]++;
            tracker = 0;
        }

    }
    gameRecored[tracker-1]++;
    int v= 0;


}
2
  • make a dynamic array, or use a vector? use a template function? Commented Sep 30, 2013 at 2:40
  • 5
    This is C++. Why don't you use std::vector? Commented Sep 30, 2013 at 2:41

2 Answers 2

2

C++ does not support the variable length array feature available in C.99. However, C++ offers std::vector<> which is as easy to use, and some may say safer.

    std::vector<int> gameRecored(arrsize, 0);

You can use gameRecored as an array like you do in your current code, and it will clean itself up when the function call returns.

Sign up to request clarification or add additional context in comments.

Comments

0

you can't not define an array whose size is a VARIABLE. If you want a dynamic size, you should use operator new, just like this:

int mysize = 10;
int* array = new int[mysize];

the variable mysize can be a dynamic number, such as function parameter. If your array will never change its size, you can use :

int array[10];

remember, if you use operator new, you must use operator delete to delete your array when you don't need it.

Hop can help you.

2 Comments

how can i fill them all 0
As @paddy said before me, using std::vector would be even better than making a dynamic array with new.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.