problem with stack

Discussion in 'C++' started by ckmelissa86, Oct 12, 2007.

  1. ckmelissa86

    ckmelissa86 New Member

    Joined:
    Oct 10, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Oct 12, 2007
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Please comply.
     
  3. ckmelissa86

    ckmelissa86 New Member

    Joined:
    Oct 10, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Well i'm not that clear.. wont u mind explain more clearly??
    THX
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice