Stack to state input, show and delete

I try to make a coding to show a stack that state input, show and delete. But there is a mistake in the coding that i cant find by myself. Help me to solve this please. I need to know the answer

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
 
#include<iostream>
using namespace std;

class nodeStack
{
	//int data;
	//nodeStack *next;
	public:
	int data;
	nodeStack *next;
};

class stack
{
	//private: // pengisytiharan ahli data
		nodeStack *top;
	public:
		stack()
		{
			top=NULL;
		}
 		void push(); // to insert an element
 		void pop();  // to delete an element
 		void show(); // to show the stack
 		bool isEmpty ();
 		
};
		
void push(int newitem)
{ // create newnode
	nodeStack *head;
	nodeStack *newnode;
	newnode = new (nodeStack);
	if( newnode == NULL)
	cout << "Cannot allocate memory…"<< endl;
		else // add to empty stack, or to front stack
	{ 
		newnode->data = newitem;
		newnode->next = head;
		head = newnode;
	}// end if
} //

void pop()
	{  //char stackTop (); 
		bool isEmpty();
		nodeStack *head;
		nodeStack *delnode;
		stackTop();
		if (isEmpty())
		cout <<"Sorry, Cannot pop item from stack.Stack is still empty!" <<endl;
		else
		{ 
			delnode = head;
			cout << "Item to be popped from stack is: " << stackTop()<<endl;
			head = delnode->next;
			delete(delnode);
		}// end else
}	

int stackTop()
	{ 
		bool isEmpty();
	    nodeStack *head;
		if (isEmpty())
		cout <<"Sorry,Stack is still empty!"<<endl;
		else
		return head->data;
} // end check item at top
	
void show()
{
   nodeStack *top;
   nodeStack *newnode=top;
   cout<<"\nThe stack is:    ";
   while(top!=NULL)
   {
      cout<<newnode->data<<" ->";
      newnode=newnode->next;
   }
   cout<<"NULL\n";
}

//main function
int main()
{
   stack s;
   int choice;
   while(1)
   {
      //cout<<"\n-----------------------------------------------------------";
      cout<<"\n\t\tSTACK USING LINKED LIST\n\n";
              cout<<"1:PUSH, 2:POP, 3:DISPLAY STACK, 4:EXIT";
      cout<<"\nEnter your choice(1-4): ";
      cin>>choice;
      switch(choice)
      {
         case 1:
                   s.push();
                   break;
         case 2:
                   s.pop();
                   break;
         case 3:
                   s.show();
                   break;
         case 4:
                   exit(0);
                   break;
         default:
                   cout<<"Please enter correct choice(1-4)!";
                   break;
       }
   }
   return 0;
}
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <cassert>

struct stack
{
    stack() : top_of_stack(nullptr) {}
    ~stack() { clear() ; }

    void push( int v ) { node* n = new node( v, top_of_stack ) ; top_of_stack = n ; }

    void pop()
    {
        assert( !empty() ) ;
        node* n = top_of_stack ;
        top_of_stack = top_of_stack->next ;
        delete n ;
    }

    void clear() { while( !empty() ) pop() ; }

    bool empty() const { return top_of_stack == nullptr ; }

    void show( std::ostream& stm = std::cout ) const
    {
        for( node* n = top_of_stack ; n != nullptr ; n = n->next ) stm << n->value << ' ' ;
        stm << '\n' ;
    }

    int& top() { assert( !empty() ) ; return top_of_stack->value ; }
    int top() const { assert( !empty() ) ; return top_of_stack->value ; }

    private:

        struct node
        {
            explicit node( int v, node* n = nullptr ) : value(v), next(n) {}
            int value ;
            node* next ;
        };

        node* top_of_stack = nullptr ;

        // non-copyable, non-moveable
        stack( const stack& ) = delete ;
        stack& operator= ( const stack& ) = delete ;
};

int main()
{
   std::cout << "\n\t\tSTACK USING LINKED LIST\n\n" ;

   stack s;
   int choice;
   int value_to_push = 0 ;

   while(true)
   {
      std::cout << "1:PUSH, 2:POP, 3:DISPLAY STACK, 4:EXIT\nEnter your choice(1-4): ";
      std::cin>>choice;

      switch(choice)
      {
         case 1:
                   s.push( value_to_push += 7 );
                   break;
         case 2:
                   if( !s.empty() ) s.pop();
                   else std::cout << "empty!\n" ;
                   break;
         case 3:
                   s.show();
                   break;
         case 4:
                   // exit(0); // *** avoid std::exit
                   return 0 ;
                   // break;
         default:
                   std::cout << "Please enter correct choice(1-4)!\n";
                   break;
       }
   }
}
The code that you give still cannot run. How can i fixed it
Use a reasonably current compiler (and for g++: enable C++11 with -std=c++11).
http://coliru.stacked-crooked.com/a/f007daa896fb1f4d

If you are irrevocably saddled with an obsolete compiler, modify lines 44, 45
1
2
3
4
5
        // stack( const stack& ) = delete ; // line 44
        stack( const stack& ) ;

        // stack& operator= ( const stack& ) = delete ; // line 45
        stack& operator= ( const stack& ) ;
Topic archived. No new replies allowed.