0

I have wrote a small program that generates unique random numbers. I wrote it first using what I know, an array, to load and print the numbers. I am trying to replace the array with a vector so if I want make copies of the list I can do that easier. I am getting a error.

  error: cannot convert 'std::vector<int>' to "std::vector<int>*' for argument '1' to bool numInList(std::vector<int>*, int)' 

this error occurs when I call the numInList function.

I am new at using vectors but I thought you could use vectors like arrays with the benefits of built in functions, no fixed size, and the ability to copy one vector into another vector.

here is my code:

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <ctime>


using namespace std;
bool numInList(vector <int> randNumbers[], int num);

const int length = 100;

int main()
{
int countCheck = 0;
vector <int> randNumbers(length);
vector <int> newlist();

srand(time(0));

 while(countCheck < length){
    int num = rand() % 90000 +10000;

    if (!numInList(randNumbers, num)){
        randNumbers[countCheck] = num;
        cout << "The Random Number  " << randNumbers[countCheck] << endl;
        countCheck++;
    }
 }

cout << "\n\n\n";
newlist[] = randNumbers[];

return 0;
}
bool numInList(vector<int> randNumbers[], int num){
for (int index = 0; index < length; index++){
    if (randNumbers[index] == num){
        return true;
    }
}
return false;
}

I tried de-referencing hoping that would solve the problem

  if (!numInList(&randNumbers, num))

then I get an error on the IF statement in the function numInList

 error: ISO C++ forbids comparisons between pointers and integer [f-permissive]  

Any help would be greatly appreciated.


I have changed a few things, now I don't get any compilation errors but the program crashes when executed ... any suggestions???

    #include <iostream>
    #include <string>
    #include <vector>
    #include <fstream>
    #include <cstdlib>
    #include <ctime>


    using namespace std;
    bool numInList(vector <int> randNumbers, int num);

    const int length = 100;

    int main()
    {
    int countCheck = 0;
    vector <int> randNumbers;
    vector <int> newlist;

    srand(time(0));

    while(countCheck < length){
        int num = rand() % 90000 +10000;

        if (!numInList(randNumbers, num)){
            randNumbers.push_back(num);
            cout << "The Random Number  " << randNumbers[countCheck] << endl;
            countCheck++;
        }
     }

     cout << "\n\n\n";
     newlist = randNumbers;

     return 0;
     }
    bool numInList(vector<int> randNumbers, int num){
    for (int index = 0; index < length; index++){
        if (randNumbers[index] == num){
            return true;
        }
    }
    return false;
   }
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
bryan
  • 69
  • 5
  • 11
  • 1
    `vector randNumbers[]` declares `randNumbers` as an array **of** vectors. Or actually, as a pointer to vector since it's a function argument. Also `newlist[] = randNumbers[]` should be just `newlist = randNumbers`. – Emil Laine Mar 20 '15 at 00:48
  • Zenith, how would you declare the vector correctly? I tried removing the brackets [ ], but I got the same error. – bryan Mar 20 '15 at 00:56
  • @bryan, Do it in the declaration *and* definition of the function. – chris Mar 20 '15 at 00:57
  • Why don't you use a `std::set` instead of a vector if you don't want duplicates? If you did that, the `numInList` would not be required. – PaulMcKenzie Mar 20 '15 at 01:09
  • Thanks Zenith, I need a little break, I think your solution makes sense and I will try it out. – bryan Mar 20 '15 at 01:33

2 Answers2

3

You should pass vectors around by reference (unless you want to copy it). If you're not going to modify the vector, make it a const reference:

bool numInList(const vector<int>& randNumbers, int num)

More info: How to pass objects to functions in C++? (These are the very basics of C++. Get them right.)

Also, you can use vector::size to get the number of elements in a vector. This way index will never exceed the size of the vector and you won't have an out-of-bounds access, no matter what the size of the vector is:

for (int index = 0; index < randNumbers.size(); index++)

Here, I rewrote the whole thing for you to show how a vector should be used. Read it through carefully and make sure you understand everything (including why your original approach was not the best possible design choice):

int main()
{
  const int length = 100;
  vector<int> randNumbers;
  vector<int> newList;

  srand(time(0));

  while(randNumbers.size() < length){
    int num = rand() % 90000 +10000;

    if (!numInList(randNumbers, num)){
      randNumbers.push_back(num);
      cout << "The Random Number  " << randNumbers.back() << endl;
    }
  }

  cout << "\n\n\n";
  newList = randNumbers;

  return 0;
}

bool numInList(const vector<int>& randNumbers, int num){
  for (int index = 0; index < randNumbers.size(); index++){
    if (randNumbers[index] == num){
      return true;
    }
  }
  return false;
}
Community
  • 1
  • 1
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
1

The argument for numInList vector randNumbers[] is equivalent to vector* randNumbers change the argument to

bool numInList(vector<int> &randNumbers, int num)
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
Gary Kaizer
  • 274
  • 1
  • 7
  • Gary, I tried that. I get an error which says C++ forbids comparisons between pointer and integer [f-permissive] ... – bryan Mar 20 '15 at 00:54
  • remove newlist[] = randNumbers[]; – Gary Kaizer Mar 20 '15 at 00:57
  • I have removed all the []'s and ()'s, now the program crashes ... I had it running correctly using an array. I have posted my edited code. – bryan Mar 20 '15 at 01:03
  • You need to intialize your vector to vector randNumbers(length); – Gary Kaizer Mar 20 '15 at 01:07
  • Also move countCheck++; to outside the if statement otherwise counterCheck will never increment and you are stuck in the loop – Gary Kaizer Mar 20 '15 at 01:08
  • @GaryKaizer He's using `push_back`, no need to initialize with an initial size. – Emil Laine Mar 20 '15 at 01:10
  • 1
    @bryan A vector knows its size by calling the `vector::size` function. There is no need to be using variables to keep track of the size. I'm speaking specifically of your `for` loops that use a `length` variable. Why risk going out of bounds with a variable that may or may not have the correct value? Just get the true length by calling the `size()` member function. – PaulMcKenzie Mar 20 '15 at 01:12
  • @zenith The crash is at randNumbers[countCheck] – Gary Kaizer Mar 20 '15 at 01:13
  • @GaryKaizer Because he's using `length` instead of `randNumbers.size()` when the vector is empty. – Emil Laine Mar 20 '15 at 01:17
  • 1
    @bryan `randNumbers[countCheck]` Replace this with the `vector::back()` function to get you the last item in the vector. Again, use the vector's member functions when possible, and stop using separate variables to index the vector. Same thing here: `while(countCheck < length)` -- the `countCheck` is nothing more than the `size()` of the vector, so get rid of countCheck. – PaulMcKenzie Mar 20 '15 at 01:17
  • What PaulMcKenzie said. Vector is not an array, don't use it like one. – Emil Laine Mar 20 '15 at 01:19
  • @zenith Im not going to fix his [poor] design choice, just showing him why his code doesn't work – Gary Kaizer Mar 20 '15 at 01:20
  • Thanks for the help Zenith & PaulMcKenzie, I not familiar with vectors yet ... Your suggestions worked, I think the main problem was trying to variables instead of the size function. – bryan Mar 20 '15 at 01:43