# Thread: C++ Beginner Simple Calculator

1. ## C++ Beginner Simple Calculator

I am writing a program that models a simple calculator. The user will imput an operand and number and the calculator, starting w/zero, will continually accumulate a new sum, until it is promped to quit. The only operands I am using are (+,-,*,/,^, and q for quit).
I don't understand how the computer can intake the operand and number in the same command and read them and do the computation. I understand how it can read them separately. I know I can accumulate using a loop, but still lost.
* Any direction will be appreciated!

2. One way to do it is to read the entire input into a string. Parse the string separating number ( double would be the most flexible ) from the operators. Put both in separate queues (first in first out array ). When done take the first two numbers and use the first operator, put that value in your accumulator and get the next two numbers and perform the operation of the second operator, and so on. This method has no operator presidence but you could put that in with multiple passes

a hint on the string to float would be the atof function.

And remember if its not an operator or a space it should be checked to see if its a number. If it is none of the above its bad data.

3. read them all in as characters.
If you are in a loop, just use if statements.
such as first read = 2

cin>>char;
if(!isdigit(char)){ // if char is not a digit
operand = char;
}
else{
save digit // arbitrary
}

This is really simple if you have a stack class.

4. You want the calculator to be able to evaulate expressions like

3 + 2 * 7

correctly?

It's actually not as trivial as it may seem initially.

Popular pages Recent additions