Ending the app when a bool becomes false?

This is a discussion on Ending the app when a bool becomes false? within the C++ Programming forums, part of the General Programming Boards category; I havn't programmed in a while and I forgot how to declare a bool and how to end a program ...

  1. #1
    Unregistered
    Guest

    Ending the app when a bool becomes false?

    I havn't programmed in a while and I forgot how to declare a bool and how to end a program when the bool becomes false. For example:

    bool goes here

    int main()
    {
    say if the player's health becomes -= 0 end the program
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Could try this
    Code:
    int main ( void )
    {
      bool bDone = false;
      while ( !bDone ) // Equivalent to bDone == false
      {
         // Execute your game code....
         // When the player's health becomes < 0, 
         // Set the boolean to true
      }
    
      return ( 0 );
    }
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Simply return from main.

    Code:
    int main() {
        bool stillGoing=true;
        while (1) { // "forever"
             // stuff
             if (!stillGoing) return 0;
       }
       return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    Unregistered
    Guest
    Thanks guys, that worked but I'm having another problem, first I'll give you my code:

    #include <stdlib.h>
    #include <iostream.h>
    #include <time.h>



    int main()
    {
    srand(time(NULL));
    int pgh=rand() % 25;
    int mgh=rand() % 10;

    bool alive=true;
    bool malive=true;
    while(alive==true && malive==true)
    {
    int playerhealth=100;
    int monsterhealth=20;

    int choice;
    cout<<"What do you want to do?\n";
    cout<<"1. Attack";
    cin>>choice;
    if(choice==1)
    {
    cout<<playerhealth;
    cout<<" Health";
    cout<<"\nYou hit the monster for "<<mgh;
    cout<<" HP!\n";
    }
    monsterhealth=monsterhealth-mgh;
    cout<<monsterhealth;
    if(monsterhealth<=0)
    {
    cout<<"\n\n\n\nYou Win!";
    malive=false;
    }
    cout<<"\nthe monster hits you for "<<pgh;
    cout<<" HP!\n";
    playerhealth=playerhealth-pgh;
    cout<<playerhealth;
    if(playerhealth<=0)
    {
    cout<<"You have died";
    alive=false;
    }
    }
    return 0;
    }


    Now, when the player or monster gets hit, it subtracts it properly but if the player/monster doesnt die in 1 hit, you have to hit it again and again until it dies, BUT it seems like the playerhelath and monsterhealth int's dont get looped or somthing so It only counts the first hit. Please help .

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    30

    Well...

    ...from what I can see, the problems are these: pgh and mgh are only randomized once. They should be randomized every loop, lest the damage be the same every loop. And also, the health variables should be declared outside the loop. As it is, they're reset to 100 and 20 each loop.
    Fix these things and it should be OK.
    "You... Remarkably made it rain. Rain of blood." - Tomoe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing params between managed c++ and unmanaged c++
    By cechen in forum C++ Programming
    Replies: 11
    Last Post: 02-03-2009, 08:46 AM
  2. BIG problem. How do I find a bug in so much code?
    By Yarin in forum C++ Programming
    Replies: 44
    Last Post: 01-31-2008, 12:39 PM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 11:50 AM
  4. Problem displaying bitmaps
    By batman123 in forum Game Programming
    Replies: 2
    Last Post: 01-09-2005, 02:01 AM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 05:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21