error in stacks: no response in stdout

I dont know why the programm doesnt print


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
53
54
55
56
57
58
59
60
61
 #include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stack>
#include <string>
using namespace std;


int main() {
stack<char> F;
stack<char> F1;
stack<char> F2;
int a;
cin>>a;
int b[a];
char word;
for(int i = 0;i<a;i++){
cin>>b[i];
}
while(cin>>word){
F.push(word);
}
while(cin>>word){
F1.push(word);
}
while(cin>>word){
F2.push(word);
}

for(int i=a-1;i>-1;i--){
if(b[i]==2){
char l1=F2.top();
F.push(l1);
F2.pop();
}
else {
char l=F1.top();
F.push(l);
F1.pop();

}
}
while(!F.empty()){
char k=F.top();
cout<<k;
F.pop();
}
while(!F1.empty()){
cout<<F1.top();
F1.pop();
}
while(!F2.empty()){
cout<<F2.top();
F2.pop();
}


return 0;
}
1
2
3
4
while(cin>>word)
{
  F.push(word);
}


This will never end.
Yup ,thought the same.
Times it works
in Olympiads
cause i compete in Olympiads and this works.thanks
But i dont know how many chars will the programm give me so how i will do the input?
How are you meant know when you've been given the last char?
The exercise says i give you N chars and you dont know how many are they,and put them in a stack.How to do that?
Will they tell you what N is?

Do they pass the char one at a time, or all in one go?
all in one go
A robot consisting of a mechanical arm can take a box from the left stack and place it either in stack 1 or in stack 2. We can program the robot by placing a series of numbers consisting of numbers 1 and 2 only. Each number indicates in which stack the box has to be entered.

If given the program that the robot did, find the original layout of the boxes.
Input Format

*Number N.
*A series of N numbers (1 or 2), the program executed by the robot.
*characters "B" (black box) and "W" (white box), the final form of the original stack.
*characters "B" and "W", the final form of the stack 1.
*characters "B" and "W", the final form of the stack 2.
Constraints

1<=Ν<=200

Input (example):
7
2 1 2 1 1 2 1
WWB
WBBBWWB
BWBWW

Output(of example):
BBWWWWBBWW
BBW
WB



This is what the program wants!




Last edited on
Must be solved by stacks!
Last edited on
Imagine you are doing this with pen and paper, write down how you would go about completing this task - what steps would you take? This is called pseudo code and can be very helpful in organizing ones thoughts and the program structure. Start out very general, then go back and refine each part until you are happy to convert to real code.

Edit:
I would interpret that assignment as being able to use something else apart from a stack to store the 1's and 2's . If you put them in a stack, you won't be able to access the stack from the bottom. So try a std::vector like I suggested earlier. Use the stacks for the B and W though.

Good Luck !!

I am off to bed soon, but Repeater is here, so you are in good hands ! Others too :+)
Last edited on
Thanks a lot,TheIdeasMan!
Last edited on
1
2
3
4
while(cin>>word)
{
  F.push(word);
}

This will never end

of course it ends. it ends when there is no more input.
you may observe it by using stream redirection or by sending EOF.
what makes little sense is to keep trying to read after that. (loops 25--27 and 28--30)

however, by looking at your input example, you need to read till end-of-line. use `std::getline()' with a `std::string' or with cin.get() or the noskipws flag and compare against '\0'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int n;
std::cin>>n;
std::vector<int> operations(n);
for(int K=0; K<n; ++K)
   std::cin>>operations[K];
std::cin.ignore(); //to discard the '\n' at the end

string original, stack1, stack2;
std::getline(std::cin, original);
std::getline(std::cin, stack1);
std::getline(std::cin, stack2);

//alternative without std::string
char box;
std::cin>>std::noskipws;
while(std::cin>>box and box not_eq '\n')
   F.push(box);
while(std::cin>>box and box not_eq '\n')
   F1.push(box);
while(std::cin>>box and box not_eq '\n')
   F2.push(box);




by the way, i don't understand your example.
the original layout of stack 2 was `WB', ¿how did it become `BWBWW'? you can only add at one end, so it should start or end with WB, but that is not the case.
Thanks a lot,to you too,ne555,you helped me with solving the problem a lot!

by the way, i don't understand your example.
the original layout of stack 2 was `WB', ¿how did it become `BWBWW'? you can only add at one end, so it should start or end with WB, but that is not the case.


It's difficult to understand the exercise because is from an olympiad:0 :)
But i will explain,i did the exercise with stacks and then i had the same problem with the example (i was confused) cause the program wants to print it how it is not in the correct order!


Topic archived. No new replies allowed.