Stack problem
Apr 7, 2015 at 11:45pm UTC
I try to solve this problem but still not succesful.
The number zero still print out?
what should I changed?
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
#include<iostream>
#include<stack>
using namespace std;
int main()
{
int cse,x=0,y=0,i;
int num;
stack<int > mystack;
cin>>cse;
cout<<endl;
while (x<cse)
{
for ( i=0; i<10; i++)
{
cin>>num;
mystack.push(num);
if (num==0)
{
break ;
}
}
cout<<i<<":" ;
while (!mystack.empty())
{
cout<<mystack.top()<<" " ;
mystack.pop();
}
cout<<"\n" ;
x++;
}
return 0;
}
Last edited on Apr 7, 2015 at 11:46pm UTC
Apr 7, 2015 at 11:55pm UTC
Its because you first insert the 0, and then break the loop. You dont want it to allow an insertion of 0 in the first place right? Add an additional if statement saying do not add it if its 0.
1 2 3 4 5 6 7 8 9
cin >> num;
if (num == 0)
{
break ;
}
else
{
mystack.push(num);
}
Topic archived. No new replies allowed.