* problem

i have to solve a problem that says if the character is an '*' pop an item from the queue and if it's not push that character into the queue. this is my code but i don't get the right answer.

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

int main(){
 int i;
 string n;
 queue <char> b;

cin>>n;

for(i=0;i<n.size();i++){
    if(n[i]=='*')
        b.pop();
    else
        b.push(n[i]);
}

for(i=0;i<b.size();i++){
    cout<<b.front()<<endl;
    b.pop();

 }
 }
Last edited on
How does the result differ from expected?

Line 15: what if the queue is empty?

Line 20: How about while ( ! b.empty() )
i only had to change the for with the while :) thank you
Topic archived. No new replies allowed.