Thread: Need help with remembering numbers

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    35

    Need help with remembering numbers

    Ok, so Im making what I call a battle system. I think I pretty much know what I need to know to do it and I've already started working out the logistics of it, but Im not too sure on how you would get it to remember a number. They life points are 100, how do input this number and make its so it will remember whatever would be taken out of it?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    const variables?

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    35

    what?

    Quote Originally Posted by citizen
    const variables?
    what about them?

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    35

    This is all I have so far

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
    int UHP[100]; // Users Hit points
    int UATK = rand()%10; // Users Attack points
    int UDFC = rand()%10; // Users Defence points
    int UACC = rand()%10; // Users Accuracy points
    
    int CHP[100]; // Challengers Hit points
    int CATK = rand()%10; // Challengers Attack points
    int CDFC = rand()%10; // Challengers Defence points
    int CACC = rand()%10; // Challengers Accurasy points
    
    cout<<"As your walking down a path a cloaked swordsman ambushes you.\n";
    cout<<"He then challenges you to a fight and you humbly accept.\n";
    cin.get();
    
    }
    Heres what I was thinking when you type in attack it will see if you hit by ( UACC <= 6 ) if it is then you hit if not you miss. afterwards the challenger would get to use defence if their ( CACC <= 6 ) if not the dont get to defend and it would take the random number generated from UATK and subtract it from CHP. but i cant figure out how you would make it so it would keep it subtracted and not just reset back to 100

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    well life points would change eventually, no? You wouldn't want to make a const variable because it wouldn't change. What you could do is make a class (or struct) for a character, and the construction of the character class would set a member variable "health" to 100.

    Edit: In your code example you are creating an array of 100 ints. That doesn't really represent a health value. Health is a whole number that would get reduced by whole numbers. There is no real way to use an array of 100 ints as a health value.
    Last edited by indigo0086; 05-05-2006 at 06:42 PM.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    35
    would you mind helping me with what I would do then? Im looking over the tuturials and im not seeing it and if i am its just not clicking.

  7. #7
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by SebastionV3
    would you mind helping me with what I would do then? Im looking over the tuturials and im not seeing it and if i am its just not clicking.
    Health is a number, from 0 to how much health you have. Setting it up would be the same as the other variables you have there. What you want is a variable for your current health, and one for your maximum health. The same goes for the enemy.

    int UHP = 100; int MUHP = 100;
    int CHP = 100; int MCHP = 100;

    Then you just take out what you want directly from UHP (current health). UHP = UHP - 10; would remove 10 health points. That could also be written as UHP -= 10; btw. So then the user then has 90/100 health (UHP = 90, and MUHP = 100).
    Warning: Have doubt in anything I post.

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

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    It would look something like this
    Code:
    struct character
    {
        //What the current health of the charcter is
        int health;
        //What they can have health wise
        int max_health;
        //Whatever else here
    };
    
    int main()
    {
        character player;
        
        player.max_health = 100;
        player.health = 100;
        
        return 0;
    }
    Woop?

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    35
    ok yeah haha i have no idea how to work structures yet. kind of only going off the tuturials in this website doesnt exactly teach you everything you need to know.

  10. #10
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by SebastionV3
    ok yeah haha i have no idea how to work structures yet. kind of only going off the tuturials in this website doesnt exactly teach you everything you need to know.
    Nope, but it does teach classes (which are the same as structures in C++). Made it to Lesson 12? You'll get it!
    Warning: Have doubt in anything I post.

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

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    35
    yeah ill figure it out sooner or later. I hit c++ pretty hard and only took me maybe half an hour to figure out enough to make some chat bots and deathclocks and a few death games that I made up

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    35
    Quote Originally Posted by prog-bman
    It would look something like this
    Code:
    struct character
    {
        //What the current health of the charcter is
        int health;
        //What they can have health wise
        int max_health;
        //Whatever else here
    };
    
    int main()
    {
        character player;
        
        player.max_health = 100;
        player.health = 100;
        
        return 0;
    }
    Now is the structure part global?

  13. #13
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    player is just an instance of a type character.

  14. #14
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by SebastionV3
    Now is the structure part global?
    The struct can be accessed anywhere in that file yes. If you declared it in a header file, and included that header file you could use it too. So it is global, but it's generally not referred as global, since thats usually how they are. It's just a data type, like floats and ints, and you have to make an instance of it. Which is what character player; does. player is an instance of character and its scope (can only be used in) is in main() only. You could make it global by removing that part from main(), and doing this:
    Code:
    struct character
    {
        ...
    };
    
    character player;
    or the shortcut:
    Code:
    struct character
    {
        ...
    } player;
    However, there are better alternatives (eg. passing references/pointers). This approach is fine for now, but just keep that in mind.
    Warning: Have doubt in anything I post.

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

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Just think of a struct as a new type, like int or float. This behaviour is emphasized even more in C++ classes. You can create instances of type int and float. You can do the same with structs and classes as well. Variables of user-defined types generally conform to the same basic rules as their built-in counterparts, including scope rules, memory allocation and the works.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 09:15 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM