Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

The Loot

Every monster carries gold, and the struct has a new member for it, gold, so each monster in the vector now has a fourth value. When Aria defeats a monster, she takes its gold. The monster that defeats her keeps its gold, and so do the ones she never fights.

Below the fights, the program prints two more lines: how much gold Aria has, and the names of the monsters she defeated, separated by a comma and a space. Together, they tell how far she got. Aria defeats the Bat, the Goblin and the Orc, which carry 2 + 10 + 25 = 37 gold, so the output ends like this:

Troll: Aria loses.
Gold: 37
Defeated: Bat, Goblin, Orc

The exercise checks other fights too. An Aria with 300 health defeats all five monsters and ends with 177 gold. And an Aria who loses her very first fight has 0 gold, and her last line is Defeated: nobody.

Tips: the gold is Aria's, like her health, so it belongs in her class: a private member that the constructor sets to 0, a method that adds to it, and a getter. Build the list of names as a string with +=, and put the comma before each name except the first: empty() tells you whether the string has a name in it yet.

That's the whole project. Your program describes the monsters with a struct, keeps Aria's health in a class whose methods follow the rules, and builds its report out of strings: the bars, the column and the list of names. In the next lesson, Maps, you'll learn to keep values under names instead of indexes: the price of each item in a shop, or Aria's inventory, which says how many of each item she has.

Instructions

  1. 1

    Below the fights, print how much gold Aria has.

  2. 2

    Below that, print the monsters she defeated, as in the example, or Defeated: nobody when she defeated none.

Back

3/3

  1. 1. The Line-Up
  2. 2. The Fights
  3. 3. The Loot
Next

Try it yourself

Do this exercise right here in your browser. No account needed.