Thread: some help with loop very noob question i guess?

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    5

    some help with loop very noob question i guess?

    hey guys im just trying to get the function for fighting set up a very basic set up with random numbers, the loop works. but when the number goes down below 0 it starts over i dont understand why or how i can fix this :-/


    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    int fight();
    
    int main()
    {
    
    fight();
    
    system("pause");
    return 0;
    }   
    
    int fight()
    {
    int damage, maxdamage = 18;
    int hisdamage, hismax = 5;
    int health = 40, hishealth = 20;
    srand((unsigned)time(0));
    while(hishealth>=0 && health>=0 )
    {
        damage = (rand()%maxdamage)+1;
        cout <<"You have done "<< damage << " damage. "<< endl;
        result=hishealth-damage;
        cout<<"monster has "<< hishealth <<" hitpoints left." << endl;
    
        hisdamage = (rand()%hismax)+1;
        cout<<"Monster did " << hisdamage << " damage to you!"<< endl; 
        health - hisdamage;
        cout<<"You have "<< health <<" hitpoints left." << endl;
        }
            if(hishealth<=0)
                    {
                        cout<<"You have successfully slain the monster.";
                    }
            else if(health<=0)
                    {
                        cout<<"you were slain by the monster, game over";
                    }
    }

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    9
    what i dont understand is this line

    health - hisdamage;

    what do you do with that one. i am more of a c coder and my understanding of c++ is limited, but i dont understand, what this line should do. you subtract hisdamage from health, but you dont store the value. so the var health doesnt get smaller, the loop goes on.
    i assume you wanted something like this:

    health=health - damage

    or

    health-=damage

    i am not sure, but i think this could be the reason.

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Code:
    while(hishealth>=0 && health>=0 )
    {
        damage = (rand()%maxdamage)+1;
        cout <<"You have done "<< damage << " damage. "<< endl;
        result=hishealth-damage;//where does result come from?
        //^should be hishealth=hishealth-damage
        cout<<"monster has "<< hishealth <<" hitpoints left." << endl;
    
        hisdamage = (rand()%hismax)+1;
        cout<<"Monster did " << hisdamage << " damage to you!"<< endl; 
        health - hisdamage;//should be health=health-hisdamage
        cout<<"You have "<< health <<" hitpoints left." << endl;
        }
            if(hishealth<=0)
                    {
                        cout<<"You have successfully slain the monster.";
                    }
            else if(health<=0)
                    {
                        cout<<"you were slain by the monster, game over";
                    }
    }
    Woop?

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    5
    thanks guys this really helped, i have a minor problem but i think i can fix that and then work on making the function smaller and faster cause the function overhead must be huge on that one

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 08:39 AM
  2. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-29-2008, 11:12 PM
  3. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-06-2006, 11:43 PM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM
  5. for loop question
    By cdalten in forum C Programming
    Replies: 4
    Last Post: 03-20-2006, 09:42 AM