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