Hey if I have a value that was p=3 in a for loop, and after a few loops it gets changed to p=6 is there anyway to say "go back to previous value"?

But the value is getting changed all the time, example: the value starts off at 0, first loop changes it to 1, 4th loop changes it to 5, 7th loop changes it to 9. How do I go from 9 to 5 without knowing its going to be 9 and 5?

Edited 5 Years Ago by GhostMonkey: n/a

The value is irrelevant, only the timing matters:

#include <cstdlib>
#include <iostream>

int main()
{
    bool saved = false;
    int curr, prev;
    int opt;

    for (int i = 0; i < 10; i++) {
        curr = std::rand();

        std::cout<<"1) Save\n2) Restore\n3) Do nothing\n> ";

        if (!(std::cin>> opt))
            break;

        switch (opt) {
        case 1:
            prev = curr;
            saved = true;
            break;
        case 2:
            if (saved)
                curr = prev;
            break;
        case 3:
            break;
        default:
            return 0;
        }

        std::cout<< curr <<'\n';
    }
}

Hi again, I have got my code going to the previous value by simply making anither value equal it outside the loop like:

for(j;j<10;j++)
{
x2=x;
y2=y;
}
for(i;i<10;i++)
if(array[i][j] == 0)
{
x=i;
y=j;
}

if(array[i][j] == counter)
{
  counter++
}
if(array[i][j] != counter && counter<=9)
{
   array[i][j] == counter
}	

if(array[i][j] != counter && counter>9)																				{																																										x=x2;																					y=y2;																					i=0;
counter++																																																											}

So that works and makes x and y the previous value, but is it possible with this code to make it do that more than once? I've simplified the code but it basically searches an array for the counter and increments it if it finds the numbers already used, if the counter goes above 9, it goes to the last zero and changes that number to try and fix the problem and it should repeat this until it works. Problem is with my code it will only do it once since I can only save the previous zero and not the zeros before it. Any help?

Hello ghostmonkey,
In line 19 you didn't assign the value. You have used == . Is that a mistake due to your typing ? Why can't you use another integer array for storing zeros and a variable to count the number of zeros ?

Edited 5 Years Ago by Arbus: n/a

Ah yeah sorry that should read

if(array[i][j] != counter && counter<=9)
{
   array[x][y] == counter;
}

Edited 5 Years Ago by GhostMonkey: n/a

I'd suggest you keep a vector of previous entries and provide an interface to access that data (or use vector iterators to do it).

For instance vector::push_back will allow you to add to the end of the vector and you can use an iterator to cycle backwards through the set of values in that vector. vector::back will give you the most recently 'assigned' value.

A nicer way of doing this would be to implement a wrapper to the vector that allows for operator= , prev , and next calls that hide all of the details.

This article has been dead for over six months. Start a new discussion instead.