Question about classes and structures.

This is a discussion on Question about classes and structures. within the C++ Programming forums, part of the General Programming Boards category; I am making the base values for the stats for each race in my text base rpg. Right now I ...

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Question about classes and structures.

    I am making the base values for the stats for each race in my text base rpg. Right now I have a stucture set up for it, which I though would be able to house all the information. Either I do not understand how class's and structures work or its not meant to do what I am wanting it to do. Here is what I have so far:

    Code:
    struct RaceStats {
           int h_basehp;
           int h_basemp;
           int h_basestr;           // h = human
           int h_baseagi;
           int h_basearmor;
           int e_basehp;
           int e_basemp;          // e = enchanter
           int e_basestr;
           int e_baseagi;
           int e_basearmor;
           int b_basehp;
           int b_basemp;             // b = brute
           int b_basestr;
           int b_baseagi;
           int b_basearmor;
         
           };
    I found out that I can not initialize the values of these variables from inside the structure without getting an error. I tried this
    Code:
    struct RaceStats {
     int h_basehp = 200;
    }
    and it gives me an error.
    ISO C++ forbids initialization of member `h_basehp'
    So I assume either I am doing it wrong or you just cant initialize values from inside the structure. Which would defeat the whole purpose of me using a structure.

    Im I just doing it wrong or would it be better to just make these in the global scope and give them values like this?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int h_basehp = 200;
    
    int main {
    //code here
    }
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    It wouldn't make sense to do so. You could want different initial values for each instance.

    You can stuff your initializations in a constructor's [i]initializer list[/i[, though.

    Code:
    struct RaceStats {
       RaceStats() : h_basehp(200), h_basearmor(100), ... etc ... { }
       // more stuff ...
    };
    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    In MY text-based rpg (unfinished, but playable), I define the struct and declare instances of the struct globally, then in the function that creates a character, it saves values to an instance of the struct depending on the class you chose. Here's my code...

    Code:
        struct statistics {
               string nam; //Name
               int lev; //Level
               int att; //Attack
               int def; //Defense
               int tempdef; //To heighten defense for a turn.
               int agi; //Agility (not currrently used in the program)
               int mon; //Money
               int sta; //Stamina
               int tempsta; //To keep track of player health while regenerating it after battle.
               float gloc; //To keep track of your location in Story Mode.
               float exp; //Experience (for leveling up, not currently used)
        };
        statistics player; //For players stats
        statistics opponent; //For enemy stats during battle
    And my createdata() function:
    Code:
    void createdata(string rac) {
         if (rac == "d" || rac == "D")
         {
                 string dnam;
                 
                 ofstream char1("dnam.txt"); //Name
                 ofstream char2("dlev.txt"); //Level
                 ofstream char3("datt.txt"); //Attack
                 ofstream char4("ddef.txt"); //Defense
                 ofstream char5("dagi.txt"); //Agility
                 ofstream char6("dmon.txt"); //Money
                 ofstream char7("dexp.txt"); //Experience
                 ofstream char8("dsta.txt"); //Stamina
                 ofstream char9("dtst.txt"); //Stamina for use in battles;
                                             //refills to value of sta after battle().
                 ofstream char10("dglo.txt"); //See gloc in struct statistics.
                 cout << endl << "What is your character's name? ";
                 cin >> dnam;
                 char1 << dnam;
                 char2 << "1"; //Starting level
                 char3 << "100"; //Starting attack
                 char4 << "95"; //Starting defense
                 char5 << "100"; //Starting agility
                 char6 << "0"; //Starting money
                 char7 << "1"; //Starting experience
                 char8 << "25"; //Starting stamina
                 char10 << "1"; //Starting game loc
                 char1.close();
                 char2.close();
                 char3.close();
                 char4.close();
                 char5.close();
                 char6.close();
                 char7.close();
         }
         else if (rac == "e" || rac == "E")
         {
                 string enam;
                 
                 ofstream char1("enam.txt"); //Name
                 ofstream char2("elev.txt"); //Level
                 ofstream char3("eatt.txt"); //Attack
                 ofstream char4("edef.txt"); //Defense
                 ofstream char5("eagi.txt"); //Agility
                 ofstream char6("emon.txt"); //Money
                 ofstream char7("eexp.txt"); //Experience
                 ofstream char8("esta.txt"); //Stamina
                 ofstream char9("etst.txt"); //Stamina for use in battles;
                                             //refills to value of sta after battle().
                 ofstream char10("eglo.txt"); //See gloc in struct statistics.
                 cout << endl << "What is your character's name? ";
                 cin >> enam;
                 char1 << enam;
                 char2 << "1"; //Starting level
                 char3 << "50"; //Starting attack
                 char4 << "40"; //Starting defense
                 char5 << "65"; //Starting agility
                 char6 << "0"; //Starting money
                 char7 << "1"; //Starting experience
                 char8 << "20"; //Starting stamina
                 char10 << "1"; //Starting game loc
                 char1.close();
                 char2.close();
                 char3.close();
                 char4.close();
                 char5.close();
                 char6.close();
                 char7.close();
         }
         else if (rac == "h" || rac == "H")
         {
    //...
    //You get the idea.

  4. #4
    1479
    Join Date
    Aug 2003
    Posts
    253
    linkofazeroth, about your createdata function it looks like you are sending each individual statistic to its own file....I may be wrong but thats what it looks like to me. Wouldn't it be better to send all that information to the same file? The reason I ask is because I am working with a 'profile.txt' file that I want to send all the stats for the player to that one specific file. Is there any benifits for sending each stat to its own file or am I misunderstanding the code you wrote for that function?
    Knowledge is power and I want it all

    -0RealityFusion0-

  5. #5
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by linkofazeroth
    In MY text-based rpg (unfinished, but playable), I define the struct and declare instances of the struct globally, then in the function that creates a character, it saves values to an instance of the struct depending on the class you chose. Here's my code...

    Code:
        struct statistics {
               string nam; //Name
               int lev; //Level
               int att; //Attack
               int def; //Defense
               int tempdef; //To heighten defense for a turn.
               int agi; //Agility (not currrently used in the program)
               int mon; //Money
               int sta; //Stamina
               int tempsta; //To keep track of player health while regenerating it after battle.
               float gloc; //To keep track of your location in Story Mode.
               float exp; //Experience (for leveling up, not currently used)
        };
        statistics player; //For players stats
        statistics opponent; //For enemy stats during battle
    And my createdata() function:
    Code:
    void createdata(string rac) {
         if (rac == "d" || rac == "D")
         {
                 string dnam;
                 
                 ofstream char1("dnam.txt"); //Name
                 ofstream char2("dlev.txt"); //Level
                 ofstream char3("datt.txt"); //Attack
                 ofstream char4("ddef.txt"); //Defense
                 ofstream char5("dagi.txt"); //Agility
                 ofstream char6("dmon.txt"); //Money
                 ofstream char7("dexp.txt"); //Experience
                 ofstream char8("dsta.txt"); //Stamina
                 ofstream char9("dtst.txt"); //Stamina for use in battles;
                                             //refills to value of sta after battle().
                 ofstream char10("dglo.txt"); //See gloc in struct statistics.
                 cout << endl << "What is your character's name? ";
                 cin >> dnam;
                 char1 << dnam;
                 char2 << "1"; //Starting level
                 char3 << "100"; //Starting attack
                 char4 << "95"; //Starting defense
                 char5 << "100"; //Starting agility
                 char6 << "0"; //Starting money
                 char7 << "1"; //Starting experience
                 char8 << "25"; //Starting stamina
                 char10 << "1"; //Starting game loc
                 char1.close();
                 char2.close();
                 char3.close();
                 char4.close();
                 char5.close();
                 char6.close();
                 char7.close();
         }
         else if (rac == "e" || rac == "E")
         {
                 string enam;
                 
                 ofstream char1("enam.txt"); //Name
                 ofstream char2("elev.txt"); //Level
                 ofstream char3("eatt.txt"); //Attack
                 ofstream char4("edef.txt"); //Defense
                 ofstream char5("eagi.txt"); //Agility
                 ofstream char6("emon.txt"); //Money
                 ofstream char7("eexp.txt"); //Experience
                 ofstream char8("esta.txt"); //Stamina
                 ofstream char9("etst.txt"); //Stamina for use in battles;
                                             //refills to value of sta after battle().
                 ofstream char10("eglo.txt"); //See gloc in struct statistics.
                 cout << endl << "What is your character's name? ";
                 cin >> enam;
                 char1 << enam;
                 char2 << "1"; //Starting level
                 char3 << "50"; //Starting attack
                 char4 << "40"; //Starting defense
                 char5 << "65"; //Starting agility
                 char6 << "0"; //Starting money
                 char7 << "1"; //Starting experience
                 char8 << "20"; //Starting stamina
                 char10 << "1"; //Starting game loc
                 char1.close();
                 char2.close();
                 char3.close();
                 char4.close();
                 char5.close();
                 char6.close();
                 char7.close();
         }
         else if (rac == "h" || rac == "H")
         {
    //...
    //You get the idea.
    Considering it would be unwise to read the file containing the data every time you wanted to find out how much health the player had, I would make sure if you did it how linkofazeroth does it.. to do it the way Zach L. does it also, and then pass the variables to write to the file, and not just '20'.

    Code:
    struct RaceStats {
           int h_basehp;
           int h_baselvl;
           int h_basemp;
           int h_basestr;           // h = human
           int h_baseagi;
           int h_basearmor;
           int e_basehp;
           int e_baselvl;
           int e_basemp;          // e = enchanter
           int e_basestr;
           int e_baseagi;
           int e_basearmor;
       RaceStats() : h_basehp(200), h_basearmor(100), ... etc ... { }
       // more stuff ...
    } Player, Enemy;
    
    void createdata(string rac) {
         if (rac == "d" || rac == "D")
         {
                 string dnam;
                 
                 ofstream char1("dnam.txt"); //Name
                 ofstream char2("dlev.txt"); //Level
                 ofstream char3("datt.txt"); //Attack
                 ofstream char4("ddef.txt"); //Defense
                 ofstream char5("dagi.txt"); //Agility
                 ofstream char6("dmon.txt"); //Money
                 ofstream char7("dexp.txt"); //Experience
                 ofstream char8("dsta.txt"); //Stamina
                 ofstream char9("dtst.txt"); //Stamina for use in battles;
                                             //refills to value of sta after battle().
                 ofstream char10("dglo.txt"); //See gloc in struct statistics.
                 cout << endl << "What is your character's name? ";
                 cin >> dnam;
                 char1 << dnam;
                 char2 << Player.h_baselvl; //Starting level
                 char3 << Player.h_baseatk; //Starting attack
                 char4 << Player.h_basedef; //Starting defense
                 char5 << Player.h_baseagi; //Starting agility
                 char6 << Player.h_basemon; //Starting money
                 char7 << Player.h_baseexp; //Starting experience
                 char8 << Player.h_basesta; //Starting stamina
                 char10 << Player.h_baseloc; //Starting game loc
                 char1.close();
                 char2.close();
                 char3.close();
                 char4.close();
                 char5.close();
                 char6.close();
                 char7.close();
         }
         else if (rac == "e" || rac == "E")
    I would also consider using classes for the player data, instead of a struct. Have one class be Race, then derive the different type of races from that, 'class Elf : public Race {};'. That way any similar starting variables (ie. baseloc //location) that all races share can be in the Race class, and the others that differ can overload in the derived Elf/Human/etc. classes.

    Just ideas, if you dont understand just ignore me.
    Warning: Have doubt in anything I post.

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

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    No, you understood it right. I don't know how to make the program search in a file for a certain value and pull it up (I have a function called load()), so I'm stuck with separate files. Like, if I have a file that looks like this...

    Code:
    1
    5
    5
    4
    6
    0
    0
    1
    ...I can't (or don't know how to) make the program search for a specific number. Maybe if I could tell a function to look at a certain line in the file and pull that line's value out, that'd be helpful, but I don't know how.

    Did my previous post help you with your problem at all, though?

  7. #7
    1479
    Join Date
    Aug 2003
    Posts
    253
    Quote Originally Posted by linkofazeroth
    Did my previous post help you with your problem at all, though?
    Not really, but your sturct statistics(); function was nice and will help me to shorten my code in the long run. It's a nice structure that I will probably use, and modify a bit, to help create random encounter mobs.

    I think I am gonna stick with the initializer list that Zach L. posted.
    Last edited by RealityFusion; 08-30-2005 at 01:02 PM.
    Knowledge is power and I want it all

    -0RealityFusion0-

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    I created my own function for that, too, called monst(). It's incomplete, buuut...
    (Oh, and struct statistics isn't a function, it's a struct. Similar to a class, actually.)

    Code:
    void monst() {
         int rando = 1+rand()%50;
         if (player.lev >= 1 && player.lev <= 3)
         {
                if (rando <= 5 && rando >= 0)
                {
                        opponent.nam = "Bug";
                        opponent.lev = 1;
                        opponent.att = 3;
                        opponent.def = 2;
                        opponent.agi = 2;
                        opponent.mon = 10;
                        opponent.sta = 10;
                        opponent.exp = 5;
                }
                else if (rando > 5)
                {
                     opponent.nam = "Rabbit";
                     opponent.lev = 1;
                     opponent.att = 3;
                     opponent.def = 3;
                     opponent.agi = 3;
                     opponent.mon = 5;
                     opponent.sta = 15;
                     opponent.exp = 10;
                }
         }
    }

  9. #9
    1479
    Join Date
    Aug 2003
    Posts
    253
    Quote Originally Posted by Dae
    Considering it would be unwise to read the file containing the data every time you wanted to find out how much health the player had, I would make sure if you did it how linkofazeroth does it.. to do it the way Zach L. does it also, and then pass the variables to write to the file, and not just '20'.
    Why would it be unwise to read the file every time you wanted to pull information from it?
    Knowledge is power and I want it all

    -0RealityFusion0-

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Mostly it's memory usage (I think. I'm not too knowledgable on that subject). Keeping an ifstream open, or repeatedly closing and opening one, could potentially use more memory than you want.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I wrote a program with lots of hard-coded information. The executable was about 30KB. Then I removed the info and read it in from a file. The exe was 16KB. There is no loss in speed, and some gain in flexibility.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Quote Originally Posted by dwks
    I wrote a program with lots of hard-coded information. The executable was about 30KB. Then I removed the info and read it in from a file. The exe was 16KB. There is no loss in speed, and some gain in flexibility.
    I'm unable to decipher what the point is. Do you mean reading in from files and leaving the ifstream open is good, or that it's better to use the ifstream quickly (like you open, use, then close)?

  13. #13
    1479
    Join Date
    Aug 2003
    Posts
    253
    Thank you to all who posted, I think I have it working the way I want to now.
    Knowledge is power and I want it all

    -0RealityFusion0-

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Just saying that I like to read information in from a file. You can leave the stream open or close it, I don't think it would make too much difference. Unless another program is using the same file or something.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    No difference from reading from say a struct, or loading a struct from a file and using it? I'll keep that in mind and check that out, that would be nice. But I would assume the stream and functions going on in the background cause some slow effect.
    Warning: Have doubt in anything I post.

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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-17-2005, 08:31 AM
  2. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 07:14 PM
  3. Classes and Structures.
    By jrahhali in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2004, 05:03 PM
  4. Structures, Unions and Classes
    By Makoy in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2004, 02:57 PM
  5. structures fooloing to look like classes
    By samsam1 in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2003, 11:43 AM

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