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: Restocking

The function restock should add amount to every item in the shop. But the program prints bread: 2 and potion: 0, as if the function did nothing. Click the line with the mistake.

1#include <iostream>
2#include <map>
3#include <string>
4
5void restock(std::map<std::string, int>& shop, int amount) {
6    for (auto [item, count] : shop) {
7        count += amount;
8    }
9}
10
11int main() {
12    std::map<std::string, int> shop = {{"bread", 2}, {"potion", 0}};
13    restock(shop, 5);
14
15    for (const auto& [item, count] : shop) {
16        std::cout << item << ": " << count << std::endl;
17    }
18    return 0;
19}
Back

9/12

  1. 1. What Is a Map?
  2. 2. Adding and Changing Values
  3. 3. Looping over a Map
  4. 4. Missing Keys
  5. 5. Counting with a Map
  6. 6. Quiz: Counting Loot
  7. 7. Removing Elements
  8. 8. Maps and Functions
  9. 9. Quiz: Restocking
  10. 10. Structs as Values
  11. 11. Unordered Maps
  12. 12. Summary
Next

Try it yourself

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