Question: this code you guys gave me doesn't work: #include Imagine there is an unknown data structure that holds a collection of strings. It has two

this code you guys gave me doesn't work: #include Imagine there is an "unknown" data structure that holds a collection of strings. It has two operations defined as follows:
How this data structure works is mysterious. Given a sequence of operations, it may operate like a LIFO stack, or it may not.
Your program must determine whether or not it is operating as a stack given a series of operations and the corresponding returned strings.
Program Input and Output
Input File Format
length of 10. The input is terminated by reaching end-of-file (EOF).
program. You can also use the Stack.h template in this module.
Program Output
For each test case, output one of these lines:
stack
not stack
which will indicate whether or not the series of operations are behaving like a stack (in LIFO order).
Sample Input
Create a text file with the following lines and use it as input into your program.
4
i cat
i dog
r dog
r}\mathrm{ cat
6
sunshine
i apple
i street
r apple
r street
r}\mathrm{ sunshine
2
red
red
i blue
i green
r green Sample Output
When your program reads the above input file, it should output these four lines only:
stack
not stack
stack
not stack
Explanation of the Sample Output
output:
st
most recent string inserted). At that point we know that this is not a stack, so program outputs:
no
At this point your code can skip the remaining lines in that test case and proceed to the next one in the file.
The fourth case inserts two strings, then removes one in the correct order ('green' comes out first). The test case ends and the stack is not empty, so the program outputs:
hot stack Imagine there is an "unknown" data structure that holds a collection of strings. It has two operations defined as follows:
How this data structure works is mysterious. Given a sequence of operations, it may operate like a LIFO stack, or it may not.
Your program must determine whether or not it is operating as a stack given a series of operations and the corresponding returned strings.
Program Input and Output
Input File Format
length of 10. The input is terminated by reaching end-of-file (EOF).
program. You can also use the Stack.h template in this module.
Program Output
For each test case, output one of these lines:
stack
not stack
which will indicate whether or not the series of operations are behaving like a stack (in LIFO order).
Sample Input
Create a text file with the following lines and use it as input into your program.
4
i cat
i dog
r dog
r}\mathrm{ cat
6
sunshine
i apple
i street
r apple
r street
r}\mathrm{ sunshine
2
red
red
i blue
i green
r green
#include
#include
#include
using namespace std;
#define INPUT_FILE "test.txt"
int main(){
ifstream f(INPUT_FILE);
string tmp;
bool failed = false;
if (!f.good()){
cout "Invalid file." endl;
exit(-1);
}
while (true){
getline(f, tmp);
if (f.eof())
return 0;
failed = false;
int n = atoi(tmp.c_str());
stack st;
while (n--){
getline(f, tmp);
char op = tmp[0];
string word = tmp.substr(2);
if (op =='i'){
st.push(word);
} else if (op =='r'){
if (st.empty()|| st.top()!= word){
failed = true;
} else {
st.pop();
}
}
}
if (!st.empty()){
failed = true;
}
if (failed){
cout "not stack" endl;
} else {
cout "stack" endl;
}
}
return 0;
}
the output when I run the code is: Invalid file.
Program ended with exit code: 255
can you help me fix it because it is not letting me input this: 4
i cat
i dog
r dog
r cat
6
i sunshine
i apple
i street
r apple
r street
r sunshine
2
i red
r red
3
i blue
i green
r green
this code you guys gave me doesn't work: #include

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!