Myi stack programs compiles OK! but when I enter the data it bombs out after I press the enter button

This is the message that I get from the build log:
mingw32-g++.exe -Wall -fexceptions -g -c C:\Martin\Savitch\Unisa\Unisa\STACKS\StackFrame\Stack.cpp -o obj\Debug\Stack.o
C:\Martin\Savitch\Unisa\Unisa\STACKS\StackFrame\Stack.cpp: In destructor 'stacksavitch::Stack::~Stack()':
C:\Martin\Savitch\Unisa\Unisa\STACKS\StackFrame\Stack.cpp:48:14: warning: variable 'next' set but not used [-Wunused-but-set-variable]
C:\Martin\Savitch\Unisa\Unisa\STACKS\StackFrame\Stack.cpp: In member function 'void stacksavitch::Stack::push(char)':
C:\Martin\Savitch\Unisa\Unisa\STACKS\StackFrame\Stack.cpp:66:36: warning: 'temp_ptr' is used uninitialized in this function [-Wuninitialized]
Process terminated with status 0 (0 minute(s), 5 second(s))
0 error(s), 2 warning(s) (0 minute(s), 5 second(s))


Here is the code:

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
 //DISPLAY 13.17 Interface File for a Stack Class
//This is the header file stack.h. This is the interface for the class Stack,
//which is a class for a stack of symbols.
#ifndef STACK_H
#define STACK_H
namespace stacksavitch
{
    struct StackFrame
    {
        char data;
        StackFrame *link;
    };

    typedef StackFrame* StackFramePtr;

    class Stack
    {
    public:
        Stack( );
        //Initializes the object to an empty stack.

	Stack(const Stack& a_stack);
        //Copy constructor.

        ~Stack( );
        //Destroys the stack and returns all the memory to the freestore.

        void push(char the_symbol);
        //Postcondition: the_symbol has been added to the stack.

        char pop( );
        //Precondition: The stack is not empty.
        //Returns the top symbol on the stack and removes that
        //top symbol from the stack.

        bool empty( ) const;
        //Returns true if the stack is empty. Returns false otherwise.
    private:
        StackFramePtr top;
    };
}//stacksavitch

#endif //STACK_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
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
56
57
58
//DISPLAY 13.19 Implementation of the Stack Class
//This is the implementation file stack.cpp.
//This is the implementation of the class Stack.
//The interface for the class Stack is in the header file stack.h.
#include <iostream>
#include <cstddef>
#include "stack.h"
using namespace std;

namespace stacksavitch
{
    //Uses cstddef:
    Stack::Stack( ) : top(NULL)
    {
        //Body intentionally empty.
    }

    Stack::Stack(const Stack& a_stack)
         //The definition of the copy constructor is Self-Test Exercise 11.
    Stack::~Stack( )
    {
        char next;
        while (! empty( ))
           next = pop( );//pop calls delete.
    }


    //Uses cstddef:
    bool Stack::empty( ) const
    {
        return (top == NULL);
    }


    void Stack::push(char the_symbol)
          //The rest of the definition is Self-Test Exercise 10.
    //Uses iostream:
    char Stack::pop( )
    {
        if (empty( ))
        {
            cout << "Error: popping an empty stack.\n";

exit(1);
        }

        char result = top->data;

        StackFramePtr temp_ptr;
        temp_ptr = top;
        top = top->link;

        delete temp_ptr;

        return result;
    }
}//stacksavitch


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
//DISPLAY 13.18 Program Using the Stack Class
//Program to demonstrate use of the Stack class.
#include <iostream>
#include "stackSavitch.h"
using namespace std;
using namespace stacksavitch;

int main( )
{
    Stack s;
    char next, ans;

    do
    {
        cout << "Enter a word: ";
        cin.get(next);
        while (next != '\n')
        {
            s.push(next);
            cin.get(next);
        }

        cout << "Written backward that is: ";
        while ( ! s.empty( ) )
            cout << s.pop( );
        cout << endl;

        cout << "Again?(y/n): ";
        cin >> ans;
        cin.ignore(10000, '\n');
    }while (ans != 'n' && ans != 'N');

    return 0;
}


Please assist
OKAY its all fixed up now.

Thanks
Where are your definitions of the second constructor and stack::push()?
Topic archived. No new replies allowed.