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

Quiz: Rarity Names

This program should print Magic Ring: rare, but it prints Magic Ring: legendary. Click the line with the mistake.

1#include <iostream>
2#include <string>
3
4enum class Rarity { Common, Rare, Legendary };
5
6struct Item {
7    std::string name;
8    Rarity rarity;
9};
10
11std::string rarityName(Rarity rarity) {
12    if (rarity == Rarity::Common) {
13        return "common";
14    } else if (rarity == Rarity::Common) {
15        return "rare";
16    }
17    return "legendary";
18}
19
20int main() {
21    Item ring = {"Magic Ring", Rarity::Rare};
22    std::cout << ring.name << ": " << rarityName(ring.rarity) << std::endl;
23    return 0;
24}
Back

11/12

  1. 1. What Is a Struct?
  2. 2. Braces and Default Values
  3. 3. Copies and References
  4. 4. A Vector of Structs
  5. 5. Changing Structs in a Loop
  6. 6. Quiz: Copy or Reference?
  7. 7. Structs and Functions
  8. 8. Returning a Struct
  9. 9. Structs Inside Structs
  10. 10. Enumerations: enum class
  11. 11. Quiz: Rarity Names
  12. 12. Summary
Next

Try it yourself

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