problem with stack

ckmelissa86's Avatar, Join Date: Oct 2007
Newbie Member
Code:
#include <iostream>
#include <stack>
#include <string>
#include <math>         
using namespace std;

int main()
{
  stack<double> stackObject;
  double a, b,c,d;
  string s;

  do {
    cout << ": ";
    cin >> s;
    switch( s[ 0 ]) {
      case 'q': // quit the calculator
        break;
      case '.': // show top-of-stack
        cout << stackObject.top() << endl;
        break;
      case '+': // add
        if(stackObject.size() < 2) {
          cout << "Operand Missing\n";
          break;
        }

        a = stackObject.top();
        stackObject.pop();
        b = stackObject.top();
        stackObject.pop();
        c=a+b;
        cout << c << endl;
        stackObject.push(a + b);
        break;
      case '-': // subtract
        // see if user entering a negative number
        if(s.size() != 1) {
          // push value onto the stack
          stackObject.push(atof(s.c_str()));
          break;
        }

        // otherwise, is a subtraction
        if(stackObject.size() < 2) {
          cout << "Operand Missing\n";
          break;
        }

        a = stackObject.top();
        stackObject.pop();
        b = stackObject.top();
        stackObject.pop();
        d= b-a;
        cout << d << endl;
        stackObject.push(b - a);
        break;
      case '*': // multiply
        if(stackObject.size() < 2) {
          cout << "Operand Missing\n";
          break;
        }

        a = stackObject.top();
        stackObject.pop();
        b = stackObject.top();
        stackObject.pop();
        cout << a*b << endl;
        stackObject.push(a*b);
        break;
      case '/': // divide
        if(stackObject.size() < 2) {
          cout << "Operand Missing\n";
          break;
        }

        a = stackObject.top();
        stackObject.pop();
        b = stackObject.top();
        stackObject.pop();
        cout << b/a << endl;
        stackObject.push(b/a);
        break;
      default:      
        // push value onto the stack
        stackObject.push(atof(s.c_str()));        
        break;
    }
  } while(s != "q");

  return 0;
}
this is the source code i found in web, but there's some errors when i compile.. the compiler said there's too few argument in template class "stack" , and undefined symbol "stackObject" .. hope you guys can help me find out the errors.

Last edited by shabbir; 12Oct2007 at 18:19.. Reason: Code block
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Please comply.
Quote:
Originally Posted by Before you make a query
7) Use code tags. Use the appropriate code tag (e.g. [ php][/php ] and [ code][/code ]) to make your code easier to read. See BB Code Section
0
ckmelissa86's Avatar, Join Date: Oct 2007
Newbie Member
Well i'm not that clear.. wont u mind explain more clearly??
THX
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You should be having the code snippets in the code block for better readability. Read Hints before you make a post
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
If you're going to copy stuff from the web, you still need to go through and understand it. You should also research any error messages you get. Learn them. They're there to be useful, not impediments.

One of your problems, too, is that the code you have posted here has a couple of crap characters. These cause subsequent statements that may be correct to be uninterpretable.

Another thing is that you should probably be including either math.h or cmath, not math. You don't mention your compiler, so it is hard to know.

Once these problems are corrected, the code compiles just fine, and runs. I didn't test it for correct performance.

If you can't understand something as simple as adding code tags to preserve your formatting, I fear that programming is going to be far too difficult for you.