Function, Loop, and String Exercise: Table of Contents Display Read the entire i
ID: 3558369 • Letter: F
Question
Function, Loop, and String Exercise: Table of Contents Display Read the entire instructions before starting. As in assignment 5, submit snapshots of your code for all 5 steps (but not helper steps). This assignment is about using variables and passing parameters. Only the code in main knows book details like title, chapter number, and page number. All the other functions do not assume any particular title, chapter number, or page number. Those values are passed as inputs to produce the table of contents for any book. The resulting output is shown at the end, below In order to use both cout and the string variable type, your cpp file will need lines at the top for both Hinclude kios tream and Hinclude The overall project is to create an app to print the table of contents for a book. Create a main function with 5 string variables, three of which are called sCh1Name, sCh2Name and sCh3Nam e and two which are called sSec3 1Name and sSec3 2Name. Use the chapter names Introduction "Using Variables and "Using If Statements". The sections in the last chapter Chapter 3 are "If and "Else". Here is an example string sch1Name Introduction Step one Create two separate functions, one to print a single chapter number and title, and the other to print a single section number and title. The functions will take the number as an int parameter and title as a string parameter. When the chapter function is called with the number 3 and the title string "Using If Statements" it should print the following line to cout Chapter 3 Using If Statements The section function, when called with the number 2 and the string "Else" should print to cout: Section 2 Else Note that the word "Section" is preceded by two spaces Call the printChapterLine function from your main three times, and printSectionLine twice printChapter Line 1, s Ch Name print ChapterLine 2, s Ch2Name print ChapterLine (3 sCh3Name); printsection Line 1, s, Sec3 1Name); print Section Line 2 s, Sec NameExplanation / Answer
Code:
#include <iostream>
#include <string>
using namespace std;
int numChar(int num){
int numDigits = 0;
while(num > 0){
num /= 10;
numDigits++;
}
return numDigits;
}
void printPageWithDots(int spacesLeft, int pageNumber){
int numchar = numChar(pageNumber);
int num = spacesLeft - numchar;
for(int i=0; i<num; i++){
cout << ".";
}
cout << pageNumber;
cout << endl;
}
void dotHelper(){
for(int i=1; i<=62; i++)
cout << i%10;
}
int printChapterLine(int chapter, string chapName){
cout << "Chapter " << chapter << ": " << chapName << " ";
return (8 + numChar(chapter) + 2 + chapName.size() + 1);
}
int printSectionLine(int section, string secName){
cout << " Section " << section << ": " << secName << " ";
return (10 + numChar(section) + 2 + secName.size() + 1);
}
int main(){
string sCh1Name = "Introduction";
string sCh2Name = "Using Variables";
string sCh3Name = "Using If Statements";
string sSec3_1Name = "If";
string sSec3_2Name = "Else";
printPageWithDots(62 - printChapterLine(1, sCh1Name), 1);
printPageWithDots(62 - printChapterLine(2, sCh2Name), 18);
printPageWithDots(62 - printChapterLine(3, sCh3Name), 29);
printPageWithDots(62 - printSectionLine(1, sSec3_1Name), 30);
printPageWithDots(62 - printSectionLine(2, sSec3_2Name), 37);
dotHelper();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.