#whats wronk with my code bru i cant die my loop broken

21 messages · Page 1 of 1 (latest)

whole swift
atomic tuskBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

snow mango
#

jesus christ brother you need to start using functions right now

#

I can't pinpoint the issue with your code, but surely that's one of two things:

  • either your loop conditions don't check what they should (the first loop allows you to fall to 0 health without you dying if you still have energy and/or money)
  • or the stuff your loop conditions are checking isn't being updating correctly
#

it would be much easier to reason about your code if it was organized better, and in that regard I strongly suggest that you start using functions

pseudo quest
# whole swift

do you want to lose when when health reaches 0, hunger reaches 0 AND energy reaches 0, or do you want to lose when either reaches 0?

whole swift
#

i think i kind of solved the problem i added a if statement at the end

sinful juniper
#

betting the problem is with this line: while (health>0 || hunger>0 || energy>0)

#

this means that as long as one of these variables is greater than zero, the game's not going to end

#

you probably want the game to end when any of these variables reaches zero, rather than when all of them reaches zero

#

you can do that like so: while (health > 0 && hunger > 0 && energy > 0)

#

a couple of other notes:

  • the inner loop (the one with while (run) doesn't seem to have a way to set run to false, causing an infinite loop if none of the if-conditions inside it are met
  • using srand(time(0)); multiple times in quick succession may not provide different seeds for the random number generator, and you may end up with the same random number
#
#include <iostream>
#include <ctime>

void printStatus(int health, int hunger, int energy, double money, double bank);
void processRandomEvents(int& health, int& hunger, int& energy, double& money);

int main() {
    std::srand(static_cast<unsigned int>(std::time(nullptr)));  // Seed RNG only once
    int health = 6;
    int hunger = 6;
    int energy = 6;
    double bank = 100;
    double money = 30;
    int turns = 0;

    while (health > 0 && hunger > 0 && energy > 0) {
        processRandomEvents(health, hunger, energy, money);
        printStatus(health, hunger, energy, money, bank);

        int input;
        std::cout << "-";
        std::cin >> input;
        // More code for handling user input and actions (split into functions)
    }

    std::cout << "You survived " << turns << " turns before dying" << std::endl;
    return 0;
}

void printStatus(int health, int hunger, int energy, double money, double bank) {
    std::cout << "\n";
    std::cout << "# # # # # # # # # # # # # # # # # # # #" << std::endl;
    std::cout << "Health: " << health << "/5  Hunger: " << hunger << "/5  Energy: " << energy << "/5  " << std::endl;
    std::cout << "Money: " << money << "  Bank " << bank << std::endl;
    std::cout << "1. Take money from bank \n";
    std::cout << "2. Put money in bank \n";
    std::cout << "3. Buy food to eat \n";
    std::cout << "4. Buy things to use \n";
    std::cout << "5. Go to work \n";
}

void processRandomEvents(int& health, int& hunger, int& energy, double& money) {
    int random = std::rand() % 12;
    if (random == 7) {
        random = std::rand() % 5;
        std::cout << "You got hurt and lost " << random << " health" << std::endl;
        health -= random;
    }
    else if (random == 1 || random == 2 || random == 3) {
        std::cout << "You got robbed and lost " << money / random << "$" << std::endl;
        money /= random;
    }
    energy--;
    hunger--;
    // More code for event processing (you may want to further split into functions)
}
whole swift
#

thank you for telling me som bugs

atomic tuskBOT
#

@whole swift Has your question been resolved? If so, run !solved :)

whole swift
#

idk

#

!solved

atomic tuskBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity