Hi,

I need to make a simple calculator that just does addition and subtraction. But the program should keep a running total of the calculations entered like this:

sum = 0
12 + 3
sum = 15
5-2
sum = 12
1+1
sum = 13

But it also needs to have an UNDO command which removes the last calculation from the sum as if it never executed. The number of UNDO levels is unlimited, so it is possible to use the UNDO command to UNDO all of the operations back to the initial state of the program.

I have so far the code on how to do the calculations, but I need help with keeping a running total and the UNDO command. Thank you.

#include <iostream>
#include <iomanip>

using namespace std;

int main() 
{
    double firstNum, secondNum;   // Operands
    char oper;					  // Operator
    int  result;                  // Resulting value
    
    while (cin >> firstNum >> oper >> secondNum) 
	{
        switch (oper) 
		{
            case '+': result = firstNum + secondNum; 
					  break; 
            case '-': result = firstNum - secondNum; 	  
                      break;
            default : cout << "Bad operator '" << oper << "'" << endl;
                      continue;  // Start next loop iteration.
        }
		
     cout << result << endl << endl;

    }


	system("pause");
    return 0;
}

Recommended Answers

All 4 Replies

You need to add/initialize sum--

int sum = 0;

-- and then change your code to accommodate one of your requirements --

#include <iostream>
#include <iomanip>

using namespace std;

int main() 
{
    double firstNum, secondNum;   // Operands
    char oper;					  // Operator
    int  result;                  // Resulting value
    int sum = 0;                  // sum
    
    while (cin >> firstNum >> oper >> secondNum) 
	{
        switch (oper) 
		{
            case '+': result = firstNum + secondNum;
                      sum+= result;
					  break; 
            case '-': result = firstNum - secondNum;
                      sum-= result;	  
                      break;
            default : cout << "Bad operator '" << oper << "'" << endl;
                      continue;  // Start next loop iteration.
        }
		
     cout << result << endl << endl;
     cout << "Sum: " << sum << endl << endl;
    }


	system("pause");
    return 0;
}

-- you can easily implement undo by using a Stack of Pairs (or a stack of structs with char variable and int variable) (The left side of the datum contains the operand, the right side contains the value returned by firstNum + or - secondNum. This way you can pop the datum from the stack and do the opposite operation on sum with the value. Obviously disable undo when the stack is empty).

I wont show you how to do this. There are other methods, but this is a fairly decent one you should look up and try out for your calculator.

Shouldn't the input be more along the lines of:

sum = 0
+ 12
sum = 12
- 2
sum = 10
+ 5
sum = 15

It doesn't make sense to me to have a full equation added to the sum.

commented: I was thinking that too, but he specified different (and misunderstood) requirements XD +0

And because you have declared like: double firstNum, secondNum;

The result also must be a double.Not a integer.
For the undo command i would suggest you to try use the Goto function.

You need to add/initialize sum--

int sum = 0;

-- and then change your code to accommodate one of your requirements --

#include <iostream>
#include <iomanip>

using namespace std;

int main() 
{
    double firstNum, secondNum;   // Operands
    char oper;					  // Operator
    int  result;                  // Resulting value
    int sum = 0;                  // sum
    
    while (cin >> firstNum >> oper >> secondNum) 
	{
        switch (oper) 
		{
            case '+': result = firstNum + secondNum;
                      sum+= result;
					  break; 
            case '-': result = firstNum - secondNum;
                      sum-= result;	  
                      break;
            default : cout << "Bad operator '" << oper << "'" << endl;
                      continue;  // Start next loop iteration.
        }
		
     cout << result << endl << endl;
     cout << "Sum: " << sum << endl << endl;
    }


	system("pause");
    return 0;
}

-- you can easily implement undo by using a Stack of Pairs (or a stack of structs with char variable and int variable) (The left side of the datum contains the operand, the right side contains the value returned by firstNum + or - secondNum. This way you can pop the datum from the stack and do the opposite operation on sum with the value. Obviously disable undo when the stack is empty).

I wont show you how to do this. There are other methods, but this is a fairly decent one you should look up and try out for your calculator.

Thanks for the advice !
Now, I looked up stacks and got this class to use in my program. I am not sure if this is what you have meant. So, this is what I have now and I think the stack push method works, but the stack pop method doesn't it just ends the program.How could I use the pop method to return the previous sums.

Thanks again.

#include <iostream>
#include <iomanip>
 
using namespace std;
 
#define MAX 50      
 
class stack
{
  private:
    int arr[MAX];  
    int top;      

 
  public:
     stack()         
     {
        top=-1;    
     }
 

     void push(int total) 
     {

        top++;        
        if(top < MAX)
         {
            arr[top]=total;  
         }

         else
         {
            cout<<"STACK FULL"<<endl;
            top--;
         }

     }

	int pop(int total)               
    {
		 if(top==-1)
		 {
            cout<<"STACK IS EMPTY"<<endl;
            return NULL;
		 }
        else
		{
            total = arr [top];	
            arr[top]= NULL;				 
            top--;			
            return total;				
		 }

     }
};

int main() 
{
	stack total;

    int firstNum, secondNum;		// Operands
    char oper;						// Operator
	char undo;
    int  result;                  // Resulting value
    int sum = 0;                  // sum
 
	cout << "press c to input numbers, or press u to undo calculation" << endl << endl;

	while(cin >> undo)
	{
		if (undo =='c')
		{	
			while (cin >> firstNum >> oper >> secondNum)
			{
				 switch (oper) 
				{
					case '+': result = firstNum + secondNum;
							 sum+= result;
							 total.push(sum);
							 break; 
					case '-': result = firstNum - secondNum;
							 sum-= result;
					         total.push(sum);
							 break;
					 default : cout << "Bad operator '" << oper << "'" << endl;
                      continue;  // Start next loop iteration.
				}
 
				 cout << result << endl << endl;
				 cout << "Sum: " << sum << endl << endl;
			}
		}
		else if (undo == 'u')
		{
			sum = total.pop(sum);
			cout << "Sum: " << sum << endl;
		}

    }
 
 
	system("pause");
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.