Can I cite a decremented variable?

How can I include the original value of a decremented variable after its value has changed without adding a variable?

i.e

if I have a value
int value
value--
cout<<"the old value is "<<?????<<"the new value is"<<value

I want to site the inputted value
If you know you have done a decrement to get the new value you can just add one to the new value to get the old value.
cout<<"the old value is "<<value+1<<" the new value is "<<value;
the number to be decremented is inputted by the user...

my c++ teacher said its impossible, but it doesn't seem to be that hard of a request
Peter's approach will work.

Another way is to not print it all at once:

1
2
3
4
int value
cout<<"the old value is "<< value;
value--;
cout << "the new value is"<<value;
If you want to memorize the previous value you have to add a variable.

If this is in a function, I commonly use static variables.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
    while (true)
        loopedFunction();

    return 0;
}

void loopedFunction()
{
    static double position_ltv; // ltv = "last time value"
    double position;
    cin >> position;
 
    double distance = position - position_ltv

    cout << "Distance between this position and the last is: " << distance << endl;

    position_ltv = position;
}
Last edited on
Topic archived. No new replies allowed.