Kodewick
Start learning →
Basic←→0 / 60

Gift card

#043BasicClasses5 min

A bookshop gift card has balance on it. Write pay(amount): if there's enough, take it off and return true; if not, take nothing and return false (that last line is written). Exactly enough counts as enough.

Examples
card_left(50, {20, 40, 10})→20
card_left(30, {})→30
The functionint card_left(int start, const vector<int> &buys)
main.cpp
#include <vector>
using namespace std;

class GiftCard {
public:
    int balance;

    GiftCard(int start) {
        balance = start;
    }

    bool pay(int amount) {
        // your code here: if there's enough, pay and return true
        return false;
    }
};

int card_left(int start, const vector<int> &buys) {
    GiftCard card(start);
    for (int b : buys) {
        card.pay(b);
    }
    return card.balance;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.