0

I am using an array in C++, trying to generate a random number that isn't in a list of numbers used before (so each number is in a set range, can only appear once, but is in a random order) However on running it says that there is no begin function (code snippet attached)

int GenRandIndex(int size, int used[]){
    bool ret=false;
    int n;
    while (!ret){
        n=GenRandNum(0,size-1);
        for (int i:used){
            if (n==i){
                break;
            }
            else{
                ret=true;
            }
        }
    }
    return n;
}

int c[4]={0,2,3,4};
cout << GenRandIndex(5,c);

This should (in theory) output 1, as it should generate a random number between 0 and 5-1 (4), and keep generating until it reaches a number that isn't in the list c (of which the only choice is 1) however I get the error in the title (error C2672: 'begin': no matching overloaded function found)

cigien
  • 57,834
  • 11
  • 73
  • 112
HElpME
  • 45
  • 5
  • 2
    `used` is a pointer. There's no way to know how many elements it is pointing at, so a range based loop is impossible. Use a vector instead (obligatory advice to newbies). – john Dec 27 '20 at 17:53
  • Thank you for your help! I only started c++ very recently so haven't looked into vectors yet, but will look at some tutorials! – HElpME Dec 27 '20 at 18:36

0 Answers0