C++ cout~text~ if (int<=)

i want it to say sorry you have died if health is equal to or less than 0 and close the program. so if answer is 3 health is 0 and if health is 0 say you have died and close program. This is NOT the full program but i think it is all you should need to help. Thanks!

int health=50;
if (health<=0){
cout << "Sorry you have died" <<endl;
return 0;}



if (answer=='3'){
cout << "You wanted to get your mind off of the fact that you're stranded so you go for a swim." <<endl;
cout << "while you are swimming you see a big dark spot out of the corner of your eye. you turn around and see a shark, its too late, and you are chomped by the beast" <<endl;
health=0;
}
Combine the 2 if statements

1
2
3
4
if ( answer == '3' )
{ ... }
if ( health <= 0 )
{ ... }
well this is the program so i dont think that would work


#include <iostream>

using namespace std;

int main()
{
int health=50; //Automatically sets health to 50
int energy=50; //Automatically sets energy to 50
char username[100];
char answer;
int matches=0;
int coconuts=0;

if (health<=0){
cout << "Sorry you have died" <<endl;
return 0;}
if (energy<=0){
cout << "Sorry you have run out of energy" <<endl;
return 0;}
cout << "Your health is " << health <<endl; //Tells the user their health
cout << "Your energy is " << energy <<endl; //Tells the user their energy
cout << "Please enter a username" <<endl; //Tells the user to enter a user name
cin >> username; //User enters user name
cout << "Your username is now " << username <<endl;
cout << "You wake up one day. Not at home, no you are on an uninhabited island. No one in sight, no food, no water. You are on your own." <<endl;
cout << "After walking around and looking for others, food, water, anything to help you survive, you find a chest, a small chest just poking out of the sand." <<endl;
cout << "Would you like to open the chest?\nt1. [1]Yes\nt2. [1]No" <<endl;
cin >> answer;
if (answer=='1'){
cout << "You opened the chest and found 12 strike anywhere matches!\n 12 strike anywhere mathches added to inventory" <<endl;
matches=matches+12;
}
if (answer=='2'){
cout << "You don't open the chest, as you feel what is inside will be displeasing..." <<endl;
}
cout << "You continue along on your hunt for help." <<endl;
cout << "You have walked all day in search of food and have found nothing.";
energy=energy-5;
cout << "You now have " <<energy << "energy" <<endl;
cout << "would you like to\nt1.[1]Keep looking for food\nt2.[2]build shelter and try to sleep\nt3.[3]Go for a swim." <<endl;
cin >> answer;
if (answer=='1'){
cout << "You continue your search for food, about 20 minutes later you find palm trees.Cocos nucifera, the coconut palm to be exact!" <<endl;
cout << "what do you want to do with these coconuts?\nt1. [1]Take them all.They're all mine!\nt2. [2]Take one and the leave the others for later" <<endl;
cin >> answer;

if (answer=='1'){
cout << "you have taken 11 coconuts" <<endl;
coconuts=coconuts+11;
cout << "Would you like to eat the coconut meat and drink the milk\nt1. [1]Yes [2]No" <<endl;
if (answer=='1'){
cout << "You drink the milk and eat the meat of one coconut. Now as you are extremely tired from the rough day you fall right asleep" <<endl;
coconuts=coconuts-1;
}
if (answer=='2'){
cout << "You save the coconuts until you really need them" <<endl;
}


}
if (answer=='2'){
cout << "You take one coconut, and leave the rest to continue growing on the tree." <<endl;
coconuts=coconuts+1;
cout << "Would you like to eat the coconut now?\nt1. [1]Yes\nt2.[2]No" <<endl;
if (answer=='1'){
coconuts=coconuts-1;}
cout << "You eat the coconuts. You realize that you just wasted a coconut before it was really needed.crap!" <<endl;
if (answer=='2'){
cout << "You think it will be best to save it for later." <<endl;
}
}
}
if (answer=='2'){
cout << "You build a shelter out of sticks and leaves, not the coziest but it's what you've got" <<endl;
cout << "Would you rather\nt1. [1]Go to sleep\nt2. [2]make a fire." <<endl;
cin >> answer;
if (answer=='1'){
cout << "You fall asleep immediately." <<endl;
cout << "~4 hours later, you wake up." <<endl;
energy=energy+5;
cout << "you have restored 5 energy by sleeping"
"You hear scrambling in the woods." <<endl;

cout << "What would you like to do?\nt1. [1]Check it out\nt2. [2]Stay quiet and hide\nt3. [3]Charge!Kill it!" <<endl;
cin >> answer;
if (answer=='1'){
cout << "you see a wild boar runnning at you at full speed, what do you do?\nt1. [1]throw a rock at it.\nt2. [2]Run away" <<endl;
cin >> answer;
if (answer=='1'){
cout << "You hit it spot on.It's dead. would you like to cook it?\nt1. [1]Yes\nt2. [2]No" <<endl;
cin >> answer;
if (answer=='1'){
cout << "You cooked and ate the boar, you have now restored 10 energy" <<endl;
energy=energy+10;
cout << "You now have " << energy << "energy" <<endl;
}
if (answer='2'){
cout << "You leave the boar, because you don't know if it is safe to eat. Better safe than sorry." <<endl;
}
}
if (answer=='2'){
cout << "you tried to run but, while looking back at the boar, you hit a tree and it got you" <<endl;
health=0;
}

}
if (answer='2'){
cout << "You slowly creep away and find a tree to climb, you stay in the tree until the creature is gone." <<endl;
energy=energy-5;
cout << "your energy is now" << energy <<endl; }
if (answer='3'){
cout << "You charge the creature, and see that it is a boar, and kill it.would you like to cook it?\nt1. [1]Yes\nt2. [2]No" <<endl;
cin >> answer;
if (answer=='1'){
energy=energy+10;
cout << "You cooked and ate the boar, you have now restored 10 energy. you have " << energy <<endl;
}
if (answer='2'){
cout << "You leave the boar, because you don't know if it is safe to eat. Better safe than sorry." <<endl;
}
}
}
if (answer='2'){
cout << "You sit by the fire to stay warm, and eat a coconut and drink its milk. You then go to bed" <<endl;
}
}
if (answer=='3'){
cout << "You wanted to get your mind off of the fact that you're stranded so you go for a swim." <<endl;
cout << "while you are swimming you see a big dark spot out of the corner of your eye. you turn around and see a shark, its too late, and you are chomped by the beast" <<endl;
health=0;
}
}
Why wouldn't it work? You can also just terminate the program in that if statement:

