Starting from:
$30

$24

Lab : Iterators Solution

Information




In-class labs are meant to introduce you to a new topic and provide some practice with that new topic.




Topics: Using iterators with STL List




Solo work: Labs should be worked on by each individual student, though asking others for help is permitted. Do not copy code from other sources, and do not give your code to other students. Students who commit or aid in plagiarism will receive a 0% on the assignment and be reported.










Building and running: If you are using Visual Studio, make sure to run with debugging. Do not run without debugging!




Using the debugger will help you nd errors.




To prevent a program exit, use this before return 0;







cin.ignore(); cin.get();







Turn in: Once you're ready to turn in your code, prepare the les by




doing the following: (1) Make a copy of your project folder and name it




LASTNAME-FIRSTNAME-LABNAME. (Example: HOPPER-GRACE-LAB-UNIT-TESTS)




Make sure that all source les (.cpp, .hpp, and/or .h les) and the Makefile les are all present. (3) Remove all Visual Studio les - I only want the source les and Make les. (4) Zip your project folder as LASTNAME-FIRSTNAME-LABNAME.zip



Never turn in Visual Studio les!




Starter les: Download from GitHub.




Grading: Grading is based on completion, if the program functions as intended, and absense of errors. Programs that don't build will receive 0%. Besides build errors, runtime errors, logic errors, memory leaks, and ugly code will reduce your score.




Contents










1.1
About . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3


1.1.1
Setting up the project . . . . . . . . . . . . . . . . . .
3
1.2
Lab speci cations . . . . . . . . . . . . . . . . . . . . . . . . .
3


1.2.1
ReadBook . . . . . . . . . . . . . . . . . . . . . . . . .
3


1.1 About




Iterators are a way we can more e ciently traverse through certain types of data structures, such as linked lists. For this lab, we will have a list of strings to use iterators with.













1.1.1 Setting up the project




Download the starter zip le, LAB-ITERATORS.zip, o GitHub. This zip contains the following:




main.cpp Menu.hpp aesop.txt




fairytales.txt




Make sure your text les are in your project directory so that they can be read.




The LoadBook function is already implemented for you, you just have to implement the ReadBook function according to the instructions to make a book reading program.













1.2 Lab speci cations




1.2.1 ReadBook




The book reader will output one-screen-height of lines of text at a time. A standard console window is 80x30. There will be a menu bar at the bottom of the screen, taking up several lines as well. On the bottom of the page, there will be a count of what line you're currently on, and the total amount of lines in the book.




You'll need two int counters: counter (to decide when to end the page), and lines (the line count for the current progress in the book). You will also want to make an integer named pageLength to store the amount of lines to draw before ending the page (since we have to make space for the bottom





menu.)




Initialize counter and lines to 0, and pageLength to 28. Depending on the size of your console window, you might have to change it around.









After you've created the initial variables, you will begin a loop with your


iterator:
1




list < string :: iterator it ;


2




for ( it = bookText . begin () ; it != bookText . end () ; )


3
{


4
}











This sets the start and the end of the loop, and we will handle whether to move forward or backward within the loop. Display the current line of the book, update the counters, and move the iterator forward:




1
cout << * it << endl ;
// Display line of text
2
counter ++;
//
Line counter for
this page
3












lines ++;
//
Line
counter
for
book
4
it ++;
//
Move
iterator
forward























CS 250


Lab : Iterators














You'll also need an if statement to see if your counter has hit the pageLength,


then display the menu to the user...
1










if ( counter ==
p a g e L e n g t h )




2


{












3


cout << " Line "
<< lines << " of "




4
















<<
bookText . size () << endl << endl ;




5
















6


int
choice =
Menu :: S h o w I n t M e n u W i t h P r o m p t ( {




7




" BACKWARD " ,






8




" FORWARD "






9




} ,
false
) ;






10
































11


if (
choice
== 1
) // ba ck wa rd s




12


{












13




//
Move
the
iterator back p a g e L e n g t h *2 spaces .




14


}












15
































16


counter
= 0;
//
reset line / page counter




17


}


































You cannot use
-= with the iterator, so you'll need another loop to


make the iterator back up pageLength*2 times. Also, make sure to decre-


ment lines by the pageLength amount as well.



Afterward, your book reader program should work, letting you scroll be-tween pages with the 1 and 2 commands.








More products