Accessing 2 objects

Hello

I have a little issue here, i want to access and modify 2 class objects at the same time is that possible?

or modify one after another is also ok

Thanks :)
Last edited on
What to you mean with 'at the same time'? In which way you want to modify them?
Last edited on
i want to modify a value inside both unti both have the value 0 then i want to end the app
If you want to set that value to zero in a single line you can do it this way: obj1.value = obj2.value = 0;
If you need to modify that value in some other ways you need can't do it at the same time but a human won't be able of telling the difference.

Notice: also obj1.value = obj2.value = 0; applies 0 to obj2 first and then to obj1
Right now i have an object array and a while and a for that checks the elements for the value <= 0
but i have no idea how to exit that loop when all elements are 0 or less than 0
Last edited on
How is your while structured?
bool isZero = false;
while(!isZero)
{
for(int i = 0; i < v; i++)
{
num = rand()%5 + 1;
number[i].minus(num);
if(number[i].getCurrentValue() <= 0)
{
car[i].setValue(0);


}
}
}

Note: v is a function parameter that i send in and it's also the size of my elements.

i know it's an endless loop at the moment
Last edited on
If you want to exit the while when all of them are equal to zero, add a second loop for checking:

1
2
3
4
5
6
7
8
9
10
11
12
13
while ( !isZero )
{
     // first for ...

    isZero = true; // assume all of them are zero

    for(int i = 0; i < v; i++)
        if ( number[i].getCurrentValue() != 0 ) // one of them is not zero   ( you don't really need the '!= 0' part )
        {
              isZero = false; // continue the while
              break; // exit from the for
        }
}
Last edited on
how do you mean with the assume all of them are zero?

if i set isZero to true there it will exit my loop
When you find one not equal to zero, isZero will be set to false ( line 10 )

You can't know the values of all your objects at the same time so you cannot check if they are all equal to zero. But you can check if not all of them are equal to zero

¬ ( ∀ object, object = 0 ) ⇔ ( ∃ object: object ≠ 0 )
Last edited on
Intressting eaven if i set the bool to true it still continues in a endless loop

EDIT: I solved it thanks for the advice :)
Last edited on
Topic archived. No new replies allowed.