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: Buying Potions

This program should print You pay 120 gold, but it stops with the error std::invalid_argument: stoi: no conversion. Click the line with the mistake.

1#include <iostream>
2#include <string>
3
4int main() {
5    std::string command = "buy 4";
6    int price = 30;
7
8    int space = command.find(' ');
9    int amount = std::stoi(command.substr(0, space));
10    std::cout << "You pay " << amount * price << " gold" << std::endl;
11    return 0;
12}
Back

10/11

  1. 1. Building a String
  2. 2. Part of a String
  3. 3. Searching in a String
  4. 4. Splitting a Command
  5. 5. Quiz: Parts of a String
  6. 6. Alphabetical Order
  7. 7. Uppercase, Lowercase and Digits
  8. 8. Changing a String
  9. 9. Numbers in Strings
  10. 10. Quiz: Buying Potions
  11. 11. Summary
Next

Try it yourself

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