enemys wont die

This is a discussion on enemys wont die within the C++ Programming forums, part of the General Programming Boards category; To make my enemys die in my text game, I have told the computer that when the health reaches 0 ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479

    enemys wont die

    To make my enemys die in my text game, I have told the computer that when the health reaches 0 or below it to print the phrase enemy killed, but it is working, it just goes into minuses, like after 0 it changes to -5!! and it will not print the message. here is an example of what I mean, I would print the enitre code but it is too big. Can anyone see where I am going wrong?

    Code:
    #include <iostream>
    
    using namespace std;
    
    void example();
    
    int health = 10;
    
    char attack;
    
    int main()
    {
    example();
    while (health<=0)
    {
    cout << "Enemy has died" << endl;
    break;
    }
    return 0;
    }
    
    void example()
    {
    if (attack == 'a')
    {
    health=health-5;
    cout << "You attacked the enemy and hit!  It lost health!" <<endl;
    cout << endl;
    cout << "It's health is now " << health;
    }
    return;
    }
    If you attack the enemy again, when it's health is 5, it should show the phrase in my while loop, but it just shows health is -5, and seems to ignore the while loop. Am I doing the loop wrong, or should I put it in the function example and not in main? I get no errors in my program that the compiler can see, nor any warnings.. Do you know how I can solve this?? Thanks in advance guys

  2. #2
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Theres lots of room for more efficient methods, but you could do it by putting an if statement in the function that makes the enemy lose damage, before doing the damage, see if its <= 0 (of course this works in this example but it might not in other situations of coding)
    Code:
    void example()
    {
    if (health<=0)
    {
    cout << "Enemy has died" << endl;
    return;
    }
    if (attack == 'a')
    {
    health=health-5;
    cout << "You attacked the enemy and hit!  It lost health!" <<endl;
    cout << endl;
    cout << "It's health is now " << health;
    }
    return;
    }
    Yeah, I dont know how to having it so a while loop is ALWAYS checking if something happeneds, no matter where it is in the code so that it automatically does something when an event happens (health < 0). But I do know its not like that, that while loop wont even be called until after example(); function is finished. I think you can make it so it checks for events all the time (like when you update an fps counter) by using windows or a callback features, but I dont know.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    3
    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    void example();
    int health = 10;
    char attack;
    
    int main()
    {
    	while (health>0)
    	{
    		example();
    	}
    	cout << "Enemy has died" << endl;
    	system("pause");  //This only works on windows and is not the ideal way to pause the program.
    	return 0;
    }
    
    void example()
    {
    	cout << "Type a and press the Enter key to attack ";
    	cin >> attack;
    	if (attack == 'a')
    	{
    		health=health-5;
    		cout << "You attacked the enemy and hit!  It lost health!" <<endl;
    		cout << endl;
    		cout << "It's health is now " << health << endl;
    	}
    	return;
    }
    Not the ideal way of doing that, but something to get you started.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie class question
    By vastgoten in forum C++ Programming
    Replies: 4
    Last Post: 07-31-2008, 02:43 AM
  2. 1337 bible, Gen 11
    By Paz_Rax in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 05-20-2005, 10:40 PM
  3. Pointers (die! die! die! )
    By BatmaN77 in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2002, 07:11 PM

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