Home > Back-end >  i'm getting "timeout: the monitored command dumped core" error in c code of stack
i'm getting "timeout: the monitored command dumped core" error in c code of stack

Time:09-22

There are 3 stacks stk0, stk1 and stk2. To distinguish push and pop the program is using push0, push1 and push2 for 3 stacks and similarly pop0, pop1, and pop2. Program ends with stop0, stop1 or stop2 and shows contents of stack0, stack1 or stack2 and then exits. My code works fine for all test cases accept the one i mentioned below.

#include <iostream>
#include <stack>
using namespace std;

int main() {
    stack<string> stk0;
    stack<string> stk1;
    stack<string> stk2;

    while(true) {
        string a;
        cout<<"Give one of options: pop, push, stop\n";
        cin >> a;
        if(a=="push0") {
            string b;
            cin >> b;
            stk0.push(b);
        }
        else if(a=="push1") {
            string b;
            cin >> b;
            stk1.push(b);
        }
        else if(a=="push2") {
            string b;
            cin >> b;
            stk2.push(b);
        }
        else if(a=="pop0") {
            if(!stk0.empty()) {
                string b = stk0.top();
                stk0.pop();
                cout<<"Element popped from stack 0 is: "<<b<<endl;
            }
            else cout<<"Underflow in stack 0\n";
        }
        else if(a=="pop1") {
            if(!stk1.empty()) {
                string b = stk1.top();
                stk1.pop();
                cout<<"Element popped from stack 1 is: "<<b<<endl;
            }
            else cout<<"Underflow in stack 1\n";
        }
        else if(a=="pop2") {
            if(!stk2.empty()) {
                string b = stk2.top();
                stk2.pop();
                cout<<"Element popped from stack 2 is: "<<b<<endl;
            }
            else cout<<"Underflow in stack 2\n";
        }
        else if(a=="stop0") {
            while(!stk0.empty()) {
                cout<<stk0.top()<<endl;
                stk0.pop();
            }
            break;
        }
        else if(a=="stop1") {
            while(!stk0.empty()) {
                cout<<stk1.top()<<endl;
                stk1.pop();
            }
            break;
        }
        else if(a=="stop2") {
            while(!stk2.empty()) {
                cout<<stk2.top()<<endl;
                stk2.pop();
            }
            break;
        }
    }
}

when i input

push0 Agra push1 Jaipur push0 Lucknow push2 Bhopal push1 Ajmer push1 Udaipur pop0 pop1 push2 Indore push0 Meerut stop1

i get timeout: the monitored command dumped core error.

CodePudding user response:

Look at this part of your code:

else if(a=="stop1") {
    while(!stk0.empty()) {
        cout<<stk1.top()<<endl;
        stk1.pop();
    }
    break;
}

When a is stop1, the loop will continuously rotate and stk1 will be popped and its value will be printed, but the loop won't exit because the condition is !stk0.empty() and not !stk1.empty().

You probably wanted to do this:

else if(a=="stop1") {
    while(!stk1.empty()) { // <- Notice change from 'stk0.empty()' to 'stk1.empty()'
        cout<<stk1.top()<<endl;
        stk1.pop();
    }
    break;
}
  • Related