# Thread: Simple Calculator (input problem)

1. ## Simple Calculator (input problem)

I'm trying to make a simple calculator, but it has to receive input like this:

operand-operator-operand

i.e.

6*5
76+2
670/10

My problem is, how I can I write it so it knows when the first operand has ended (when it could be 1 digit or 5 for example) and then see what the operation is (multiplication, division, etc) and then start on the second operand?

Sorry for the rather silly question, and yes I need it to work this way, not enter the first operand, then enter the second, all together with the operator in one line.

2. Like this:
Code:
`std::cin>>operand1>>operator>>operand2;`
...where operands are numbers and operator is a char.

3. @manasij that only works if there are spaces or other whitespace between operators and operands.

if you make the assumption that the first operand will be all digits, you can just read in single characters, appending them to a string, until you find the first non-digit. then read that in, and then read the remainder of the line into another string. convert both strings to integer values, and apply the operator.

4. Originally Posted by Elkvis
@manasij that only works if there are spaces or other whitespace between operators and operands.
No..(with some conditions)
The following works even if there is no space between the tokens.
Code:
```#include<iostream>
using namespace std;
int main()
{
int op1,op2;char opr;
cin>>op1>>opr>>op2;
cout<<op1<<endl<<opr<<endl<<op2;
return 0;
}```

5. doh *facepalm*

Thanks

6. Originally Posted by Elkvis
@manasij that only works if there are spaces or other whitespace between operators and operands.
Or, I suppose, a non-integer.
Although, don't quote me on that.

7. Originally Posted by Elysia
Or, I suppose, a non-integer.
Although, don't quote me on that.
Quoted.. :P
Anyway.. It works when whitespaces are absent...for integers ( and in some cases floats and doubles)... The only way it'd fails in absence of whitespace is when the operators + or - are confused as parts of the floating points.

8. Ok I got it working via a swtich that does this:

std::cin>> op1 >> opr >> op2;

switch(opr){

case '+':
std::cout << op1+op2 << "\n";
break;

case '-':
std::cout << op1-op2 << "\n";
break;.......................

Now here's the million dollar question, how can I handle this:

2*2*2-0.5
??

Or any input that has multiple operands and multiple operators

(Sorry if this isn't formatted pretty)

9. Originally Posted by grache28
Now here's the million dollar question, how can I handle this:

2*2*2-0.5
??

Or any input that has multiple operands and multiple operators
There are a few ways, but one approach that could work as a starting point is to look up the Shunting Yard algorithm and the use of reverse Polish notation.

10. Thanks, that looks like a good way of doing it, but probably a little more advanced than what I need. Theres no other way to adjust for multiple operators and operands?

11. Originally Posted by grache28
Thanks, that looks like a good way of doing it, but probably a little more advanced than what I need. Theres no other way to adjust for multiple operators and operands?
No..(Unless the expression is so short that you can afford numerous passes over it/)
(Well...there is.. but I think you'll find it more complicated..) You could always build a tree... but Shunting Yard is way more easy to implement and understand ..IMHO.

12. Ok, thanks to you both. I'll work out a way to do it with the Shunting-yard algorithm and come back if I have any problems.

13. What if I don't need the calculator to perform the correct order of operations, for example:

5-3*5
=10

It just reads from left to right, but the amount of operators and operands is unknown. So the user could input:
5-4

or

5-4+3+3+2/2-2*5

Again the expression would be evaluated from left to right without using any order of operations, but the number of operators and operands is unknown.

Thanks

14. Just read the input character by character and perform the operation with a switch-case when you have two operands.

15. Ok, great. The problem is I only know how to get input with cin. So I wouldn't have to make a lot of variables, or? How do I know how many variables to make?