1. ## Simple Calculator

Hello all it's been a couple of weeks but wuts up ?? well anyway here is my problem... I have a program that i need to read from a string a set of number for ex.(1*2-3/4) i want the string to work like a simple calculator. I have the code to work for the + and - for the string but i cann't get the * or the / to work. by the way i need to keep these recursive functions. here is my code that works....
Code:
```#include <iostream.h>
int eval(char A[], int n)
{
if(n == 1)
return (int (A[0])- int('0'));

if(A[n-2] == '+')
return eval(A,n-2) + (int (A[n-1]) - int('0'));
else
return eval(A, n-2)- (int (A[n-1]) - int('0'));
}

void main()
{
char A[20];
int n=0;
cout << "Enter a String: ";
int i=0;
A[0]=cin.get();
while(A[i] != '\n')
{
i++;
A[i]=cin.get();
}
n=i;
for(i =0; i < n; i++)
cout << A[i];
cout << endl;

cout << eval(A,n) << endl;
}```
p.s i think i need else if statements maybe but not sure... any help will be aprrieciated...

2. You would probably benefit by reading a couple chapters from "The C++ programming language", by Strustrup. <-probably misspelled. He gives an implementation for exactly what you are trying to write, and explains it very well.

3. What error did you get with for the multiplication and division operations?

Kuphryn

4. ## Re: Simple Calculator

[COLOR=red]OK you have a few issues with your code that I'm going to point out. If you would like ideas of how to fix it just ask and I'll help you there. However I do challenge you to think about it yourself before you look for help.

[COLOR=blue]
Code:
```#include <iostream.h>
int eval(char A[], int n)
{
if(n == 1)
return (int (A[0])- int('0'));

if(A[n-2] == '+')
return eval(A,n-2) + (int (A[n-1]) - int('0'));
else
return eval(A, n-2)- (int (A[n-1]) - int('0'));
}```
[COLOR=red]First off I checked your input and found out that you are completely screwed if the user's input is anything but single digit numbers. That needs to be taken of. After that you are right in saying that a couple extra else if statements.

[COLOR=blue]
Code:
```void main()
{
char A[20];
int n=0;
cout << "Enter a String: ";
int i=0;
A[0]=cin.get();
while(A[i] != '\n')
{
i++;
A[i]=cin.get();
}
n=i;
for(i =0; i < n; i++)
cout << A[i];
cout << endl;

cout << eval(A,n) << endl;
}```
[COLOR=red]Secondly you need to do some crash guarding and put in a line of code to make sure the user didn't just hit enter when prompted to use the calc.

5. ok here is what i have and it works see if it works for you guys and yes i know that it wont compute irrational numbers.
Code:
```#include <iostream.h>
int eval(char A[], int n)
{
if(n == 1)
return (int (A[0])- int('0'));

if(A[n-2] == '+')
return eval(A,n-2) + (int (A[n-1]) - int('0'));
else if (A[n-2] == '-')
return eval(A, n-2)- (int (A[n-1]) - int('0'));
else if(A[n-2] == '*')
return eval(A,n-2) * (int (A[n-1]) - int('0'));
else if( A[n-2] == '/')
return eval(A,n-2) / (int (A[n-1]) - int('0'));
}

void main()
{
char A[20];
int n=0;
do
{
cout << "Enter a String: ";
int i=0;
A[0]=cin.get();

while(A[i] != '\n')
{
i++;
A[i]=cin.get();
}
n=i;
for(i =0; i < n; i++)
cout << A[i];
cout << endl;

cout << eval(A,n) << endl;
}while(A[0]!=0);
}```