LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-14-2011, 04:07 AM   #1
cuthbertmd1
LQ Newbie
 
Registered: Apr 2009
Posts: 6

Rep: Reputation: 0
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;
}
 
Old 02-14-2011, 05:18 AM   #2
Oliv'
Senior Member
 
Registered: Jan 2004
Location: Montpellier (France)
Distribution: Gentoo
Posts: 1,014

Rep: Reputation: 36
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'
 
Old 02-14-2011, 06:11 AM   #3
cuthbertmd1
LQ Newbie
 
Registered: Apr 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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?
 
Old 02-14-2011, 06:24 AM   #4
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,619
Blog Entries: 29

Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
cuthbertmd1

You have yet not read the second link in this post of mine? http://www.linuxquestions.org/questi...8/#post4257626
 
Old 02-14-2011, 06:36 AM   #5
cuthbertmd1
LQ Newbie
 
Registered: Apr 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 02-14-2011, 07:19 AM   #6
Oliv'
Senior Member
 
Registered: Jan 2004
Location: Montpellier (France)
Distribution: Gentoo
Posts: 1,014

Rep: Reputation: 36
Quote:
Originally Posted by cuthbertmd1 View Post
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'
 
Old 02-14-2011, 07:23 AM   #7
z1p
Member
 
Registered: Jan 2011
Location: the right coast of the US
Distribution: Ubuntu 10.04
Posts: 80

Rep: Reputation: 23
Quote:
Originally Posted by cuthbertmd1 View Post
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.
 
Old 02-15-2011, 06:25 AM   #8
cuthbertmd1
LQ Newbie
 
Registered: Apr 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Smile Solved

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


Reply

Tags
[c]


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
where to put headers and cpp files vendtagain Programming 9 03-15-2010 08:50 AM
Where do I put header files for a shared library I have written? DiBosco Programming 6 02-28-2010 12:45 PM
Empty header files in debian kernel-headers tree gu1 Linux - General 3 10-06-2009 11:09 PM
Including header files and source files for classes Feenix Programming 8 09-28-2005 10:53 AM
c header files in linux in place of header files in windows? harun_acs Programming 1 03-17-2004 02:24 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration