I need to generate a random customer ID and assign it, if the ID already does not exist.
>So basically, for the moment I'm using array to store the IDs' (custid[10])
>Array size is 10 (for the moment)
>Generate a random number between 1 and 10
>Check if it already exists
>If it does not, assign to customer id array.
This is what I have tried;
Code:
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
using namespace std;
int custid[10]; //array to store assigned IDs'
int randomcid; //array to store generated random ID
int randomgen()
{
srand(time(NULL));
randomcid = rand() % 10 + 1;
return 0;
}
int main()
{
custid[0] = { 1, 5, 6 }; //filling the array with some values
for (int a = 0; a <= 10; a++)
{
if (custid[a] == randomcid) //if the ID already exists
{
randomgen(); //generate another random number
}
else
{
custid[a] == randomcid;
}
}
return 0;
}
What I want it to do is say it generated 2, and the array contains the values 1 6 and 3 in the first three indexes, I want it to skip the first three indexes and assign the number 2 to the fourth index.
Hope someone helps xD