Can someone explain to me why my vector won't output?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
        int i = 0;
	int pop = 10000, stars_dscvrd = 0;
	int army = pop / 2;
	int pop2 = 10000, stars_dscvrd2 = 0;
	int army2 = pop2 / 2;
	int galaxy_stars = 400;
        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 };


	while (i == 0)
	{

		while (stars_found.size() < 150 && stars_found2.size() < 150)
		{
			int x= 0;
			x++;
			int plop = army / x;
			int plops = army2 / x;
			holder1.push_back(plop);
			holder2.push_back(plops);
			
			
		}

		int random1 = rand() % holder1.size();
		int random2 = holder1[random1];

		int random3 = rand() % holder2.size();
		int random4 = holder2[random3];
		while (galaxy > 0)
		{


			stars_found.push_back(random2);
			stars_found2.push_back(random4);
			galaxy -= 2;
			stars_dscvrd++; stars_dscvrd2++;
		}
		std::cout << stars_found.back() << std::endl;
		std::cout << stars_found2.back() << std::endl;
		}

From what I understand outputting the last element of the two vectors should work, but the console comes up blank.
Last edited on
I don't see any way for the loop on lines 18-28 to ever finish. You never push anything into either stars_found nor into starts_found2.
@helios would you care to explain ? It is supposed to run until the size of the 2 vectors reach 150. Or maybe working with a do while loop would work?
Last edited on
This is your looping condition: stars_found.size() < 150 && stars_found2.size() < 150

These are the variables you modify within the loop:
1
2
	holder1.push_back(plop);
	holder2.push_back(plops);

You're either checking or push_back'ing to the wrong vectors.
Last edited on
Hello possum swallower,

When I managed to get your code to run, and please provide enough code to compile and test, there are a couple things I noticed:

You are using "rand()", but never call "srand()" to seed the RNG.

You are using magic numbers that would be better defined as a constant variable. I started "main" with:
1
2
3
4
int main()
{
    constexpr size_t MAXSIZE{ 150 };
    srand(static_cast<unsigned int>(time(nullptr)));


Then in the while loop:
1
2
3
4
5
6
7
8
9
10
11
while (stars_found.size() < MAXSIZE && stars_found2.size() < MAXSIZE)
{
    int x = 0;
    x++;

    int plop = army / x;
    int plops = army2 / x;

    holder1.push_back(plop);
    holder2.push_back(plops);
}

As Ganado pointed out "stars_found" never changes, so yo have an endless while loop.

Inside the loop you define "x" then add 1 to it before you use it, but at the closing } of the loop "x" is destroyed. When you reenter the loop you define "x" then add 1, so "x" is always 1 and your calculations always divide by 1. I do not believe that is what you want.

If yo put a break point in the loop somewhere you will see that "holder1" and "holder2" contain the number (5000) no matter how big the vector gets. Eventuall the program will crash when it runs out of memory to add to the vector, but you would find that every element is (5000).

You need to fix this while loop before you can continue.

Andy
@Handy Andy, I didn't include the seed generator into my question, so my fault for not making that clear.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
	int x = 0;
	while (i == 0)
	{
		while (holder1.size() < 150) // I think this is functional but I can't tell as of right now
		{

			x++;
			int plops = army2 / x;
			int plop = army / x;
			holder1.push_back(plop);
			holder2.push_back(plops);


		}


		int random1 = rand() % holder1.size();
		int random2 = holder1[random1];

		int random3 = rand() % holder2.size();
		int random4 = holder2[random3];

		while (galaxy > 0)
		{
			stars_found.push_back(random2);
			stars_found2.push_back(random4);
		}
		std::cout << stars_found.back();
	}
} 

I corrected the loop, but my ide is ignoring std::cout << stars_found.back(); and my console screen is still coming up blank , which is my main issue . How could I fix that?
Now the loop from lines 23-27 has the exact same problem as before. You're looping on the value of a variable that never changes.
I corrected the loop, but my ide is ignoring std::cout << stars_found.back(); and my console screen is still coming up blank , which is my main issue.

You are interpreting the thing you see incorrectly. That statement is not ignored. Your program never gets that far due to earlier statements.

Furthermore, "Calling vector::back() on an empty container causes undefined behavior.". Are you sure that your vector is not empty?

Even when you have fixed loop on lines 23-27, there is still the outer loop (lines 2-29) that repeats forever. Do you really want that?


You had earlier:
1
2
3
4
5
6
7
8
while (galaxy > 0)
{
  stars_found.push_back(random2);
  stars_found2.push_back(random4);
  galaxy -= 2;
  stars_dscvrd++;
  stars_dscvrd2++;
}

This loop does end.
How many times does it repeat? galaxy/2 times.

You don't need explicit loop for this. http://www.cplusplus.com/reference/vector/vector/resize/
1
2
3
4
5
6
const auto G = galaxy / 2;
stars_found.resize( stars_found.size() + G, random2 );
stars_found2.resize( stars_found2.size() + G, random4 );
stars_dscvrd += G;
stars_dscvrd2 += G;
galaxy = ( galaxy % 2 ) ? -1 : 0;


But why do you add same value G times to vector?
Last edited on
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
Last edited on
Hello possum swallower,

After reading 's post a couple of times it finally hit me. in the line while (galaxy > 0) it works great the 1st time, but not after that. The value of "galaxy" will be reduced to either (0)zero or (-1), but is never reset or otherwise changed.

So after the 1st pass through the outer most while loop it is very possible that the while condition will always be false and the vectors "stars_found" and "stars_found2" will never change, so you are always printing the same numbers.

I also found that:
1
2
3
4
5
int random1 = rand() % holder1.size();
int random2 = holder1[random1];

int random3 = rand() % holder2.size();
int random4 = holder2[random3];

Are not needed at all.

Inside the while loop you can do this:
1
2
stars_found.push_back(holder1[rand() % MAXSIZE]);
stars_found2.push_back(holder1[rand() % MAXSIZE]);

I used the 1 vector here because "holder2" is exactly the same as "holder1" There is really no need for "holder2" since it is just a duplicate.

The output I now get is:

156
46

60
37

151
55

60
625

34
38



 Press Enter to continue:


Which I believe is more like what you want.

Andy
@Handy Andy, I had rewritten the code and I got it to work with some of the things you have shown me . Apologies for being oblivious.
I got it to work

Note that "it works" is a frequent assumption. It can be true, but very often the case is not
* "it works correctly"
but in reality:
* "it appears to give expected results with some inputs"

Testing can be insufficient and undefined behaviour can give false positives.

Not even best of the best can always write perfect code.
Topic archived. No new replies allowed.