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 ...

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

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.

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

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.

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:

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.