getting a headache

This is a discussion on getting a headache within the C++ Programming forums, part of the General Programming Boards category; Of course, variable naming is completely up to you, but I just wanted to use the most descriptive name possible ...

  1. #31
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Of course, variable naming is completely up to you, but I just wanted to use the most descriptive name possible so that I could get my idea across as easily as possible

    Code:
    //MyDefinitions.h
    
    
    //This next line will set a cap on the number of Skills and Spells you,
    //as a programmer, will be able to incorporate into your game. You'll
    //have to adjust this number each time you add more Skills or Spells,
    //otherwise your program will crash. Should this number be BIGGER than
    //your actual number, then it won't crash, but you will be wasting memory
    
    //This line would not be enough
    //#define NUMBER_OF_SKILLS_AND_SPELLS 1
    
    //This line would waste space
    //#define NUMBER_OF_SKILLS_AND_SPELLS 100
    
    //This line is just right
    #define NUMBER_OF_SKILLS_AND_SPELLS 5
    //Now, we will have enough room for the following Skills/Spells
    
    //It's important that the numbering goes from 0 to (NUMBER_OF_SKILLS_AND_SPELLS - 1)
    #define DAGGER_SKILL 0
    #define CLUB_SKILL 1
    #define SHIELD_SKILL 2
    #define BARDIC_LEARNING_SPELL 3
    #define MASS_HEAL 4
    
    
    //Note:
    //With this method, the most setup will come from
    //ensuring this file is setup properly. Following that, the
    //rest of your functions will remain small and take up very little
    //space
    Now that we have THAT setup, our Class is about to look much smaller

    Code:
    //cPlayer.h
    
    #ifndef CPLAYER_H
    #define CPLAYER_H
    
    #include "MyDefinitions.h"
    
    class cPlayer
    {
    public:
    	cPlayer(void); //Our Constructor
    
    	void SetValue(int SkillOrSpellID, int Value); //Sets the current value
    	int GetValue(int SkillOrSpellID); //Returns the current value
    private:
    	int SkillAndSpellValues[NUMBER_OF_SKILLS_AND_SPELLS];
    	//Now you hopefully see why the number must be defined properly
    	//in MyDefinitions.h
    
    	//And THAT'S IT for our class
    };
    
    #endif
    Now, we have an array that will hold all of our skill and spell values. So, on to the actual functions:

    Code:
    //cPlayer.cpp
    
    #include "cPlayer.h"
    
    cPlayer::cPlayer(void)
    {
    	int X;
    
    	for(X = 0; X < NUMBER_OF_SKILLS_AND_SPELLS; X++)
    	{
    		SkillAndSpellValues[X] = 0;
    	}
    	//Now all our values have been initialized to 0
    }
    
    void cPlayer::SetValue(int SkillOrSpellID, int Value)
    {
    	SkillAndSpellValues[SkillOrSpellID] = Value;
    }
    
    int cPlayer::GetValue(int SkillOrSpellID)
    {
    	return SkillAndSpellValues[SkillOrSpellID];
    }
    And that's it. Now, to use our class:

    Code:
    //Main.cpp
    
    #include <stdio.h> //Again, for C functions
    #include "cPlayer.h"
    
    int main(void)
    {
    	cPlayer MyPlayer;
    
    	printf("Dagger Skill: %d", MyPlayer.GetValue(DAGGER_SKILL));
    
    	printf("\n"); //Prints a New Line
    
    	MyPlayer.SetValue(DAGGER_SKILL, 50);
    
    	printf("Dagger Skill: %d", MyPlayer.GetValue(DAGGER_SKILL));
    
    	return 0;
    }
    And the expected output would be:

    Dagger Skill: 0
    Dagger Skill: 50
    As you can see, aside from a bigger Definitions file, the rest is relatively compact and clean. Again, this is just another idea, I'm sure there are many more, and better, ways out there. But this is probably the best one that I could think of at the moment.
    Last edited by Epo; 09-16-2005 at 06:46 PM.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  2. #32
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    now, to save definitions.h and such would i just save those as a seperate file then just include them?
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  3. #33
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    now, to save definitions.h and such would i just save those as a seperate file then just include them?
    Exactly
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  4. #34
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    ok i think i understand, do i need to save it as a .h as opposed to a .cpp? and are they seperate files entirely or should they be in the same project? cause ill usually get an error that says cplayer.h no such file or directory, but i noticed you included
    Code:
    # include "definitions.h"
    the way i did it was
    Code:
    # include <definitions.h>
    and im in school right now so i cant really edit my code lol

    Code:
    //This line is just right
    #define NUMBER_OF_SKILLS_AND_SPELLS 5
    //Now, we will have enough room for the following Skills/Spells
    does the 5 at the end say how many "skills" are in the array? so if i made it 50 i could define 50 dif. skills? and you made a cPlayer.cpp file, but never use it in main.cpp
    Last edited by sreetvert83; 09-20-2005 at 06:34 PM.
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  5. #35
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Each file (whether it ends in .cpp, or .h) is it's own file. Basically, each is a seperate text file, with different extensions.

    .h denotes a Header file. A place where you write prototypes and kind of outline/blueprint your functions.

    .cpp denotes a Source Code file. A place where you actually write what the functions are doing, step by step.

    Now, all of the files above are in the same project. They are all created by you.

    Difference between #include <definitions.h> and #include "definitions.h"

    Using <> signs represents that you wish to include a file that is a part of the standard library included with your programming language. Header files such as Stdio.h or String.h come with C, and are located in their own special directory. By using <> signs, the compiler knows where to look when including that file.

    Using " " signs represents that you wish to include a file created by you, which is a part of your project. So whenever you create a header file (such as in this case, definitions.h) you will need the " " signs to signify, look in this project (this directory). Using the <> signs will cause the compiler to look elsewhere, and in most cases, not find the necessary header file.

    so if i made it 50 i could define 50 dif. skills?
    Yes, that number represents how many skills you, as a programmer, will be incorporating into your program. And it directly affects the size of your array (how many skills you can store). This is why I recommend setting up your definitions.h file with all your definitions first, then counting how many skills you have, and adjusting the 50 (or whatever number it is) to the appropriate one.

    and you made a cPlayer.cpp file, but never use it in main.cpp
    For a very good reason. As I mentioned, header files are the outlines/blueprints for functions, etc. So, in this case, cPlayer.h gives the outline for the cPlayer class, and as you noticed, the functions within cPlayer.h are just prototypes, without any actual code to carry out. Whenever the compiler finds prototypes, it will search through the files which are a part of your program for the actual code.

    In this case, it finds all its definitions in cPlayer.cpp. It does this all on its own, and you don't actually have to "include" it anywhere. Classes are generally defined in this way. A .h file for the prototypes/layout. And a .cpp file for the actual code. It just helps to keep it all a little more readable. Of course, in the end, it's up to you, and you can write the full functions within the class, but I find it makes it a bit harder to read.
    Last edited by Epo; 09-21-2005 at 04:57 PM. Reason: Added a >
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  6. #36
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    thanks, that makes it much clearer im sure there will be more questions following, there always is
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  7. #37
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    ok, how do i make it so what they choose gets printed at the end? i mean how can i encorporate that into my previous code?
    edit: i've been thinking, and should i put
    Code:
    	MyPlayer.SetValue(Dagger, "dagger");
    in the switch case, then check each skill id if they are 0 and only print the non 0 ones?
    edit2: grrr this is so frusturationg, im gonna go watch tv
    Last edited by sreetvert83; 09-23-2005 at 04:14 PM.
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  8. #38
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Well, it's up to you.

    At the beginning, each element in the array is being initialized to 0. So, you know if it's still at 0, then they do not possess that skill.

    You could either write ALL the contents of the array with something like:
    Code:
    //C Code Again
    
    //Open a file for writing
    
    for(int X = 0; X < NUMBER_OF_SKILLS_AND_SPELLS; X++)
    {
    	if(SkillAndSpellValues[X] == 0) //Player hasn't learned anything new
    	{
    		//%s denotes "Place a String Here", i.e. the one returned by our function
    		fprintf(MyFile, "%s: Not Learned Yet", ReturnSkillString(X));
    	}
    	else //Player HAS learned something about this skill
    	{
    		//%d denotes "Place an Integer Here", i.e. the SkillAndSpellValues[X] value
    		fprintf(MyFile, "%s: %d", ReturnSkillString(X), SkillAndSpellValues[X]);
    	}
    }
    //Close the file
    This would display EACH skill, and of course it can be modified to meet your needs (I.e. leave out the fprintf(), or C++ equivalent, if you don't want to print the unknown skills.

    The above code would have to be in a Public Member Function of your cPlayer class. SkillAndSpellValues[] is a Private Member Variable Array. And ReturnSkillString() would be a Private Member Function of your class, resembling something like:
    Code:
    char * cPlayer::ReturnSkillString(int SkillID)
    {
    	switch(SkillID)
    	{
    	case DAGGER_SKILL:
    		return "Dagger Skill"
    	. . .
    	}
    	
    	//If SkillID wasn't found
    	return "Unknown Skill";
    }
    Hope that gets ya started.
    Last edited by Epo; 09-28-2005 at 05:13 PM.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  9. #39
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    Code:
    fprintf(MyFile, "%s: %d", ReturnSkillString(X), SkillAndSpellValues[X]);
    is that file i/o?
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  10. #40
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    is that file i/o?
    Sorry, I should've been more specific. Yes it is. You can use whichever File I/O method you wish to use, if you need any help adapting it from my fprintf() way, to yours, just ask, but (I believe) it is pretty straight-forward, and a slight headache at most is all it'll take to figure it out.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  11. #41
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    do i put that code into my class declaration, or the actual code?
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  12. #42
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    The above code would have to be in a Public Member Function of your cPlayer class
    As with all other Member Functions, you would write a prototype in your header file (.h) and actually write the whole function in your source file (.cpp). Hope that clears it up.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. libusb headache
    By cbee in forum C Programming
    Replies: 2
    Last Post: 12-02-2008, 08:27 PM
  2. Headache error message
    By Strait in forum C++ Programming
    Replies: 6
    Last Post: 01-31-2005, 03:48 PM
  3. My headache
    By Morgan in forum C Programming
    Replies: 7
    Last Post: 12-03-2002, 03:33 AM
  4. Pointers + Arrays = Headache
    By Estauns in forum C++ Programming
    Replies: 7
    Last Post: 11-22-2001, 06:53 PM
  5. Linked List headache
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 09-23-2001, 07:42 PM

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