stack

terminate early. I dont know what wrong with my program?

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
#include<iostream>
#include<stack>

using namespace std;

int main()
{
	int number;
	stack<int> myStack;
	
	cout<<"Please enter number :";

	
	while(!number=='0')
	{
		cin>>number;
		myStack.push(number);
	}
	
	while(!myStack.empty())
	{
		cout<<myStack.top()<<endl;
		myStack.pop();
	}
	
	
	
	return 0;
}
Last edited on
I'm not familiar with the stack library as of yet, but from what I see on line 14, you should change the condition to number != 0 instead and initialize the number variable so it doesn't cause an error.
while (!number == '0')

First of all. you're trying to use number without initializing it. Second of all. Number is an integer, and you're trying to check if its equal to a character.
Is that like this.
I confuse why we should use char instead we only cin number as integer(int).
another one, what is correct sequence that we must placed the myStack.pop()?
After cout the top() or before cout the top().

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
#include<iostream>
#include<stack>
#include<string>

using namespace std;

int main()
{
	char number;
	stack<char> myStack;
	
	cout<<"Please enter number :";

	
	while(number!='0')
	{
		cin>>number;
		myStack.push(number);
	}
	
	while(!myStack.empty())
	{
		myStack.pop();
		cout<<myStack.top()<<" ";
		
	
	}
	
	
	
	return 0;
}
Topic archived. No new replies allowed.