Kodewick
Start learning →
Mini challenge←→0 / 70

Drink a potion

#067Mini challengeStructsPointers6 min

The hero has 50 HP and 2 potions. drink(h) uses one: a potion fewer and 30 more HP, but never above 100. With no potions it does nothing (already written). main drinks three times.

Should show
HP: 100, potions: 0
main.c
#include <stdio.h>

struct Hero {
    int hp;
    int potions;
};

void drink(struct Hero *h) {
    if (h->potions == 0) {
        return;
    }
    // your code here: one potion fewer, 30 more hp, and back to 100 if above
}

int main(void) {
    struct Hero hero = {50, 2};
    drink(&hero);
    drink(&hero);
    drink(&hero);
    printf("HP: %d, potions: %d\n", hero.hp, hero.potions);
    return 0;
}
Ctrl + Enter runs too
Result

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