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
}
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
}
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
Simply return from main.
Code:int main() { bool stillGoing=true; while (1) { // "forever" // stuff if (!stillGoing) return 0; } return 0; }
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 .
...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