we got everything prepred and we just need to add main.
whats worng with the main i wrote?
main.cpp (i wrote)
#include <iostream>
using namespace std;
int main ()
{
int b;
r = tester ("empty chain","",0.0);
r = tester ("simple chain,"123",123.0);
r = tester ("chain starts with +","+4567",4567.0);
cout << "The result is " << r;
return 0;
}
int tester (str * a, str * b, float c)
{
w = fix2float ( * b);
if (w==c) return 1
else return 0;
}
-----------------------------------------------------------------------------
next are all the things that we got preperd,
and in the end the output of the debug:
-----------------------------------------------------------------------------
stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
// The following macros define the minimum required platform. The minimum required platform
// is the earliest version of Windows, Internet Explorer etc. that has the necessary features to run
// your application. The macros work by enabling all features available on platform versions up to and
// including the version specified.
// Modify the following defines if you have to target a platform prior to the ones specified below.
// Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef _WIN32_WINNT // Specifies that the minimum required platform is Windows Vista.
#define _WIN32_WINNT 0x0600 // Change this to the appropriate value to target other versions of Windows.
#endif
// stdafx.cpp : source file that includes just the standard includes
// fixtfloat.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
#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;
}
1>------ Build started: Project: fixtfloat, Configuration: Debug Win32 ------
1>Compiling...
1>fixtfloat.cpp
1>c:\users\a\desktop\מיקי\לימודים\קורסים נוכחיים\בדיקות תוכנה\מכונן s שבלימודים\fixtfloat\fixtfloat\fixtfloat.cpp(2) : warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\users\a\desktop\מיקי\לימודים\קורסים נוכחיים\בדיקות תוכנה\מכונן s שבלימודים\fixtfloat\fixtfloat\fixtfloat.cpp(4) : warning C4627: '#include <ostream>': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\users\a\desktop\מיקי\לימודים\קורסים נוכחיים\בדיקות תוכנה\מכונן s שבלימודים\fixtfloat\fixtfloat\fixtfloat.cpp(6) : warning C4627: '#include <math.h>': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\users\a\desktop\מיקי\לימודים\קורסים נוכחיים\בדיקות תוכנה\מכונן s שבלימודים\fixtfloat\fixtfloat\fixtfloat.cpp(38) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
1>main.cpp
1>c:\users\a\desktop\מיקי\לימודים\קורסים נוכחיים\בדיקות תוכנה\מכונן s שבלימודים\fixtfloat\fixtfloat\main.cpp(1) : warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\users\a\desktop\מיקי\לימודים\קורסים נוכחיים\בדיקות תוכנה\מכונן s שבלימודים\fixtfloat\fixtfloat\main.cpp(21) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
1>Generating Code...
1>Build log was saved at "file://c:\Users\A\Desktop\מיקי\לימודים\קורסים נוכחיים\בדיקות תוכנה\מכונן S שבלימודים\fixtfloat\fixtfloat\Debug\BuildLog.htm"
1>fixtfloat - 2 error(s), 4 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
In your project settings, you should go to Configuration Properties->C++->Precompiled Headers and turn off precompiled headers. Also, when you start a project like this one, you should just choose "empty project". That way, Visual Studio doesn't add stuff like targetver.h, stdafx.h, stdafx.cpp, which just muck up your code.
The function "tester" also needs a prototype, so that main can "see" it.
i did what shacktar framework and eraggo said and now i just have the main.cpp and fixtfloat.cpp.
also i had prototype for tester function.
it steel not work (the debug output is in the end).
thanks for all the help!
#include <iostream>
using namespace std;
#include <string>
#include "fixtfloat.cpp"
int main ()
{
int z;
string* a,b;
z = tester ("empty chain","",0.0,-6);
cout << "The result is " << z;
return 0;
}
int tester (str*,str*,float,int);
int tester (str* a, str* b, float c, int d)
{
w = fix2float ( *b);
if (w==c) return 1
else return 0;
}
fixtfloat.cpp (we got it done, no need to read it. i wiil just say that it changes 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;
}
Never include source modules. Instead, include the header module. Also, the prototype of a function must be before the main entry-point. In tester( ), w is an undeclared identifier. Another error points to the missing semi-colon after the return statement in tester( ). Both pointers, a and b are unused (you should receive a warning regarding this). Yet again, str is still an unidentified type-specifier (unless it's defined elsewhere).
#include <iostream>
usingnamespace std;
#include <string>
#include "fixtfloat.cpp"
int main ()
{
int z;
string* a,b;
z = tester ("empty chain","",0.0,-6);
cout << "The result is " << z;
return 0;
}
int tester (str*,str*,float,int);
int tester (str* a, str* b, float c, int d)
{
w = fix2float ( *b);
if (w==c) return 1
elsereturn 0;
}
Your function tester() is not valid! make it so it get as parameters strings, not "str"'s. Also move line 15 to something BEFORE main()-function.
on line 19: Where did you declare variable called "w"?
I (we) do not know how your function called "fix2float" works so it's dead end.
much less errors, but still a few.
someone knows how to solve it?
#include <iostream>
using namespace std;
#include <string>
#include "fixtfloat.cpp"
int 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;
}
int tester (string* a, string* b, float c, int d)
{
float w;
w = fix2float ( *b);
if (w==c) return (1);
else return (0);
}
---------------------------------------------------------------------------------------------------------------------
fixtfloat.cpp (we got it done, no need to read it. i wiil just say that it changes 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;
}
1>------ Build started: Project: targil1, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\a\desktop\מיקי\לימודים\תוכניות\targil1\targil1\fixtfloat.cpp(15): warning C4305: 'initializing' : truncation from 'double' to 'float'
1>c:\users\a\desktop\מיקי\לימודים\תוכניות\targil1\targil1\fixtfloat.cpp(34): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
1>c:\users\a\desktop\מיקי\לימודים\תוכניות\targil1\targil1\main.cpp(12): error C2664: 'tester' : cannot convert parameter 1 from 'const char [12]' to 'std::string *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\a\desktop\מיקי\לימודים\תוכניות\targil1\targil1\main.cpp(20): error C2664: 'fix2float' : cannot convert parameter 1 from 'std::string' to 'char *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Don't include source modules! I said this once before. Also, In tester( ), w is a float but you're assigning it a double. float values are suffixed with an f. The truncation is referring to the loss of accuracy and range. Both pointers a and b are uninitialized and unused.
remove the *'s from the function parameters! You are not giving any pointers as arguments... How you can change from string to char in your function call to fix2float? What your module/program is SUPPOSED to do?
You are not giving any pointers as arguments (sic)
Have you considered that he/she wants to pass the address of an object, not a copy? If the latter is the case then he/she should be using the more preferred reference operator.
#include <iostream>
//using namespace std; //not here, it pollutes the next header
#include <string>
//#include "fixtfloat.cpp" //don't include sources
#include "fixtfloat.h" //include headers
usingnamespace std;
//int tester (string*,string*,float,int); //why pointers?
bool tester(string, string, float, int);
int main ()
{
int z;
//string* a,b; //a is a pointer, b an object. The variables are never used
z = tester ("empty chain","",0.0,-6);
cout << "The result is " << z;
return 0;
}
//int tester (string* a, string* b, float c, int d)
bool tester(string, string b, float c, int)//what is the function supposed to do?
//the first and last arguments are ignored
{
float w;
w = fix2float (b);
//if (w==c) return (1);
//else return (0);
return w==c; //however you should not compare floats like that. Use a tolerance
}
1 2 3 4 5 6 7 8 9 10 11
float fix2float(char * n) //in main you are passing a string
//...
/*int ll =0;
while (n[ll] != 0x00)ll++;
ll -=1;*/
int ll = strlen(n)-1;
//...
//if(46 == int(n[jk]))break;//dot constant
if( '.' == n[jk] ) break;//clearer, don't you think?
//f=f*10+(int(n[jk])-48);
f=f*10+(n[jk]-'0'); //same here
thank you all.
just last thing.
now i have main.cpp (that i wrote... with your big help) and fix2float.cpp (that in class we got it already prepared). but the last problem now is that in line 3 in the main.cpp, i use fix2float.h and i dont have such a thing. i need to write it myself?
(ne555, after i will debug it successfuly i will add your commants about the fix2float.cpp. thankyou)
main.cpp
#include <iostream>
#include <string> #include fix2float.h // the problem is here
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;
}
fix2float.cpp (you dont need to read it, we got it already prepared in class)
#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;
}
You need to enclose the filename with quotation marks. For example: #include "fix2float.h". Quotation marks indicates that the file is locally grown. If the filename is enclosed within angle brackets, it indicates that the file is located outside the project folder structure.