Home > front end >  How do I store 1 INT into another or have it stored somewhere so it updates every time the script fi
How do I store 1 INT into another or have it stored somewhere so it updates every time the script fi

Time:01-27

My script is below for a project I'm working on as I learn/practice.

  1. I'd like to know how to keep track of the health. It seems, currently I'm just adding/subtracting/keeping it the same but the system has no no where to actually record the health and I'm not sure how to do it.

  2. Also, what am I doing wrong as I tried to make the script repeat once it's finished but instead, it gives me a blank space instead of giving the Attack/Defend/Heal options again.

I tried the "do" "while" conditions so DO this script WHILE the health is above 0 ---it works (kind of). The script doesn't end which is an improvement BUT it doesn't repeat the Attack/Defend/Heal menu.



#include <iostream>

using namespace std; 

int main()  {


int health = 50;
int Attack = 1;
int Defend = 2;
int Heal = 3;
do {
std::cout << "Please select an option: " << endl;
std::cout << "1. Attack" << endl;
std::cout << "2. Defend" << endl;
std::cout << "3. Heal" << endl;

int  Move;
std::cin >> Move;

if (Move == 1) {
    std::cout << "You did 1 damage" << endl;
    std::cout << "You lost 1 health" << endl;
   // while (health - 1)
    std: cout << "Your current health is: " << health -1 << endl;
    while (health > 0);
}
if (Move == 2) {
    std::cout << "You took no damage" << endl;
    std::cout << "You lost 0 health" << endl;
     cout << "Your current health is: " << health << endl;
    
}

if (Move == 3) {
    std::cout << "You gained 1 health" << endl;
     cout << "Your current health is: " << health  1 << endl;
}
}

while (health > 0);




}```






CodePudding user response:

Remove the first while (health > 0);, that what's called an infinite loop and that is what is giving you the 'blank space'.

The second while (health > 0); is fine because that is the last part of the do .. while loop.

Secondly to store the new health you have to use the health variable. You must assign the new value to this variable, just like you assigned the initial value of 50. E.g.

// assign new health value
health = health - 1;
// display new health value
std::cout << "Your current health is: " << health << std::endl;
  •  Tags:  
  • c
  • Related