Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 118,309 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,710 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



More files than one in Console

 
Reply to this topicStart new topic

More files than one in Console

didair
post 6 Aug, 2008 - 10:58 AM
Post #1


New D.I.C Head

*
Joined: 24 Jul, 2008
Posts: 13


My Contributions


Hello!

Iv'e going to do a program whit more than just one file smile.gif

So i wondering if i just put:
CODE
#include <somenamehere.cpp>


in the library part of the code?

PLZ help!!!! biggrin.gif biggrin.gif biggrin.gif biggrin.gif biggrin.gif pirate.gif
User is offlineProfile CardPM

Go to the top of the page


gabehabe
post 6 Aug, 2008 - 11:05 AM
Post #2


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 3,516



Thanked 73 times

Dream Kudos: 2350

Expert In: (X)HTML, CSS, Batch Scripting, C, C++

My Contributions


No. You would make a .h file with declarations of functions/variables/etc.

You would then define them in a .cpp file.

Then, you would use #include "myHeader.h" to include it in your program.

You can just define things in a .h file, but it can cause errors later on. I tend to stick with a .h and a .cpp file, but if I can get away with it I will sometimes define it in the .h (on days when I feel lazy)

smile.gif

Edit:
Make sure that all the .h and .cpp files are in the correct paths, I tend to store them all in the same place.
However, you can access them from another folder, too. Say, for example, you have a character class, and want to put it in a seperate folder. Your project folder would then look something like:
path/project-folder-name/character/character.h
Now, since it is a path deeper than your actual project, you would include it like this:
#include "character/character.h"
But this is quite a rare and pointless habit, so it's kinda needless.


Another edit:
If anyone's interested, post here with a request for a tutorial on setting up a project, including headers, libs, dlls, etc, and I'll gladly write one smile.gif
User is online!Profile CardPM

Go to the top of the page

didair
post 6 Aug, 2008 - 11:23 AM
Post #3


New D.I.C Head

*
Joined: 24 Jul, 2008
Posts: 13


My Contributions


QUOTE(gabehabe @ 6 Aug, 2008 - 11:05 AM) *

No. You would make a .h file with declarations of functions/variables/etc.

You would then define them in a .cpp file.

Then, you would use #include "myHeader.h" to include it in your program.

You can just define things in a .h file, but it can cause errors later on. I tend to stick with a .h and a .cpp file, but if I can get away with it I will sometimes define it in the .h (on days when I feel lazy)

smile.gif

Edit:
Make sure that all the .h and .cpp files are in the correct paths, I tend to store them all in the same place.
However, you can access them from another folder, too. Say, for example, you have a character class, and want to put it in a seperate folder. Your project folder would then look something like:
path/project-folder-name/character/character.h
Now, since it is a path deeper than your actual project, you would include it like this:
#include "character/character.h"
But this is quite a rare and pointless habit, so it's kinda needless.


Another edit:
If anyone's interested, post here with a request for a tutorial on setting up a project, including headers, libs, dlls, etc, and I'll gladly write one smile.gif


Is it any tutorial or something that shows how to write a .h file?
Or can you show me?
It don't need to be a whole program! Just so i can see how it work smile.gif
User is offlineProfile CardPM

Go to the top of the page

cJr.
post 6 Aug, 2008 - 11:45 AM
Post #4


New D.I.C Head

*
Joined: 10 May, 2008
Posts: 30


My Contributions


gabehabe, I'd be very interested in that tutorial if you don't mind writing it please? It would be very helpful & is sometihng I will need to know for this next year at uni.
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 6 Aug, 2008 - 11:51 AM
Post #5


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 3,516



Thanked 73 times

Dream Kudos: 2350

Expert In: (X)HTML, CSS, Batch Scripting, C, C++

My Contributions


Basically, you would create a .h file.

Then, you would just type declarations of variables, functions, etc.

Basically, it's best to avoid any assignment of variables, functions in there. Take a look at this:
This would be a header file:
cpp
double a,b,c;
double add (double x, double y);

And this would be a .cpp file, used to define them:
cpp
#include "myHeader.h" // the name of the header file

a = 3;
b = 2;
c = 29929;

double add (double x, double y)
{
return x + y;
}


BUT:
We also use header guards on the header file, to prevent it from being included/declared more than once. The most common are:
#pragma once
but more commonly, this:
cpp
#ifndef MYHEADER_H
#define MYHEADER_H
// stuff goes in here
#endif


So, let's revisit that header which we made earlier:
cpp
#ifndef MYHEADER_H
#define MYHEADER_H
double a,b,c;
double add (double x, double y);
#endif


NOTE:
At this point, you might be wondering what this ifndef, define, and endif are. They are called preprocessor directives.

Look at it like this:
ifndef ~ if not defined
define ~ Speaks for itself
endif ~ end of the preprocessor if

Hope this helps smile.gif
User is online!Profile CardPM

Go to the top of the page

Codegamer
post 6 Aug, 2008 - 12:24 PM
Post #6


D.I.C Head

**
Joined: 4 May, 2008
Posts: 127


My Contributions


QUOTE(gabehabe @ 6 Aug, 2008 - 11:51 AM) *

Basically, you would create a .h file.

Then, you would just type declarations of variables, functions, etc.

