Old program help

I am working on an old program I had a while back just to understand how it works. So far I have just been looking at a BST I found on this site (http://www.cplusplus.com/forum/general/1551/ and I was trying to change all the ints to strings to be able to sort some strings and ran into a snag. "switch expression of type 'std::string' is illegal" and I'm not sure what they mean since I have included strings in the program already. These are the instructions:

Motivation You are charged with printing the "Table of Content" for a book, where Chapters, sections,
and subsections are printed with their page numbers.
However, this needs to be dynamically implemented, in that you must allow for the author to provide for
additional material, that will be included in the book. Each chapter has zero or more sections, each section
has zero or more subsections, and each subsection will have zero or more paragraph. Only paragraphs will
actually take up space on the printed page, and each paragraph takes between 1 and 5 pages. Your program
needs to be able to add paragraphs to subsections, subsections to sections, sections to chapters, and chapters
to the book.

Some Notes
1. You must implement these with the binary representation of a general tree, you must
use linked pointers, you must use recursion, and you must use C++.
2. You might be asked to insert a chapter, section, subsection, or paragraph `immediately after 0'. How-
ever, there are no chapters, sections, subsections, or paragraphs that have number 0. So this is a
mechanism to populate the particular sub-structure with its rst chapter, section, subsection, or para-
graph.
3. The title are chosen from the text of Hamlet. Each title is four, ve or six words taken from the text,
and are enclosed in double quotes. To strip the extra quotes, you may want to use filein.get();
(or use another method, such as regular expressions from the STL for 5 points extra credit).
4. Some of you have started the project already, and have populated already parts of the ToC, which is
perfectly ne. The new chapters, sections, and subsections can still be inserted
5. You may add functionality for extra credit. However, you must discuss these with me before imple-
menting, and extra credit is limited to a total of 15%



Also a sample section of our input that was given then looks like this:

chap ` the tragedy of` 0
chap `hamlet prince of denmark by william` 0
chap `shakespeare dramatis personae claudius king` 0
chap `of denmark marcellus officer hamlet son` 1
sec `to the former and nephew` 1 0
chap `to the present king` 1
sub `polonius lord chamberlain horatio friend` 1 1 0
sec `to hamlet laertes son to polonius` 4 0
par 1 1 1 1 0
sec `voltemand courtier cornelius courtier rosencrantz` 2 0
sec `courtier guildenstern courtier osric courtier a` 4 1
sec `gentleman courtier a priest` 5 0
par 4 1 1 1 1
sub `marcellus officer bernardo officer francisco a` 1 1 1
chap `soldier reynaldo servant to polonius` 2
prtb



Then it must output to look something like this:

Sample output:
Table of Content
or Limited table of Content
Chapter 1 title 1
1.1 skjdksjd 9
1.1.1 skjdksjd 17
1.1.2 skjdksjd 25
1.1.3 skjdksjd 33
1.2 skjdksjd 41
(the actual output is centered and has spacing like how a real table of contents would)
If anyone has any tips to how I would solve this problem or rebuild this program in steps it would be much appreciated since I'm kinda stuck.
Last edited on
closed account (DEUX92yv)
Is the sample input you're given a text file? Also, in this sample input, what do the numbers at the end of each line mean?

As far as creating the ToC, I'd set up some classes:
1. Table of Contents
- Chapters
2. Chapter
- Sections
- Pointer to next chapter
3. Section
- Subsections
- Pointer to next section
4. Subsection
- Paragraphs
- Pointer to next subsection
5. Possible (ONLY if you need to have the actual text or some text representation of the
paragraphs stored in your table of contents) - Paragraph class
- Text/Sentences/etc.
- Pointer to next paragraph

The pointers make the table of contents essentially a linked list (of linked lists [of linked lists {of linked lists}]). That explains why you need to use (or why it's convenient to use) recursion in printing it out.

Last question: since you're talking about rebuilding, can you post the original code you're working off of?
Oh yes the input is a text file. More specifically a .dat not a .txt and the numbers at the end of each line mean this:

chap "former and nephew to the present" 2
This instruction means that a new chapter is to be created.
The title of the new chapter is former and nephew to the present
The new chapter will be immediately after the current chapter 2. And thus it will be chapter 3. If
there was already a chapter 3, (and 4, and 5, . . . ), then these will now be chapter 4, (and 5, and 6,
. . . ).
The title of a chapter does not take enough space on a page.

sec "king polonius lord chamberlain horatio friend" 5 7
This instruction means that a new section is to be created.
The title of the new section is king polonius lord chamberlain horatio friend
The new section will be immediately after the current section 7 in chapter 5. And thus it will be
section 8 in chapter 5. If there was already a section 8, (and 9, and 10, . . . ) in chapter 5, then this
will now be section 9, (and 10, and 11, . . . ).
The title of a chapter does not take enough space on a page.

sub "to hamlet laertes son to polonius" 3 5 8
This instruction means that a new subsection is to be created.
The title of the new subsection is to hamlet laertes son to polonius
The new subsection will be immediately after the current subsection 8 of section 5 in chapter 3. And
thus it will be subsection 9 of section 5 in chapter 3. If there was already a subsection 9, (and 10, and
11, . . . ), then this will now be subsection 10, (and 11, and 12, . . . ).
The title of a chapter does not take enough space on a page.

par 2 4 6 8 10
This instruction means that a new paragraph is to be created.
Paragraphs do not have titles, but paragraphs take up a number of pages. This particular paragraph
takes 2 pages when printed.
The new paragraph will be in chapter 4, section 6, subsection 8, and will be immediately after the
current paragraph 10. And thus it will be paragraph 11. If there was already a paragraph 10, then
this will now be subsection paragraph 11.

prtb
This instruction means that you need to print the current table of content: all chapters, all sections,
all subsections, correctly indented, and with correct page numbers.

prch 8 3
This instruction means that you need to print the current table of content, but limit yourself to printing
the chapter titles for all chapters (with page numbers), no deeper. Except when you get to chapter 8,
you need to print also all the section titles (with page numbers). Except when you get to section 3,
you also need to print all the subsection titles with page numbers.

I kind of figured I would need classes for each part (chapter, section, sub section, paragraph) I also need one for prtb and prch?

And I guess I should have mentioned that too. I lost a lot of code when my laptop died a few months back but was still able to access some programs descriptions via the internet. If only I had my old program then I would read through it.
closed account (DEUX92yv)
You don't need classes for prtb and prch. These will probably end up as void member functions of the table of contents class (if you create one - it isn't absolutely necessary), or just void functions in your main program file. If you choose not to create a table of contents class, you can just have the linked list of chapters exist in your main(). You'll need a pointer - known commonly as a "head" pointer - to the first chapter, so you don't lose where the table of contents lives in memory.

So prch() can take in any two arguments, right? (Specifying a single chapter and section) Or can it also optionally only take in one (and print all subsections within a chapter, because you didn't specify a section within the chapter)?
Ahh I see. Would it be easier to not use a class to contain the table of contents and just have everything as functions in main or will that only work for the prtb and prch?
Yes it can take in any 2 arguments and for the entire input prch only uses 2 arguments. No more no less.

I only need the inorder traversal code from that bst right? along with the class and what not.
closed account (DEUX92yv)
Yes, it would be easier to not use a class to contain the table of contents. It makes sense to put prtb() and prch() in your main .cpp file (with prototypes and definitions), and call them from main(). I'm blanking on how you would apply a binary tree to this problem, but I haven't worked with them almost at all, so I'm not saying it's impossible! On a different note, your link includes the final ")", so the page linked doesn't exist. You should go fix that to avoid any grumpy people who don't want to see a 404 error :)
Last edited on
and I was trying to change all the ints to strings to be able to sort some strings and ran into a snag. "switch expression of type 'std::string' is illegal" and I'm not sure what they mean since I have included strings in the program already.


This is because switch cases must be constant integral expressions - that is they must be a char or int (or any of the the other int types). I you want to have strings then you will need to use if, else if , and else. You would have to convert the strings to all upper or lower case for it all to make sense. The problem is that the strings must compare exactly - if there is a space on the end then it won't work.
I have one last question. The input I have seems to be something that is giving me a lot of trouble. I don't recall how to read in this problem in a way where a BST could help. What I mean is in the problem I have to read in and basically sort differently for each variable. Chapters I just have to see the 0 and add 1 and that becomes the chapter while sections i have to read in the chapter and section and add 1 to the section and then for subsection i have to read the chapter, section and subsection and increment the subsection by 1 and so on. Also, that information being at the end of the line confuses me since I'm used to sorting using the start of each line. Finally how do I just store the data between the ' ' and associate it with each part I have input so when it comes out of my inorder traversal of my BST the words will just be in order?

EDIT:Thank you all for the help so far btw. I've since fixed that problem also IdeasMan. I not only went back to int to try a different approach but I deleted the code that even had the conflict in the first place as it was for visualization of the BST purposes only.

EDIT2: yeah I fixed that trojan thanks for catching that.
Last edited on
I found a sample linked list program (http://www.cplusplus.happycodings.com/Beginners_Lab_Assignments/code17.html ) but my problem still stands with reading this input correctly. I need to sort the info between the ' ' using the numbers at the end of each line. If the numbers were at the start it would be a lot easier for me but I'm thinking the problem is just a issue of not thinking outside the box. Any suggestions?
Last edited on
Topic archived. No new replies allowed.