I'm trying to solve the exercise 22 (chapter 4) from Thinking in C++ but there's something that I'm missing because also after a few days of work, my solution doesn't do it's job. I don't like so much to ask for help in solving exercises, but in this moment I'm feeling overwhelmed.
Create a Stack that holds Stashes. Each Stash will hold five lines from an input file. Create the Stashes using new. Read a file into your Stack, then reprint it in its original form by extracting it from the Stack.
#include "CppLib.h"
#include "Stack.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
//typedef unsigned int uint;
int main() {
ifstream in("main.cpp");
Stack stackStashes;
stackStashes.initialize();
Stash linesStash;
linesStash.initialize(sizeof(char) * 80);
string line;
bool flag = true;
while (flag) {
for (int i = 1; flag && (i <= 5); i++)
if ((flag = (bool)getline(in, line)))
linesStash.add(line.c_str());
if (flag) {
stackStashes.push(new Stash(linesStash));
linesStash.cleanup();
linesStash.initialize(sizeof(char) * 80);
}
}
Stash* s;
char* cp;
int z = 0;
while ((s = (Stash*)stackStashes.pop()) != 0) {
while ((cp = (char*)s->fetch(z++)) != 0)
cout << "s->fetch(" << z << ") = "
<< cp << endl;
delete s;
}
s->cleanup();
stackStashes.cleanup();
return 0;
}
I tried to solve it with vector, without using of flag, all of my solutions returned an error. Moreover, in all my experiment, this is oneo f the worse, but is the only one left.
Here are the libraries provided by the book. All the code below is written by Bruce Eckel.
CppLib.cpp, CppLib.h, Stack.cpp, Stack.h, require.h.