How can I increment elements of a vector(or array)?

Adding to this 'example' would be appreciated :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	int x = 1;
	std::vector<int> children{};
	int dog = 10, cat = 10
	do 
	{
		cat--;
		dog--;

		if (cat == dog)
		{
			children.push_back(1); // as in kids or children being created
		}
		Sleep(700);
	} while (x == 1);

each children value that is added I wish to be incremented, so I could compare an age to it and if it reaches a certain age it becomes an adult or dies depending on it's incremented integer. Problem is, is that I don't know how I would do that so I am stuck with that code up there(which is practically meaningless, just a starting point for someone to add to). I've seen all the std::valarray stuff, but I cannot pushback a value with that.
Last edited on
Hello possum swallower,

I think what you are looking for is: children[index]++;.

Andy
Hello possum swallower,

Or adding to your code maybe:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int x = 1;
std::vector<int> children{};
int dog = 10, cat = 10

do
{
    cat--;
    dog--;

    if (cat == dog)
    {
        children.push_back(1);

        children.back()++;
    }

    Sleep(700);
} while (x == 1);

http://www.cplusplus.com/reference/vector/vector/back/

Un tested, but I think it should work.

Andy
Hello possum swallower,

When I put your code into my IDE I noticed that: while (x == 1); is an endless loop because "x" never changes.

I eventually came to this:
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
#include <iostream>
#include <iomanip>
#include <limits>
//#include <string>
#include <vector>
//#include <cctype>

#include <chrono>
#include <thread>

int main()
{
    int x{ 1 };
    std::vector<int> children;
    int dog{ 10 }, cat{ 10 };

    std::cout << "\n working";

    do
    {
        cat--;
        dog--;

        if (cat == dog)
        {
            children.push_back(1);

            children.back()++;
        }

        std::this_thread::sleep_for(std::chrono::milliseconds(700));  // Requires header files "chrono" and "thread".

        std::cout << '.';
    } while (dog);  // <--- When "dog" becomes (0)zero the condition fails.


    // <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
    // The next line may not be needed. If you have to press enter to see the prompt it is not needed.
    //std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    std::cout << "\n\n Press Enter to continue: ";
    std::cin.get();

    return 0;  // <--- Not required, but makes a good break point.
}

In line 31 "milliseconds" can be changed to "seconds" and the number in () would be the whole seconds, (1) is 1 second. Much nicer to use than "Sleep()" and more portable.

When I checked this left "children" with 10 elements all having a value of 2. Not sure if that is what you wanted, but it is a start.

Andy
@Handy Andy, so it left each element with a value of two? How could I up their value ?
And also one more thing, would you know why my children.back() comparison will not work?
1
2
3
4
5
6
7
8
9
10
11
	if (cat == dog)
		{
			children.push_back(1);

			children.back()++;
			if (children.back() == 2)
			{
				std::cout << "The kid is now 2 years old ";
			}
		}
Last edited on
Hello possum swallower,

I honestly have no idea why it does not work. All I can think of is the the ".back" returns a reference to the element, so that would be a temporary variable that holds an address of the last element of the vector. I am not sure how to go from the reference to the value needed.

As far as changing an element in the vector somewhere you would use children[index]. Of course you would have to know which element of the vector goes with which child.

Andy
@Handy Andy, ok, I'll try some more stuff with it. Thanks for your help.
1
2
3
4
if (children.back() == 2)
{
	"The kid is now 2 years old ";
}

 In function 'int main()':
Line 3 warning: statement has no effect [-Wunused-value]


In other words, your if-statement does the same as:
1
2
3
if (children.back() == 2)
{
}

How can you tell that it does not work?

Did you intend:
1
2
3
4
if (children.back() == 2)
{
	std::cout << "The kid is now 2 years old\n";
}

Don't understand what is trying to be achieved. All of the posted code does not really make any sense.

@possum swallower - perhaps you would explain in more detail exactly what you are trying to achieve.
Agree with @seeplus - what is the point of all this? You could find out how many times through the loop just by looking at the values of cat or dog.

Your "example" looks to be heading for the proverbial XY problem. Nothing you have shown here indicates why you need an array called children in the first place.
Hello possum swallower,

Last night I was so focused on the if condition that I missed the actual problem. I see that keskivert has pointed that out.

I would suggest 1 other change: if (!children.empty() && children.back() == 2). From what I have read the first part of the if condition is a good idea because if the vector is empty it causes undefined behavior. And you do not want that. You could also use this with an else to say that the vector is empty.

Andy
@Keskiverto, yes my mistake.
Last edited on
@seeplus, yes that was the point, I really did not understand how I could achieve the solution I was looking for, I asked for someone to please add to it because it basically means nothing , it was a starting point I had for someone else to add to
Topic archived. No new replies allowed.