When I test it the stack seems to be filling up correctly, but the answer always comes out as zero. Everthing compiles fine so it is some logic error I have.
#include <iostream>
#include "IntStack.h"
usingnamespace std;
// destructor
IntStack::~IntStack()
{
StackNode *nodePtr = nullptr, *nextNode = nullptr;
//point nodePtr at top
nodePtr = top;
while (nodePtr != nullptr)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
// function to push stack
void IntStack::push(int num)
{
//point to new node
StackNode *newNode = nullptr;
//Make new node and store num
newNode = new StackNode;
newNode->value = num;
//put on top if list is not empty, if emptry create it as top
if (isEmpty())
{
top = newNode;
newNode->next = nullptr;
}
else
{
newNode->next = top;
top = newNode;
}
}
// function to pop number from the top of the stack, will provide warning if the stack is empty
void IntStack::pop(int &num)
{
StackNode *temp = nullptr; // create temp ptr
//make sure not empty
if (isEmpty())
{
cout << "The stack is empty. \n";
}
else // pop top num from stack
{
num = top->value;
temp = top->next;
delete top;
top = temp;
}
}
//check if the stack is empty
bool IntStack::isEmpty()
{
bool status;
if (!top)
{
status = true;
}
else
{
status = false;
}
return status;
}
From the compile error that Thomas1965 had, the lesson there is to set the warning / error levels to their highest possible.
With g++ , one should at least use -Wall -Wextra -pedantic. IMO, the situation with VS is a little more awkward: the default level (-w3) is too low, the next (-w4) which is the highest finds lots of errors in the standard include files, so I guess one has to resort to setting individual warnings.
The clang++ compiler is probably (IMHO) one of the best freely available compilers: it has nice messages, and seems to be a frontrunner in terms of compliance with latest standards.
Also, your editor should be able to match pairs of braces or brackets automatically. If it doesn't or can't be set up to do so, then revert to old school method of typing both opening and closing braces, then go back and fill in what should be inside them. That way you will never be out of whack. I did that for about 20+ years, then had to unlearn that when I had an editor with that capability.
The program is a RPN stack, isn't it? So 2+3*2-2 should be 2 3 2 * + 2 - which is 6 ? If I am not mistaken. The 2 was produced with the first input because that was the last value placed on the stack.
Learning to use a GUI debugger should be really easy, basically say which variables you want to keep track of on a watch-list, set a beak point somewhere before where you think there is a problem, then step through 1 line at a time.
With a command line debugger such as gdb, there is a tutorial article in the articles section top left of this page, and a man page should be easily found on the web.
for (; is >> str1;){
if (str1 == "+"){
topNum = atoi(str1.c_str());
stack.pop(catchVar);
secondNum = atoi(str1.c_str());
stack.pop(catchVar);
stack.push(topNum + secondNum);
}
I am a bit confused by what you are doing here: Shouldn't line 24 be a while loop? And Line 28 sets topNum to 43 (the ASCII value) because str1 is "+"
You don't use catchVar in the addition expression.
With division, you don't seem to check for division by zero. And integer division won't work. Use static_cast<double>(TopNum) to cast one of the numbers to double.
IMO, the situation with VS is a little more awkward: the default level (-w3) is too low, the next (-w4) which is the highest finds lots of errors in the standard include files, so I guess one has to resort to setting individual warnings.
I am reading Debugging Windows Programs by Everett N. McKay and Mike Woodring and they recommend to use always the /W4 warning level.
To prevent problems compiling some libraries they recommend the following solution:
#prgma warning (push,3) // temporarily revert to /W3
#include "difficultLib.h"
#pragma warning(pop) // revert back to previous settings
Make sense to me, we should be concerned about compiling our own code without warnings.
Make sense to me, we should be concerned about compiling our own code without warnings.
Yes, I agree.
Although it strikes me as a little odd to have to put (non standard? ) pragmas into code, in order for it to compile with helpful warnings / errors or for it to not display heaps of spurious warnings.
Last time I dabbled with VS, I didn't look in detail as to what the warnings were for, for STL headers, but I remember there being pages of them. Was trying to read about it, but can't connect to MSDN website.
I guess I prefer to use clang on Linux and not have to worry about any of this :+)