problems using header files.

First this program is just a test to see if functions calc and return right. i plan to do more after i get the rest of the functions i need. my problem is i want to make a header file with these functions if i don't inclued the statment
3include "baseball.h"; and uncomment the functions underneat the includes and down at the functions the program works. i have included the baseball.h code here also what am i doing wrong that i can't use the header file to access the fuctions. I am not worried how the oputput looks at this point. Thank you all who answer for your time and effert in advanced.

jay.c (main program)

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
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <math.h>
#include "baseball.h"
using namespace std;
 /* float calcip(double fpart);
  float getera(int runs, float ip);
  float calcbavg(int hits,int atbats);
  int calctotal_bases(int single,int doubles,int triples,int homeruns);
  int getnumsingles(int hits,int doubles,int triples,int homeruns);
  int calctotal_hits(int single,int doubles,int triples,int homeruns);
  float calc_slugper(int atbats, int totalbases);
  float calc_sbper(int steals, int total_sba);
  int calc_totsba(int steals, int caughtsteal);*/
  
int main()
{
 double fpart;  
 int runs,end,hits,atbats, doubles,triples,homeruns,single;
 int totalbases,totalhits, steals, caughtsteal, total_sba;
 float era, ip, fpt, bavg, slugper, sbper;
 end=9;
 do
 {
       cout << "Enter the numbers of runs allowed ";
       cin >> runs;
       cout << "\n";
       cout << "Enter the number of innings pitched ";
       cin >> ip;
       cout << "\n";
       fpart=ip;
       ip=calcip(fpart);
       era=getera(runs,ip);
       cout << "Enter the number of at bats " ;
       cin >> atbats;
       cout << "\n";
       cout << "Enter the number of hits "; 
       cin >> hits;
       cout << "\n";
       bavg=calcbavg(hits,atbats);
       cout << "Enter the number of doubles ";
       cin >> doubles;
       cout << "\n";
       cout <<"enter the number of triples ";
       cin >> triples;
       cout <<"\n";
       cout <<"Enter the number of home runs ";
       cin >>homeruns;
       cout <<"\n";
       cout<<"Enter the number of stolen bases "<<"\n";
       cin >>steals;
       cout<<"\n";
       cout <<"Enter the number of time caught stealing "<<"\n";
       cin>>caughtsteal;
       cout<<"\n";
       total_sba=calc_totsba(steals,caughtsteal);
       sbper=calc_sbper(steals,total_sba);
       single=getnumsingles(hits,doubles,triples,homeruns);
       totalbases=calctotal_bases(single,doubles,triples,homeruns);
       totalhits=calctotal_hits(single,doubles,triples,homeruns);
       slugper=calc_slugper(atbats,totalbases);
       cout <<"The number of at bats = "<<atbats<<"\n";
       cout <<"The number of hits = "<<hits<<"\n";
       cout <<"The numbe of singles = "<<single<<"\n";
       cout <<"The number of doubles = "<<doubles<<"\n";
       cout <<"The numbe of triples= "<<triples<<"\n";
       cout <<"the number of home runs = "<<homeruns<<"\n";
       cout << "The batting average = " << setprecision(3)<< bavg << "\n";
       cout << "The number of Total Bases = " << totalbases <<"\n";    
       cout<<"The number of total stolen base attempts = "<<total_sba<<"\n";
       cout <<"Totalhits= " << totalhits << "\n";
       cout << " The earned run average = "<< fixed <<setprecision(2) << era;
       cout << "\n";
       cout<<" the Slugging Percentage = "<<fixed<<setprecision(3)<<slugper<<"\n";
       cout<<"The stolen base percentage = "<<fixed<<setprecision(2)<<sbper<<"\n";
       cout << " Enter 0 to quit or anything else to continue ";
       cout <<"\n";
       cin >> end;
   }while (end > 0);   
 return 0;   
}    

/* float getera(int rs, float i)
{
       float er;
       er=(rs*9)/i;
 return er;
}       

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;
    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;
      sp=float(tb)/float(ab);
      return sp;
}     

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

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


baseball.h (header file i want to use)

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

float getera(int rs, float i)
{
       float er;
       er=(rs*9)/i;
 return er;
}       

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;
    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;
      sp=float(tb)/float(ab);
      return sp;
}     

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

int calc_totsba(int sb, int cs)
{
    int tsb;
    tsb=sb+cs;
    return tsb;
}float getera(int rs, float i)
{
       float er;
       er=(rs*9)/i;
 return er;
}       

float calcip(float 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;
    cout << hit << "\n" << ab << "\n";
    ba=float(hit)/float(ab);
    cout << setprecision(3) << ba <<"\n";
    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;
}    

Add all of the functions to the baseball.h file and add it to your project. What IDE are you using?
Header files are meant to store only function declarations, not definitions themselves.

You should have a baseball.cpp file and put all of this stuff there and make your baseball.h like this:
1
2
3
4
5
6
7
#ifndef BASEBALL_H
#define BASEBALL_H

float getera(int,int);
/*and so on*/

#endif 


Alternatively, I believe you could inline all those functions and use them as you have them right now, but that's not a very good idea.
Last edited on
Poke386

Let me make sure i understand what your saying the only thing in the .h file should be the first line of a function like we define it at the top of the .cpp file. or do we add the code for the whole function in the .h file because if the .h file contains only the first line of a function than i might as well just keep the functions in the main program which just make it that much bigger and harder to edit. i though the .h files were there to help keep reusable function in one spot and you can call them from the main file and have them readily available to use. or am i misunderstanding something.

eker676
i am using the latest dev c++ i believe 4.92

Thank you for your replies and i hope you can help me further.
Header files are meant to only store function prototypes (what you are calling 'the first line') and documentation on those functions. The actual functions themselves are kept in a separate .cpp file. There are several reasons for this, I'll just give you a couple. Let's say that you are M$, and you have proprietary code. You still want developers to use your functions, but you don't want them to have the source code. You can precompile the .cpp file(the implementation file), and let developers have the .h(header file) as is, so they can see what functions you have written, and how to use them.
Another reason. Say you have 5,000 functions, and you want to let people use them. Do you want them to have to search through thousands of pages of funky code just to see what is there?
No. all they need to look at to use your functions is the prototype, and a short comment, which are the only things present in the header file. This saves everybody a bunch of time.
Although it may seem easier with very simple programs to stuff everything into the header, and it is, it's just bad style. In my classes we were taught from about the second week to start doing it the right way, with separate header and implementation files. Then as your code gets more complex, you are already doing it correctly.
And as for putting everything into your main cpp file, that works ok for tiny programs, but as things get more complex, this is the worst idea of all. If you have your functions working, it is better to have them in a separate file. If they are in your main file, everytime you work on it you run the risk of breaking them. Get them working, throw them in a separate file, and you don't have to worry about them anymore.
Last edited on
Thank you grcunning. in the .h file is should have just the function declarations like this,

for example:

int geta(int d, int f);

or should it look like this

int geta(int, int);

Thank you
Topic archived. No new replies allowed.