Stock span code problem

code link: https://pastebin.com/MD8GSZhJ
why whenever I use empty function of stack stl , I always get segmentation error , please correct it.

Hey @Divya_321 Whenever you use stack and a while loop please write s.empty() statement before s.top().
So,

while(s.top() < a[i] && !s.empty())

would become

while(!s.empty() && s.top() < a[i])

if something is not clear or you have any query please feel free to ask.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

Hey @keshavgupta0103
codeLink:https://pastebin.com/4Jp2hB0z
now I am not getting a segmentation fault but wa please look into my code and explain why it is failing

Hey @Divya_321 the problem i=was that you werent taking input and you were comparing s.top() with a[i] but you need to compare a[s.top()]
Here is the link of code with mistaked rectified with comments: