LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   using header files: how do you ref and put fumulas in headers files (https://www.linuxquestions.org/questions/programming-9/using-header-files-how-do-you-ref-and-put-fumulas-in-headers-files-862544/)

cuthbertmd1 02-14-2011 04:07 AM

using header files: how do you ref and put fumulas in headers files
 
New to programming having issues reference header files. Not sure how to put the fomula in headers below:

Header: cent_to_fahr.h

#ifndef CENT_TO_FAHR_H
#define CENT_TO_FAHR_H

#define C_TO_F 1.8

double cent_to_fahr(double);


#endif /* CENT_TO_FAHR_H */


Header: fahr_to_cent.h

#ifndef FAHR_TO_CENT_H
#define FAHR_TO_CENT_H

#define F_TO_C 0.55555556

double fahr_to_cent(double);


#endif /* FAHR_TO_CENT_H */


Program

#include <stdio.h>
#include "fahr_to_cent.h"
#include "cent_to_fahr.h"


using namespace std;

/*
*
*/
int main() {
int selection;
double temperature;
double conversion;

printf("1 - Fahrenheit to Celsius\n");
printf("2 - Celsius to Fahrenheit\n");
printf("-------------------------\n");
printf("Your selection is?: ");

//Selection from user
scanf("%d", &selection);

if( selection ==1 || selection ==2)
{
printf("Please enter the temperature you would like to convert: ");
//Scanning in the temperature
scanf("%lf", &temperature);

if(selection == 1)
{
conversion = fahr_to_cent(temperature);
printf("%3.01f Fahrenheit is %3.01f Celsius\n", temperature, conversion);
}

else if (selection == 2)
{
conversion = cent_to_fahr(temperature);
printf("%3.01f Celsius is %3.01f Fahrenheit\n", temperature, conversion);
}
}
//Error message if invalid Selection
else
printf("Invalid selection!\n");


return 0;
}

Oliv' 02-14-2011 05:18 AM

Hello,

In theory you do not do the implementation in header files. Of course you can always have an exception, for example when your function do only one very trivial thing like an assignment, or just one mathematical operation.
So what you should do is to define the .c file. For example, for cent_to_fahr.c:
Code:

#include "cent_to_fahr.h"

double cent_to_fahr(double temp)
{
  /* put your implementation */
}

And just for your information, if your code is some C code, I think that you should remove "using namespace std;" line in your code

Regards,

Oliv'

cuthbertmd1 02-14-2011 06:11 AM

C++ Header file
 
Oliv,

So if you don't put the fomula inside the header file what do you use the header file. Not really understanding how or why you would use a header file. For such simple programs why would you need a header file?

Aquarius_Girl 02-14-2011 06:24 AM

cuthbertmd1

You have yet not read the second link in this post of mine? http://www.linuxquestions.org/questi...8/#post4257626

cuthbertmd1 02-14-2011 06:36 AM

Anisha Kaul,

I remove the < > from the program in the include. But I not understanding header files, I was given the header file and told to use them in the program in the conversion. Not really sure how to implement. I read your secound post and I am still confuse. As you can probably tell I am new to programming.

Oliv' 02-14-2011 07:19 AM

Quote:

Originally Posted by cuthbertmd1 (Post 4257799)
Oliv,

So if you don't put the fomula inside the header file what do you use the header file. Not really understanding how or why you would use a header file. For such simple programs why would you need a header file?

For such a simple program, it is true that the interest is quite limited. But headers are really to create a clean separation between declaration and implementation. A typical example is library. When you use a library to link with your program, you just need header files and the .so (shared object) file. The headers contain the declaration of available functions of your library and the .so file the binary code (compiled implementation).
So in your specific case, you would have a "libconvert_temp.so" generated from "cent_to_fahr.c" and "fahr_to_cent.c" files and the headers. And when you create your program, you just have to include the correct headers, and then link your program with "-lconvert_temp".


Regards,

Oliv'

z1p 02-14-2011 07:23 AM

Quote:

Originally Posted by cuthbertmd1 (Post 4257819)
Anisha Kaul,

I remove the < > from the program in the include. But I not understanding header files, I was given the header file and told to use them in the program in the conversion. Not really sure how to implement. I read your secound post and I am still confuse. As you can probably tell I am new to programming.

Since you say you were given the header file, I am guessing this is an assignment of some kind.

Header files are used to share information across multiple files and to organize your declarations. In this case the header files contains some defines and declarations for some functions.

I expect you are expected to provide the implementation for those functions. Possibly in files of there own.

If that is the case you could create 1 or 2 more files containing the conversion functions.

cuthbertmd1 02-15-2011 06:25 AM

Solved
 
Thanks everyone for their assistants. I finally understand why you would use header files.


All times are GMT -5. The time now is 07:40 PM.