Stacks: [Error] 'exit' was not declared in this scope.
Aug 10, 2014 at 6:23am UTC
So our professor asked us to make a project where we will need three files (two .cpp files and one .h file) This is regarding our topic about Stacks. When I compile the project in Dev C++, I only get one error which is the
[Error] 'exit' was not declared in this scope. Can anyone please tell me how to solve this? I'm really close letting this program run. Only the exit isn't working. Would appreciate your help. Thank you!
Here's the code for
Stacks.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#ifndef STACK
#define STACK
const int STACK_CAPACITY = 128;
typedef int StackElement;
class Stack
{
public :
Stack();
bool empty() const ;
void push(const StackElement &value);
void display(ostream &out) const ;
StackElement top() const ;
void pop();
private :
StackElement myArray[STACK_CAPACITY];
int myTop;
};
#endif
Here's the code for
Stacks.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <iostream>
using namespace std;
#include "Stack.h"
Stack::Stack(){
myTop--;
}
bool Stack::empty() const
{
return (myTop == -1);
}
void Stack::push(const StackElement &value)
{
if (myTop < STACK_CAPACITY - 1)
{
++myTop;
myArray[myTop] - value;
}
else
{
cerr << "*** Stack full -- can't add new value ***\n"
"Must increase value of STACK_CAPACITY in Stack.h\n" ;
exit(1);
}
}
void Stack::display(ostream &out) const
{
for (int i = myTop; i >= 0; i--)
cout << myArray[i] << endl;
}
StackElement Stack::top() const
{
if ( !empty() )
return (myArray[myTop]);
else
{
cerr << "*** Stack is empty -- returning garbage value ***\n" ;
StackElement garbage; return garbage;
}
}
void Stack::pop()
{
if ( !empty() )
myTop--;
else
cerr << "*** Stack is empty -- can't remove a value ***\n" ;
}
Here's the code for
Driver.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include <iostream>
using namespace std;
#include "Stack.h"
int main()
{
Stack s;
cout « "Stack created. Empty?" « boolalpha « s.empty() « endl;
cout « "How many elements to add to the stack? " ;
int numItems;
cin » numItems;
for (int i = 1; <=numItems; i++)
s.push(i);
cout « "Stack contents:\n" ;
s.display(cout);
cout « "Stack empty? " « s.empty() « endl;
cout « "Top value: " « s.top() « endl;
while (!s.empty())
{
cout « "Popping " « s.top() « endl;
s.pop();
}
cout « "Stack empty? " « s.empty() « endl;
cout « "Top value: " « s.top() « endl;
cout « "Trying to pop:" « endl;
s.pop();
}
Aug 10, 2014 at 6:32am UTC
exit()
is declared in the header
<cstdlib>
(or <stdlib.h> for C programs). You just need
#include <cstdlib>
in Stacks.cpp.
Also, If you use Dev-C++, please make sure it is the
Orwell version. Earlier versions are considered obsolete.
http://orwelldevcpp.blogspot.com/
Topic archived. No new replies allowed.