1
2
3
4
5
6
7
8
9
10
11
if (answer == '3')
{
   	cout << "You wanted to get your mind off of the fact that "
   	     << "you're stranded so you go for a swim.\n"
   	     << "while you are swimming you see a big dark spot\n"
   	     << "out ofthe corner of your eye. You turn around and\n"
   	     << "see a shark, its too late, and you are chomped by the beast\n"
	
	cout << "Sorry you have died\n";
	return 0;
}
Last edited on
i know but i want it to say that anytime health is <=0
Then you need better control structure in your code. Making use of functions to replace repetitive code and using loops will give you the ability to do this rather than copying the if statement to another line. Something like:

1
2
3
while (health > 0)
{ ... }
cout << "Sorry you have died\n";
soooo what would that look like, i tried it and what i did didnt work
Then you paste what you did here and someone will try to help you
can someone just paste what everything would look like with the fixes? please
There is no point in someone else re-factoring your code. YOU need to learn to use functions and control structures properly before attempting any kind of adventure game (especially text-based). Games like this are a waste of time for a beginner. You will quickly find the logic of even a very simple story line with environment details, player stats, enemies, inventory, etc. is overwhelming until you've learned classes. Stick with the basics until you're ready to tackle this kind of coding.
@SloshyFob:

See this thread:

http://www.cplusplus.com/forum/beginner/121532/

This guy had similar problems. I show an example of how to restructure to make the program more data-driven than code driven.
Topic archived. No new replies allowed.