简体   繁体   中英

how do I make an unlimited loop that executes once in c++

I'm creating a game in c++. My current goal is to check the player's health in a forever loop to check if he is dead. While he is, i want it to cout << You are dead. But since the cout << is in a while statement, checking for the health all the time, the result outputs and executes cout << many times. I want it to check for the health forever in a loop, and if the health is < 1, i want it to cout << "You are dead"; only one time. Is there a way to tell the while statement to only execute one time? heres my current while statement:

bool gamerunning = true;

while (gamerunning == true) //While game is running...
{
if (health < 1) //If player is dead...
{
cout << "You are dead";
}
}

Executing this code outputs "you are dead" a bunch of times. I want it to keep checking for the health in this loop but i want the cout to execute once, so it doesnt repeat the same message over and over again. If you can provide a small example on what to do to stop this, it would be highly appreciated. Thanks so much guys!

Sounds like you want to stop the game running when the player is dead, in which case you want this:

while (gamerunning == true) //While game is running...
{
  if (health < 1) //If player is dead...
  {
    cout << "You are dead";
    gamerunning = false;
  }
}

If not, then you can keep another bool that says whether the death output has occurred:

bool deadOutput = false;
while (gamerunning == true) //While game is running...
{
  if (health < 1 && !deadOutput) //If player is dead...
  {
    cout << "You are dead";
    deadOutput = true;
  }
}

Why not something like that?

bool gamerunning = true;

while (gamerunning == true) //While game is running...
{
   if (health < 1) break
}
cout << "You are dead";

If you want to keep your loop you can just add extra bool variable - userDead, make your check only if userDead is false and after writing the output you can set it to true.

How about this?

bool gamerunning = true;
bool output_done = false;

while (gamerunning == true) //While game is running...
{
    if ((health < 1) && (output_done != true)) //If player is dead...
    {
        cout << "You are dead";
        output_done = true;
    }
}

You can use a bool to keep track of whether or not it's happened already:

bool gamerunning = true;
bool printed = false;

while (gamerunning == true) //While game is running...
{
    if (health < 1) //If player is dead...
    {
        if (!printed) {
            cout << "You are dead";
            printed = true;
        }
    }
}

normally you'd have a statemachine....but a simple way is to keep track..

bool gamerunning = true; bool alive = true;

 while (gamerunning == true) //While game is running... { if (health < 1 && alive) //If player is dead... { alive = false; cout << "You are dead"; } } 

bool gamerunning = true;

while (gamerunning == true) //While game is running...
{
    if (health < 1) //If player is dead...
    {
         cout << "You are dead";
         break;
    }
}

or

bool gamerunning = true;

while (gamerunning == true) //While game is running...
{
    if (health < 1) //If player is dead...
    {
         cout << "You are dead";
         gamerunning = false;
    }
}
bool gamerunning = true;
bool playerDead = false

while (gamerunning == true) //While game is running...
{
    if (health < 1) //If player is dead...
    {
       if (!playerDead) 
       {
           cout << "You are dead";
           playerDead = true;
       }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM