Stacks,String,For.....

Hey, i'm little confused. Somewhere in this code there is a problem i think so. I believe that the problem is in the for which push p in the stack.I need help!

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
 #include<iostream>
#include<algorithm>
#include<stack>
#include<vector>
#include<string>
using namespace std;

int main(){

int a,b,n,i,j;
stack <string> c;
vector <string> d;
string p;

cin>>a;
for(i=0;i<a;i++){
    cin>>n>>b;

for(j=0;j<n;j++){
    c.pop();
}
for(j=0;j<=b;j++){
    cin>>p;
    c.push(p);
}
}
while(!c.empty()){
    d.push_back(c.top());
    c.pop();
}
sort(d.begin(),d.end());
for(i=0;i<d.size();i++)
    cout<<d[i]<<endl;
}
Last edited on
1
2
3
for(j=0;j<n;j++){
    c.pop();
}
At the moment c is empty. Trying to pop an empty stack leads to undefined behavior.
Still wrong :/
What exactly is wrong?
It works for me.
Last edited on
the problem is to find out which cars are still in the ferry boat. It's given a number of the ports, how many cars went out, how many cars came in and the name of the car.
For example:
Input:
3
0 4 Audi Ford Fiat Toyota
2 3 BMW Suzuki Honda
4 2 Mercedes Citroen

Output:
Audi
Citroen
Mercedes
i'm sorry for my poor english
Last edited on
In this case your code works if you replace <= with canonical < in one of the loops (Why did you have it in first place? All other loops uses <)
Now it works!!! Thank you very much!!! :)
Topic archived. No new replies allowed.