I'm trying to call a class function using a loop
for (int i = 0; i < Basket.getLemonNum(); i++)
{
lemonWeights[i] = Fruit.generateWeight(fruit, fruitWeight);
cout << lemonWeights[i] << " ";
}
This goes to the Fruit class to it's member function generateWeight():
int fruitClass::generateWeight(char fruitN, int& fruitW)
{
srand(time(NULL));
int weight = 0;
switch (fruitName)
{
case 'a':
weight = rand() % 500 + 100;
return fruitW = weight;
break;
case 'l':
weight = rand() % 400 + 300;
return fruitW = weight;
break;
case 'w':
weight = rand() % 1000 + 800;
return fruitW = weight;
break;
}
}
Output:
128 128 128 128
but it's generating the same number all the time, even when I use a different function to call it:
for (int i = 0; i < Basket.getWatermelonNum(); i++)
{
watermelonWeights[i] = Fruit.generateWeight(fruit, fruitWeight);
cout << watermelonWeights[i] << " ";
}
Output:
128 128 128
As you can see, I did seed srand(). Also, the header files and are included. What is going on?