how i write .h file for .cpp?

how i write fix2float.h for this program?

-------------------------------------------------------------------------------
main.cpp

#include <iostream>
#include <string>
#include fix2float.h
using namespace std;

bool tester (string,string,float,int);

int main ()
{
int z;
//string* a,b;
z = tester ("empty chain","",0.0,-6);
cout << "The result is " << z;
return 0;
}

bool tester (string, string b, float c, int)
{
float w;
w = fix2float (b);
return w==c;
}

-------------------------------------------------------------------------------

fix2float.cpp (you dont need to read it, i'll just say that it
turns string to float)

#pragma once
#include <iostream>
#include <tchar.h>
#include <ostream>
using namespace std;
#include <math.h>
float fix2float(char * n)
{
int ll =0;
while (n[ll] != 0x00)ll++;
ll -=1;
int js =0;
float f =0.0;
int sign =1;
float frac =0.1;
if (ll < 0)return 0.00; // like in empty chain
if ('-'==n[0]) // if the first ch is -
{ sign = -1;
js =1; // if there are - or +, js is 1, otherwise 0
}
if ('+' == n[0])js =1; // if the first ch is +
int jn = js; // like js: if there are - or +, jn will be 1, otherwise 0
for (int jk =js; jk <= ll;jk++) // if there are - or +, jk will move on the input from the second ch, otherwise from the first ch
{
jn =jk; // at first, if there are - or +, jn like jk, will start from the second ch and will be 1, otherwise it will start from the first ch and will be 0
if(46 == int(n[jk]))break;//dot constant // if we see dot we break
f=f*10+(int(n[jk])-48);//add binary number not character! // moving one place right and add the binary
}
if(jn == ll)return sign*f;//dot is the last character // in the previous loop we searched all the input and we found dot in the last ch
js = jn+1; //we got here if there is a dot in the middle of the chain. now js is the place after the dot
for (int lk = js;lk<=ll;lk++) // we search from the place after the dot until the end
{
f =f + frac*(int(n[lk])-48);
frac = frac*0.1;//as fraction part grows its multiplier decreases
}
return sign*f;
}

-------------------------------------------------------------------------------

debug output


1>------ Build started: Project: fix2float, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>c:\users\a\documents\visual studio 2008\projects\fix2float\fix2float\main.cpp(3) : error C2006: '#include' : expected a filename, found 'identifier'
1>c:\users\a\documents\visual studio 2008\projects\fix2float\fix2float\main.cpp(3) : fatal error C1083: Cannot open include file: '': No such file or directory
1>Build log was saved at "file://c:\Users\A\Documents\Visual Studio 2008\Projects\fix2float\fix2float\Debug\BuildLog.htm"
1>fix2float - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
Try putting it between brackets, or in between quotation marks, maybe that will help:

#include <fix2float.h>

or:

#include "fix2float.h"
Last edited on
Using angle brackets (#include <header.h> ) instructs the compiler to search for the header file amongst the standard header files.

Using double quotes (#include "header.h" ), instructs the compiler to search for the header file in the same directory where it was called (in this case the same directory where main.cpp is located).

In your case it should be enclosed in double quotes #include "fix2float.h" .

You can find more information about it here: http://www.cplusplus.com/doc/tutorial/preprocessor/ and here http://msdn.microsoft.com/en-us/library/36k2cdd4%28v=vs.71%29.aspx .

I hope this helps!

P.S: Next time you post any code in these forums, please you the code tags!!! Using code tags makes it easier for us to help you. [ code ]your code[ /code ] (without spaces), becomes your code.
The angle brackets are for header files found in the search path of the compiler. The quotation marks are for header files found in the relative directory of the build project.
I didn't know that either, but it makes sense in hindsight. thanks :)
thank you. in class we got the function fixtfloat.cpp already prepared and we needed to
write main.cpp that uses it. i wrote the main but i dont know what how to write fixtfloat.h.


main.cpp (i wrote)

#include <iostream>
#include <string>
#include "fixtfloat.h"
using namespace std;


bool tester (string,string,float,int);

int main ()
{
bool z; //
//string* a,b;
z = tester ("empty chain","",0.0,-6); //
cout << "The result is " << z;
z = tester ("simple chain","123",123.0,-3);
cout << "The result is " << z;
z = tester ("chain starts with +","+4567",4567.0,-2);
cout << "The result is " << z;
z = tester ("chain starts with -","-98765",-98765.0,-1);
cout << "The result is " << z;
z = tester ("chain starts with .",".3798",.3798,-6);
cout << "The result is " << z;
//z = tester ("chain with . in the middle","45.76","45.76",-4);
//cout << "The result is " << z;
z = tester ("chain ends with .","9753.",9753.0,-2);
cout << "The result is " << z;
z= tester ("chain with length of 10","1234567890",123456700,4); //
cout << "The result is " << z;

return 0;
}

bool tester (string, string b, float c, int)
{
float w;
w = fixtfloat (b);
return w==c;
}

----------------------------------------------------------------------------------------------------------------------

fixtfloat.cpp (you dont need to read it, we got this already prepared in
class. i'll just say that it turns string to float)


#pragma once
#include <iostream>
#include <tchar.h>
#include <ostream>
using namespace std;
#include <math.h>
float fixtfloat(char * n)
{
int ll =0;
while (n[ll] != 0x00)ll++;
ll -=1;
int js =0;
float f =0.0;
int sign =1;
float frac =0.1;
if (ll < 0)return 0.00; // like in empty chain
if ('-'==n[0]) // if the first ch is -
{ sign = -1;
js =1; // if there are - or +, js is 1, otherwise 0
}
if ('+' == n[0])js =1; // if the first ch is +
int jn = js; // like js: if there are - or +, jn will be 1, otherwise 0
for (int jk =js; jk <= ll;jk++) // if there are - or +, jk will move on the input from the second ch, otherwise from the first ch
{
jn =jk; // at first, if there are - or +, jn like jk, will start from the second ch and will be 1, otherwise it will start from the first ch and will be 0
if(46 == int(n[jk]))break;//dot constant // if we see dot we break
f=f*10+(int(n[jk])-48);//add binary number not character! // moving one place right and add the binary
}
if(jn == ll)return sign*f;//dot is the last character // in the previous loop we searched all the input and we found dot in the last ch
js = jn+1; //we got here if there is a dot in the middle of the chain. now js is the place after the dot
for (int lk = js;lk<=ll;lk++) // we search from the place after the dot until the end
{
f =f + frac*(int(n[lk])-48);
frac = frac*0.1;//as fraction part grows its multiplier decreases
}
return sign*f;
}

---------------------------------------------------------------------------------------------------------------------

debuge output

1>------ Build started: Project: fix2float, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>c:\users\a\documents\visual studio 2008\projects\fix2float\fix2float\main.cpp(21) : warning C4305: 'argument' : truncation from 'double' to 'float'
1>c:\users\a\documents\visual studio 2008\projects\fix2float\fix2float\main.cpp(27) : warning C4305: 'argument' : truncation from 'int' to 'float'
1>c:\users\a\documents\visual studio 2008\projects\fix2float\fix2float\main.cpp(36) : error C3861: 'fixtfloat': identifier not found
1>Build log was saved at "file://c:\Users\A\Documents\Visual Studio 2008\Projects\fix2float\fix2float\Debug\BuildLog.htm"
1>fix2float - 1 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Topic archived. No new replies allowed.