Actually my stack overflow error isn't working does anyone see an issue? |
You're testing too late. You need to check there's room before adding (trying to add) the new element.
And you pop() method has the same problem. And more! (inc. you say "The Following Has Been Removed From The Stack." but not what!)
Andy
Meanwhile...
Why are you posting broken code? Lines 60-62 are clearly wrong!
Edit: Ah ok, I assume it was for readablity reasons.
Instead of using multiple
cout <<
s you can do the following:
58 59 60 61 62
|
cout <<"Welcome to the Amazing Stack Program\n"
"p= to push into the stack\n"
"i= to pop the stack\n"
"d=to display the stack\n"
"q= to quit"<<endl;
|
During preprocessing all adjacent string literals are concatenated, so this ends up looking more or less like your earlier code by the time the compilation phase begins.
I want this to be perfect. |
Well...
1. as Bdanielz has already commented, you should not use system() to pause.
See this thread from 2008...
Console Closing Down
http://www.cplusplus.com/forum/beginner/1988/
and
Why system() is evil
http://www.cplusplus.com/forum/articles/11153/
based on "Console Closing Down" article:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
using namespace std;
void waitForUser(){
cout << "Press ENTER to continue...";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
int main(){
cout << "Hello world!\n";
waitForUser();
return 0;
}
// main() is one case where you can omit the return, even though it's
// a function which returns an int, but I'm one of the people who
// still does it as it's consistent with all other functions.
|
2. conio.h is not a standard header. And you don't even appear to need it. So lose it!
3. C++ programmers do not use #define for consts. We use consts
const int maxSize = 10;
(I follow the convention that all caps is used for #defines -- used for other things than defining consts!)
4. It is better form to initialize class members using the constructor initializer list.
5. operator== returns true or false, so you can simplify some of your code.
6. if() tests a boolean condition, and isFull() and isEmpty() already return true or false. So the explicit test looks a bit wordy to me. (This is more a convention thing, but I can tell you the standard library writers don't use ==true or ==false either!)
7. I follow the convention that all variables are initialized.
(This is point 19 of Sutter and Alexandrescu's C++ Coding Standards book: "Always Initialize Variables")
8. C++ programs should not really use exit() to end a program.
(Calling exit() disrupts the destruction of locally scoped objects:
http://stackoverflow.com/questions/461449/return-statement-vs-exit-in-main
return statement vs exit() in main() )
You have already defined endless to control your while loop but you don't use it.
Also, it looks like endless should be a bool rather than int.
9. the (evil)
system("pause");
call on line 42 is after a return statement so is pointless.
10 why so many pauses, anyway? You only really need the one at the end to stop the console closing (which you would have been skipping as you called exit().)
11 you have a superfluous ; on line 47
And in a few other places.
12 I think it's better to keep a condition evaluation and what it triggers on separate lines. This is to do with the way debugger step though code and handle break points. e.g.
1 2 3
|
if(isEmpty()){ // no == true
cout<<endl<<"Stack is empty ";
} // no ;
|
rather than
if(isEmpty()==true){cout<<endl<<"Stack is empty "; };
13 globals are evil.
coolStack is only used in main(), so that's where it should de defined.
14 you could make the methods of your class which don't alter its state const.
15 This
48 49 50
|
for (int count = 0; count <= tos; count++){
cout << stack[count]<<", ";};
cout<<endl;
|
would read better as
48 49 50 51
|
for (int count = 0; count <= tos; count++){
cout << stack[count]<<", ";
} // lost ;
cout<<endl;
|
(at first glance I thought the cout<<endl; was inside the loop.)
Etc.
16 your indenting could (should) be tidied it up to make it easier to spot scopes
17 why tos rather than topIndex?
a variable or function name should make it clear what it does!
Self-documenting
http://en.wikipedia.org/wiki/Self-documenting
18 and finally, a blank line between each function would make your code a bit more readable.
(There are few more minor points, such as preferring ++x to x++ as a rule (only use post increment when actually required), but they're not so important.)