Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   problem with stack (http://www.go4expert.com/forums/problem-with-stack-t6797/)

ckmelissa86 12Oct2007 14:59

problem with stack
 
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.

DaWei 12Oct2007 17:15

Re: problem with stack
 
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


ckmelissa86 12Oct2007 19:41

Re: problem with stack
 
Well i'm not that clear.. wont u mind explain more clearly??
THX

shabbir 12Oct2007 22:22

Re: problem with stack
 
You should be having the code snippets in the code block for better readability. Read Hints before you make a post

DaWei 12Oct2007 22:35

Re: problem with stack
 
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.


All times are GMT +5.5. The time now is 03:34.