Thread: Trying to set a max for my variable...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    22

    Post Trying to set a max for my variable...

    Ok, i've been messing around with structures and i made one for stats, like in a game. The problem is i'm trying to max the health and mana, so i use health and maxhealth.....

    Code:
    #include <iostream>
    using namespace std;
    
    //Stat structure.////////////////////////////
    struct statstr
    {
        int str;
        int agil;
        int cons;
        int intel;
        int wis;
        int health;
        int maxhealth;
        int mana;
        int maxmana;
    };
    
    //Asigning the structure to a variable.//////
    statstr stats;
    
    //Function for writing the stats.////////////
    void showstats()
    {
        cout<<"\nStrength:---"<<stats.str<<"\nAgility:----"<<stats.agil
        <<"\nConstition:-"<<stats.cons<<"\nIntellect:--"<<stats.intel
        <<"\nWisdom:-----"<<stats.wis<<"\nHealth:-----"<<stats.health<<"/"
        <<stats.maxhealth<<"\nMana:-------"<<stats.mana<<"/"<<stats.maxmana<<"\n";
    };
    
    int main()
    {
    //Stat structure values.//
    stats.str = 15;
    stats.agil = 15;
    stats.cons = 15;
    stats.intel = 15;
    stats.wis = 15;
    stats.health = 30;
    stats.maxhealth = 30;
    stats.mana = 30;
    stats.maxmana = 30;
    
    //Testing if everything works//
        showstats();
        cin.get();
        
        stats.health -= 20;
        showstats();
        cin.get();
        
        stats.health += 100;
        showstats();
        cin.get();    
        
    //Maxing health////////////////////
    if(stats.health > stats.maxhealth)
    {
        stats.health = stats.maxhealth;
    }   
    }//int main end.
    Also i was wondering, where can i learn all the functions in different librarys on my pc...like the iostream, and what they do?

    Thanks

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    you can have a function in the structure that checks the helth/mana and if is greater it will make it it the same as max health.

    And then run the function whenever the person picks up a health kit or something that will increase the health.
    My Website
    010000110010101100101011
    Add Color To Your Code!

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You didn't ask a question. Your code looks fine.

    If you've learned how to add functions to a struct/class you should do that to your statstr struct (basically make it a full-fledged class). One of the functions could be ChangeHealth that takes the amount to change the health. If that amount makes the health higher than your max, then reset it to your max. Basically the code you have now would go into the function so that you only have to write it once.

    You also don't need the maxhealth variable unless that value might be different for different sets of stats. If it never changes (is always 30) then make it a const int (if you have learned that).

    To find out what the standard library functions do, you need a reference. A book would be great (maybe Josuttis'), but there are online references that are good also (e.g. from SGI or Dinkumware). I think it would be better to follow the book/tutorial/class that you are using to learn and let it introduce you to new library functions as you go. There are too many out there to memorize and many would be difficult for novices.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    22
    I didn't know i could put a function in the structure thanks.
    I need the max health cause it will be changing for each level up.

    Edit: I couldn't get it to work right inside the structure because of where i declared statstr stats;.
    I did get it to work good though now =). Just incase someone wants to know the code...

    Code:
    void hp(int min, int max)
    {
        if(stats.health <= stats.maxhealth)
        {
            stats.health += min;
        }
        if(stats.health > stats.maxhealth)
        {
            stats.health = stats.maxhealth;
        }
        stats.maxhealth += max;
    };
    Last edited by GameGenie; 08-23-2005 at 06:52 PM.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    where can i learn all the functions in different librarys on my pc...like the iostream, and what they do?
    That's one of the most frustrating things about C++... there are no good complete reference books. I remember looking through all of my C/C++ books looking for strtoul(), which is a standard C function. It wasn't in any of the books I had at the time.

    Here are a couple of sites with the complete C++ Standard Library:

    Dinkumware.com
    CPPreference.com

    Those are complete references, but they don't give examples showing how to use the library functions.

    And, you can download the actual C++ ANSI/ISO language standard from ANSI in PDF format for about $20. (For a complete C++ reference, you need the C language standard too... another $20.) Of course, it's very technical (and no examples either).

    If you have a Windows compiler, it has functions that are not part of the standard. For that you can use MSDN and/or get the Petzold book.

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    22
    That's one of the most frustrating things about C++... there are no good complete reference books. I remember looking through all of my C/C++ books looking for strtoul(), which is a standard C function. It wasn't in any of the books I had at the time.

    Here are a couple of sites with the complete C++ Standard Library:

    Dinkumware.com
    CPPreference.com

    Those are complete references, but they don't give examples showing how to use the library functions.

    And, you can download the actual C++ ANSI/ISO language standard from ANSI in PDF format for about $20. (For a complete C++ reference, you need the C language standard too... another $20.) Of course, it's very technical (and no examples either).

    If you have a Windows compiler, it has functions that are not part of the standard. For that you can use MSDN and/or get the Petzold book.
    Thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rearrange the value in a given set from max to min
    By Downnin in forum C++ Programming
    Replies: 14
    Last Post: 07-17-2005, 11:31 AM
  2. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  3. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM
  4. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 03:02 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM