Need help to modify this code

when user does not enter any value in the stack, and ties to get the top most elements, then a garbage value is displayed. Here, you need to handle this exception in a way that if stack is empty and user tries to get top element, then error message should be display that “Stack is Empty”. Similarly, if user tries to pop elements from an empty stack, an error message “Stack is Empty” should be displayed only once instead of equal to no of pushes in the stack.

Please help me how to do this.

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
 #include<iostream>
#include<conio.h>

using namespace std;

      int max1 = 20;
      int x = 0;
      int k;
/*Stack class*/ 
class stack
             {
             
                   int s[10]; //private visibility
                   
                   public:
                    void push(int);
                    void pop();
                    void top();
                    void empty();

};
 
/*Push method*/
void stack::push(int y) {
      if(x < max1){
               x = x + 1; //incrementing x
               s[x]=y;
 }
     else
            cout<<endl<<"stack overflows..."<<endl;
}
 
/*Pop method*/ 
void stack::pop() {
    
          int item;
        
          if(x >= 0){ //if there is any data in stack
 
    
    item = s[x]; 
    x = x -1; //----one item is lessing from stack
    cout<<endl<<endl<<"popped item :"<<item<<endl<< endl;
}
    else if(item == 0){
    cout << "Stack is empty \n";
      
       } //----if
    }

 

/*top() method*/ 
void stack::top() {
    
         if(x >= 0) { //checking if there is any data in stack
         
                cout<<endl<<endl<<"topmost element :"<<s[x]<<endl; } //-----if
         else if (x < 0){
                 cout<<endl<<"stack underflows..."<<endl; } //--------else
} //---top() end
 
/*method for checking is stack empty*/
void stack::empty() {
       if(x < 0){ //if stack is empty
          cout<<endl<<"stack is empty..."<<endl; 
        } //------if
}

/*main()*/
int main(){

  
      cout << "Important:"<< endl;
      cout<< "First you should insert data in stack, only then you can use pop and top methods";
      cout<< "you have three functions and three tries \n" << endl;
      

  stack s1;  //instentiating object
for(int j = 0; j < 3; j++){
    //--------- for
      int j;
      cout<< endl << endl<<"Please enter 1 to insert data in stack" << endl;
      cout << "1-Push"<< endl;
      cout<<"Please enter 2 if you wanna know topest item in stack" << endl;
      
      cout << "2-top"<< endl;
      cout<<"Please enter 3 if you wanna pop stack" << endl;
      cout << "3-pop"<< endl; 
      cin >> j;
      
        if(j == 1){
           int k;
             for(int i = 0; i < 10; i++){
                 cout << endl <<"please enter data for stack" << "\t";
                 cin >> k;
                              s1.push(k); 
                             
                           } //---------for 
            }  //------ if
        else if (j == 3) {
            for(int i = 0; i < 10; i++){
     
                    s1.pop();}
            } //---------else if
     
      
        else if(j == 2){
            s1.top();
            } //------else if
      } //--------for
     
getch();
} //---------------- main

Line 80 you declare j in the For loop, then declare it again on line 82.

item is tested against on line 45 but if x is not higher or equal 0 (line 38) you are testing against an uninitialized variable.

You should avoid using global variables.
Topic archived. No new replies allowed.