The Line-Up
Time for the third project! This checkpoint puts strings, structs and classes from the last three lessons to work, together with the vectors and loops you know from the playlist. As in the other checkpoints, the instructions say what the program should print, and how you write it is up to you. Each exercise also runs your program with other data, so it has to work everything out from the data.
The project. The hero Aria enters an arena where monsters are waiting for her, and she fights them one after another. Over three exercises, your program will present the monsters, play out the fights and add up what Aria wins. Each monster is a struct, and in the next exercise, Aria arrives as an object of a class.
The data. The struct Monster looks like the one in the lesson on structs: a monster has a name, health and attack, which is how much health it takes from Aria with one strike. The vector monsters holds the monsters in the order in which they fight, one per line. Keep these lines as they are: the exercise changes them to try other monsters, and your program has to get those right too. The names have no accented letters, because the program measures them with size(), and a letter such as é takes up two chars.
The task. Before the fights, the arena presents its monsters. Print each monster on its own line: its name, its health bar, a space and its health. The bar has one # for every 5 health, rounded down, as dividing whole numbers does: 45 health makes 9 #, and so does 47. To make the bars easy to compare, they all start in the same column: the longest name is followed by two spaces, and every other name by as many spaces as it takes to reach that column. For these monsters, the program prints:
Bat ## 10
Goblin ###### 30
Orc ######### 45
Troll ############ 60
Dragon ################ 80The longest names, Goblin and Dragon, have 6 characters, so each bar starts after 8 characters. Work that out from the names, don't write 8 into your code: the exercise also gives a monster a longer name, and then all the bars move to the right.
Tips: std::string(3, ' ') from the lesson on strings makes a string of 3 spaces, and std::string(9, '#') a bar of 9. You find the longest name the way you found the longest song in the playlist, only with size() instead of the song's length.
Instructions
- 1
Print the line-up: each monster on its own line, with its name, its health bar and its health.
- 2
Line up the bars, as in the example: two spaces after the longest name, and after the other names as many spaces as it takes for their bars to start in the same column.