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;
}
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" .
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.
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;
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;
}