stack

i was taught about stack this week. i tried my best to read the notes, but surprisingly i found it more harder than array. then, i was given a task to write a program using stack. so, here is my coding so far.

i dont know what is wrong. please. help me. :|

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main()
{
	stack <string> abc;
	
	cout<<"Enter a sentence ended up with ';'"<<endl;
	cin>>abc;
	
	while(strcmp(abc,";")){
		cout<<abc;
	}

	return 0;
}
what is your objective?
i got an error "no match for operator >>" and i dont know why and where is wrong.
The stack doesn't work like that. You need a string for input and comparison.

Use the member functions like push(), pop(), top():

http://www.cplusplus.com/reference/stack/stack/?kw=stack
well a stack is a container (i believe FILO). so what you would want to do is something like this:

1
2
3
4
5
6
7
8
std::stack<std::string> fun() {
    std::stack<std::string> foo;
    std::string input;

    while(std::cin >> input) {
        foo.push_back(foo);
    }
}


I would suggest googling how to use std::vector. learning how to use vector is important anyways, and will familiarize yourself with the basics of all of the std containers
Topic archived. No new replies allowed.