header files and file declarations

i have a program that has a user made header file and a main file. the program works Question is can i make a separate file with just the file definitions and if i can how do i do it. i am not sure how to do a project file. i am using devC++ any help would be appreciated. Thank you
I'm not sure what you mean but a header file usually contains classes and functions.
The header file contains the declarations for my functions. my main file had the main pr0gram and the function defintions. what i was wondering if if there is anyway to put the function declarations in a sperate file from the main program and have them all comiple and the program run properly. I not sure how to make a project file or is there another way to do this can you help me. here is an example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* main.cpp
#include<iostream>
#include "b.h"

int main()
{
   int a;
   do 
   {
       a=myfunc(a);
    }while (a<100);
return;


int my function(int a)
{
   something;
   return a;
} */

could i put the function definiton in another .cpp and have it compile along with the other files the header and the main program?
Just add the
1
2
3
4
int myfunction(int a)
{
...
}


to "b.h"

Also make sure to uncomment your whole program. Currently your program is in a big block comment.
You need to take out the /* at the beginning and use the inline // or add the */ to the end of the line
The header file is not a problem. i have all the declarations and the program works. I want to put the functions definitions which contain the actual code for the functions to a seperate file and have both the files the main .ccp file and the .cpp that have the definitions of the file.

for example

i have files named the following:

main.cpp - this is the file that drives program
def.cpp - this file contains all my function definitions
m.h - This file has all the function declatrations.

i want to compile all thes files into one exe file. i not sure how to do this and not sure i understand project files and how to make and compile them
Its not a good idea to put your function declarations in the header file it should all ways go into its own .cpp file. In order to compile the program you need to first add the m.h header to the def.cpp like this #include"m.h" and also put any libraries you used in the def.cpp file as well.

Once you have done that if you have an IDE such as Microsoft Visual C++ express you just press compile in a command line compiler type in the comiling command and put in both the file name for the main.cpp and def.cpp and hit enter.

ie. gcc main.cpp def.cpp
i have the terms definition and declarations switched. i have a program that already is right with my user defined header file that compiles fine. i am just wondering if i can put the following in its own file and compile all three files at once.

i want to put this in a seperate file. right now they are in the .cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
 float getera(int rs, float i)
{
       float er;
       if (i==0)
       {
         er=0;
       }  
       else er=(rs*9)/i;
 return er;
}       

bool checkip(double fpt)
{
    double ipt,fcpt,param;
    bool error=false;
    param=fpt;
    fcpt = modf (param , &ipt);
    if ((fcpt < .0) || (fcpt >=.21))
    {
       error=true;
    }
    else
     {
         error=false;
      }   
    return error;
}   
   
float calcip(double fpt)
{
  double ipt,fcpt,param;
     param=fpt;
    fcpt = modf (param , &ipt);
    if ((fcpt > .0) && (fcpt <=.1)) 
    {
       param=ipt+.3333;
       fpt=param;
     }
     else 
       if ((fcpt >.1) &&(fcpt <.21))
       {
         param=ipt+.6666;
         fpt=param;
      }
          else
          {
              param=ipt+.0000;
              fpt=param;
          }                  
   return fpt;
}

float calcbavg(int hit,int ab)
{
    float ba;
    if (ab==0)
    {
      ba=0;
     }      
     else ba=float(hit)/float(ab);
    return ba;
}    

int calctotal_bases(int s,int d,int t,int hr)
{
    int tb;
    tb=s+(d*2)+(t*3)+(hr*4);
    return tb;
}   

int getnumsingles(int h,int d,int t,int hr)
{
    int s;
    s=h-(d+t+hr);
return s;    
}

int calctotal_hits(int s,int d,int t, int hr)
{
    int th;
    th=s+d+t+hr;
return th;
}    

float calc_slugper(int ab, int tb)
{
      float sp;
      if (ab==0)
      {
         sp=0;
      }   
      else sp=float(tb)/float(ab);
      return sp;
}     

float calc_sbper(int sb, int t_sba)
{
      float sper;
      if (t_sba==0)
      {
       sper=0;
       }
       else
       {           
         sper=float(sb)/float(t_sba);
       }
      return sper;
}

int calc_totsba(int sb, int cs)
{
    int tsb;
    tsb=sb+cs;
    return tsb;
}

float calcsbruns(int sb, int cs)
{
   float sbr;
   sbr=float((.3*sb))- float((.6*cs));
   if (sbr<0) sbr=0;
   return sbr;
}      

float calcsoratio(int k, int ab)
{
   float kr;
   if (k==0 ||ab==0) 
   {
     kr=0;
   }  
   else
   {
       kr=float(k)/float(ab);
   }
   return kr;
}       
   
float calcobp(int th,int bb,int hp,int ab,int sf)
{
    float obp;
    if (ab+bb+hp+sf==0)
    {
       obp=0;
    }   
    else obp=float((th+bb+hp))/float((ab+bb+hp+sf));
    return obp;
}

float calcrunscre(float obp,int tb)    
{
   float rc;
   rc=obp*tb;
   return rc;
}      
      
int getpa(int ab,int bb,int hp,int sh,int sf,int di)
{
   int pa;
   pa=ab+bb+hp+sh+sf+di;
   return pa;
}      

float calcobpslug(float obp,float slgp)
{
    float obps;
    obps=obp+slgp;
    return obps;
}

float calcisop(int tb,int ab,int th)      
{
    float isp;
    if (ab==0)
    {
      isp=0;
    }
    else isp=(tb-th)/ab;
    return isp;
}      

float calcwhip(float ha,float wa,float ip)
{
   float wi;
   wi=(ha+wa)/ip;
   return ip;
}     

float calcwinper(int w,int l)
{
   float wp;
   int twl;
   twl=w+l;
   if (twl==0)
   {
     wp=0;
    }
    else wp=float(w)/float(twl);
   return wp;
}   


how can i put the main file the header file and a file that contains the above code to compile as one exe file.
I'm assuming that you want the above in its own file. Assume that you have nothing but your function headers in a header.h file and that your functions are in a function.cpp and that the main is in main.cpp all you have to do to compile is include the proper includes and the #include"header.h" in both the function.cpp and main.cpp and comile it will automatically make a single exe file.
Thank you for all your help.
Topic archived. No new replies allowed.