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.
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);
#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.
@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 ";
}
}
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.
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.
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.
@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