Hello possum swallower,
Your original code was a good start, but had problems. Some you have fixed and some you have made worse.
Just to make things a little easier, for testing, I did this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
int main()
{
constexpr size_t MAXSIZE{ 150 };
srand(static_cast<unsigned int>(time(nullptr)));
bool done{}; // <--- For 1st while loop.
int divisor{ 1 };
int loop{}; // <--- Tempory variable. may or may not stay. If kept choose a better name.
int population{ 10000 }; // <--- Consider making this a constant.
int stars_dscvrd{}; // <--- Defined and incremented, but never used.
int army{ population / 2 };
int pop2 = 10000; // <--- Consider making this a constant.
int stars_dscvrd2 = 0; // <--- Defined and incremented, but never used.
int army2 = pop2 / 2;
int galaxy_stars = 400; // <--- Consider making this a constant.
int galaxy = rand() % galaxy_stars;
std::vector <int> holder1;
std::vector <int> holder2;
std::vector<int> stars_found{ 1 };
std::vector<int> stars_found2{ 1 };
|
Then for the 1st while loop:
1 2 3 4 5 6 7 8
|
while (!done)
{
loop++; // <--- Used for testing. You may want to do something different.
if (loop > 5) // <--- Used for testing. You may want to do something different.
done = true;
}
|
This was done to limit the outer while loop so it would not run forever.
The 2nd while I would suggest this:
1 2 3 4 5 6 7 8 9 10
|
while (holder1.size() < MAXSIZE)
{
//divisor++;
int plop = army / divisor;
int plops = army2 / divisor++; // <--- Can use the (++) here;
holder1.push_back(plop);
holder2.push_back(plops);
}
|
Changing "x" to "divisor" makes more sense, but you know your program and may have a better name. Anything other than "x" which has no real meaning unless you are using it to describe a point on a graph.
Next I did have a problem whith these lines:
1 2 3 4 5 6
|
//int random1 = rand() % holder1.size();
//int random2 = holder1[random1];
int random2{ holder1[rand() % MAXSIZE] };
int random3 = rand() % holder2.size();
int random4 = holder2[random3];
|
Line 3 demonstrates how you can take 2 lines and just use 1.
As I tested the program "random1" and "random3" always produced the same number and in the end "random2: and "random4" always have the same number. Not sure why this is happening.
This was fine in your original code:
1 2 3 4 5 6 7 8 9
|
while (galaxy > 0)
{
stars_found.push_back(random2); // <--- The value of "random2" never changes.
stars_found2.push_back(random4); // <--- The value of "random4" never changes.
galaxy -= 2;
stars_dscvrd++; stars_dscvrd2++; // <--- Added to, but never used.
}
|
When you removed line 6 you created an endless loop because "galaxy" never changes.
This is what I get when I run the program:
500
53
500
53
500
53
500
53
500
53
500
53
Press Enter to continue:
100
57
100
57
100
57
100
57
100
57
100
57
Press Enter to continue:
|
Andy