Looking for guidance on how to properly solve his HW problem. I can't seem to correctly use the pop feature or try/catch exception. I have been in contact with my professor and classmates, just looking for some help as many places as I can. Thank you for any clarification. I have looked up the various solutions on this site, as well as the documentation (and tried many solutions) and this is where I have ended up.
-Looks like the lines for errors are -10, just fyi.
--Thank you for the heads up, .h and .cpp are up, (working on line codes)
~Russell
#include <iostream>
#include <string>
usingnamespace std;
constint MAX = 10; // The MAX number of elements for the stack
// MAX is unknown to the client
typedefchar el_t; // the el_t type is char for now
// el_t is unknown to the client
class stack
{
private: // to be hidden from the client
el_t el[MAX]; // el is an array of el_t's
int top; // top is index to the top element in stack
public: // prototypes to be used by the client
// exception handling classes
class Overflow{};
class Underflow{};
stack(); // constructor
~stack(); // destructor
void push(el_t);
void pop(el_t&);
void topElem(el_t&);
bool isEmpty();
bool isFull();
void displayAll();
void clearIt();
};
#include <iostream>
#include <string>
#include "HWstack.h"
usingnamespace std;
stack::stack()
{
top = -1; // indicate an empty stack }
}
stack::~stack()
{}
bool stack::isEmpty()
{
if (top == -1)
returntrue;
elsereturnfalse;
}
bool stack::isFull()
{ if (top == MAX-1 )
returntrue;
elsereturnfalse;
}
void stack::push(el_t elem)
{ if (isFull())
throw Overflow();
else
top++; el[top] = elem;
}
void stack::pop(el_t& elem)
{ if ( isEmpty() )
throw Underflow();
else { elem = el[top]; top--;}
}
void stack::topElem(el_t& elem)
{ if ( isEmpty() )
throw Underflow();
else { elem = el[top]; }
}
void stack::displayAll()
{ if (isEmpty())
cout << "[ empty ]" << endl;
elsefor (int i=top; i>=0; i--)
{ cout << el[i] << endl; }
}
void stack::clearIt()
{ if (isEmpty())
cout << "[ empty ]" << endl;
elsefor (int i=top; i>=0; i--)
{ pop( el[i] ); }
}
#include <iostream>
#include <string>
#include "HWstack.h"
usingnamespace std;
int main()
{
stack postfixstack; // integer stack
string expression;
cout << "type a postfix expression: " ;
cin >> expression;
int i = 0; // character position within expression
char item;
int box1; // receive things from pop
int box2; // receive things from pop
int result;// receive integer after operation performed
while (expression[i] != '\0')
{
try
{
item = expression[i]; //1. read an item.
if ( item == (int(item)) ) //2. if it is an operand (number),
{
int x = int(expression[i]) - 48;
postfixstack.push(x);//push it (you might get Overflow exception)
cout<<x<<endl;
cout<<"--------------"<<endl; //apply the operator to the two operands
}
elseif ( (item == '+') || (item == '-') || (item == '*') )//3. else if it is an operator,
{
postfixstack.pop(box1);//pop the two operands
postfixstack.pop(box2);//(you might get Underflow exception)
cout<<box2<<endl;
cout<<box1<<endl;
cout<<"--------------"<<endl;
if (item == '+')
{
result = box1+box2;
cout<<result<<endl;
cout<<"--------------"<<endl;
}
elseif (item == '-')
{
result = box2-box1;
cout<<result<<endl;
cout<<"--------------"<<endl;
}
elseif (item == '*')
{
result = box1*box2;
cout<<result<<endl;
cout<<"--------------"<<endl;
}
postfixstack.push(result);// push the result
} //close else if
elsethrow"invalid item";
} // this closes try
// Catch exceptions and report problems and quit the program now.
catch (HWstack::Overflow)
{cerr<<"Error: Too many items in the stack"<<Overflow; exit(1);}//handle error
catch (HWstack::Underflow)
{cerr<<"Error: Too few items in the stack"<<Underflow; exit(1);}//handle error
catch (charconst* errorcode) // for invalid item
{cerr<<"Error: Invalid item for stack"<<errorcode; exit(1);}//handle error
i++; // go to next position in expression
// go back through the loop
}// end of while
// After the loop successfully completes:
// The result will be at the top of the stack. Pop it and show it.
postfixstack.pop(result);
cout<< "result: " << result <<endl;
// If anything is left on the stack, an incomplete expression was found
// inform the user.
if (!postfixstack.isEmpty())
{ cout<<"Incomplete expression!"<<endl;}
}//end of program
ERRORS:
stackclient.cpp:147: error: no matching function for call to âstack::pop(int&)â
HWstack.h:32: note: candidates are: void stack::pop(el_t&)
stackclient.cpp:148: error: no matching function for call to âstack::pop(int&)â
HWstack.h:32: note: candidates are: void stack::pop(el_t&)
stackclient.cpp:179: error: expected type-specifier before âHWstackâ
stackclient.cpp:179: error: expected `)' before â::â token
stackclient.cpp:179: error: expected `{' before â::â token
stackclient.cpp:179: error: â::Overflowâ has not been declared
stackclient.cpp:179: error: expected `;' before â)â token
stackclient.cpp:181: error: expected primary-expression before âcatchâ
stackclient.cpp:181: error: expected `;' before âcatchâ
stackclient.cpp:183: error: expected primary-expression before âcatchâ
stackclient.cpp:183: error: expected `;' before âcatchâ
stackclient.cpp:192: error: no matching function for call to âstack::pop(int&)â
HWstack.h:32: note: candidates are: void stack::pop(el_t&)
You haven't provided hwstack.h, so can't comment on any issues in your header file. Note that several of your error messages refer to your header file.
Some other comments:
Line 37: What id the purpose of this if statement? Casting a char to an int will result in the same binary value. i.e. This if statement will always be true.
You're dealing with one character at a time from expression. You're not doing any conversion from numeric characters entered to integers (casting won't do this). What if I enter 123 ?
Line 22: cin only reads up to whitespace. You probably want to use getline to get the entire line. If you do do, you'll want to ignore whitespace as as you're parsing the expession.
edit: The line numbers in your posted code do not agree with the line numbers in your error messages. That makes it difficult to find errors in your code.