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; } |
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" Regards, Oliv' |
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? |
cuthbertmd1
You have yet not read the second link in this post of mine? http://www.linuxquestions.org/questi...8/#post4257626 |
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. |
Quote:
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' |
Quote:
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. |
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. |