Making a calculator

So i'm trying to figure a way to call back to a place in a vector (labelled history) but backwards. It's hard to explain for me.
Basically i'm making a calculator to and I'm trying to be able to call back to a place in the history of it. So for example, a command I get is "h3" which goes to my menu to redo the history item 3 places ago.

I know I need to start with my variable "operand" and do an if statement of: If operand is in range between 0 and history.size()-1 call DoCommand in a recursive fashion with the history item desired as an argument

so I am guessing it's like this: if(((operand >= 0) && (operand <= history.size - 1)) == 0){
}

but then I need to recursively call my DoCommand function using the desired history item as an argument.

1
2
3
4
5
6
7
8
9
10
	case 'h':
	case 'H':
			// If operand is in range between 0 and history.size()-1
			// call DoCommand in a recursive fashion with the
			// history item desired as an argument
			if(((operand >= 0) && (operand <= history.size()-1))== 0){
				
				DoCommand(//need something here);
				}
			break;


I am at a loss with that. Any help would be appreciated!

just be clear here's an example of what I want to do:
Say I start with number 10:
I add 10 and get 20
subtract 5 and get 15
then add 5 and get 20
BUT then go back in the history that these instructions are held and recall say step 1 to add 10 again but calling that from the history of the vector so I can add 10 again.
Last edited on
You want to start at the end of your vector and move forward to the front?

ONE WAY to walk through a vector in reverse...........

A vector's elements can be accessed by iterators.

There are forward iterators (begin/end), and there are reverse iterators (rbegin/rend)

http://www.cplusplus.com/reference/vector/vector/rbegin/
http://www.cplusplus.com/reference/vector/vector/rend/

Iterators can be treated as pointers, so you can do pointer math on iterators.

Get an iterator on rbegin, and if you want to "jump back" 3 elements, + 3 to the iterator.

No not that really.
I want to just go back to a point in the vector. Not go through the entire thing.
Iterators, pointer math.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <vector>

int main()
{
   // create a simple vector to show how iterators work
   std::vector<int> vec { 1, 2, 3 ,4 ,5 ,6 ,7, 8, 9, 10 };

   // create an iterator that the beginning is the end of the vector
   auto itr { vec.rbegin() };

   // jump forward 2 elements to get the 3rd element from the end
   std::cout << *(itr + 2) << '\n';
}
Alright I will try that to see if i can get it to work once I get home.
Yea, Furry Guy, I tried to copy and paste that code and see what it does and it doesn't work at all in DevC++
DevC++ is an old compiler, and really doesn't like iterator uniform initialization. Change line 10 to
auto itr = vec.rbegin();

Now DevC++ is not so cranky.

https://stackoverflow.com/questions/24233796/uniform-initialization-of-iterators

DevC++, the Orwell fork, is over 4 years old, and using a MinGW version even older.

Well, more to the point, C++11 had the problem, it was fixed in a later standard.

You are going to miss out on a lot of cool features added to C++ with C++17.
Last edited on
Hehe thank you. I used DevC++ for a programming class and I got so used to it. But I finally finished the calculator thanks to you. Thank you so much.
Be grateful your instructor/school uses DevC++. There are programming courses that require the student use Turbo C++.

Now THAT is really old. DevC++ is C++11 compliant, TurboC++ is not.
Topic archived. No new replies allowed.