Basically, it's best to avoid any assignment of variables, functions in there. Take a look at this:
This would be a header file:
cpp
double a,b,c;
double add (double x, double y);

And this would be a .cpp file, used to define them:
cpp
#include "myHeader.h" // the name of the header file

a = 3;
b = 2;
c = 29929;

double add (double x, double y)
{
return x + y;
}


BUT:
We also use header guards on the header file, to prevent it from being included/declared more than once. The most common are:
#pragma once
but more commonly, this:
cpp
#ifndef MYHEADER_H
#define MYHEADER_H
// stuff goes in here
#endif


So, let's revisit that header which we made earlier:
cpp
#ifndef MYHEADER_H
#define MYHEADER_H
double a,b,c;
double add (double x, double y);
#endif


NOTE:
At this point, you might be wondering what this ifndef, define, and endif are. They are called preprocessor directives.

Look at it like this:
ifndef ~ if not defined
define ~ Speaks for itself
endif ~ end of the preprocessor if

Hope this helps smile.gif



You should make an easier tutorial Gabehabe...
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 6 Aug, 2008 - 12:58 PM
Post #7


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 3,516



Thanked 73 times

Dream Kudos: 2350

Expert In: (X)HTML, CSS, Batch Scripting, C, C++

My Contributions


That isn't a tutorial.

I'll assume the two of you are interested?

I'll write one, I think a few people could find it helpful, particulary when it comes to linking libraries for the use of drivers smile.gif
User is online!Profile CardPM

Go to the top of the page

NickDMax
post 6 Aug, 2008 - 01:08 PM
Post #8


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,763



Thanked 37 times

Dream Kudos: 525
My Contributions


Technically adding #include "somecode.cpp" to your code should work. All the #include directive does is add the contents of one file into the current file.

BUT!!! The proper way to do this is to create a header file (somecode.h) which contains all of the function declarations and public structures and classes etc. and then include THAT in your main file.

So lets say for example that I like to have a text centered in my program so I write a function "centerPrintf()"
CODE
int centerPrintf(const char *format, ...)
{
    char buffer[81];
    char myformat[256];
    char *ptr = myformat;
    int len; //used to tell how much of the buffer was used.
    int spaces;
    va_list args; //the argument list
    va_start (args, format); //set up the argument list to be passed to vsprintf()
    //This is how we can pass along our argument list using
    // the vsprintf function.
    len = vsprintf (buffer, format, args);
    spaces = 40 - len/2;
    while (spaces--) { *ptr++ = 0x20; }
    spaces = 0;
    while (buffer[spaces]) { *ptr++ = buffer[spaces++];  }
    *ptr = 0x00;
    vprintf(myformat, args);
    va_end (args);
    return len; //number of characters that we needed
}


So since I want to use this function in more than one program (or I just want to separate out different files to keep my program organized). So I make a header file for the function:
CODE
//Ensure that this header is only included once...
#ifndef _CENTER_PRINTF
#define _CENTER_PRINTF 1

int centerPrintf(const char *format, ...);

#endif


and I put the code into centerPrintf.c
CODE
#include <stdio.h>
#include <stdarg.h>


int centerPrintf(const char *format, ...)
{
    char buffer[81];
    char myformat[256];
    char *ptr = myformat;
    int len; //used to tell how much of the buffer was used.
    int spaces;
    va_list args; //the argument list
    va_start (args, format); //set up the argument list to be passed to vsprintf()
    //This is how we can pass along our argument list using
    // the vsprintf function.
    len = vsprintf (buffer, format, args);
    spaces = 40 - len/2;
    while (spaces--) { *ptr++ = 0x20; }
    spaces = 0;
    while (buffer[spaces]) { *ptr++ = buffer[spaces++];  }
    *ptr = 0x00;
    vprintf(myformat, args);
    va_end (args);
    return len; //number of characters that we needed
}


Now I can create a project and add the centerPrintf.c file to it, and then have my main program include the header
CODE
#include <stdio.h>
#include "centerPrintf.h"

int main() {
    centerPrintf("My Title: %d\n", 10);
    getchar();
    return 0;
}


Tada!

I have broken my program up into two files.

Note that there is more that you can do, for example adding the centerPrintf function to a library and then putting the header file into the standard include directory and then all you need to do to include the file is add "#include <centerPrintf.h>" -- note you use the < > symbols when the header file is in the standard directory, and the quotation marks when it is just in the path.
User is offlineProfile CardPM

Go to the top of the page

Codegamer
post 8 Aug, 2008 - 11:48 AM
Post #9


D.I.C Head

**
Joined: 4 May, 2008
Posts: 127


My Contributions


QUOTE(gabehabe @ 6 Aug, 2008 - 12:58 PM) *

That isn't a tutorial.

I'll assume the two of you are interested?

I'll write one, I think a few people could find it helpful, particulary when it comes to linking libraries for the use of drivers smile.gif


And... when is the tutorial done though...?
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 8 Aug, 2008 - 12:01 PM
Post #10


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 3,516



Thanked 73 times

Dream Kudos: 2350

Expert In: (X)HTML, CSS, Batch Scripting, C, C++

My Contributions


Part 1, adding custom .h files, is already done. Click my contributions, under my name.
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 10/10/08 11:52AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month