Uys of Spades
Uys of Spades

Reputation: 93

How can I get this simple code to update properly?

I am learning how to create my own text-based game. This is nothing of the sort to amaze experienced programmers, but this is frustrating the heck out of me. My code is simple, the objective is this: You have a bar that displays health, mana, and stamina. For simplicity's sake, if the user inputs 'n', I want to subtract 1 from the stamina and display the bar again. This is my code and doesn't matter what you put into it, it doesn't update.

#include <iostream>
#include <string>


int main()
{
    while (true)
    {
        int nHealth=10;
        int nMana=5;
        int nStamina=20;
        char sMovement[] = { 'n', 's', 'w', 'e', 0 };
        std::cout << "Health: " << nHealth << " Mana: " << nMana << " Stamina: " << nStamina << std::endl;

        char chInput;
        std::cin >> chInput;

        if (chInput==sMovement[0])
        {
            nStamina = nStamina - 1;
        }
    }
}

Upvotes: 0

Views: 58

Answers (1)

Uys of Spades
Uys of Spades

Reputation: 93

Nevermind I figured it out, I realized that the code works perfectly, but when it loops back to the While() it resets nStamina back to 20, so it did do the substraction, but it resets so to fix it I declared the variables outside of the loop.

Upvotes: 2

Related Questions