i changed the quastion to be more simple.

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

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

targetver.h

#pragma once

// 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

// 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

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

fixtfloat.cpp

#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;
}

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


when i trying to compile it looks like this:

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 ==========





Last edited on
closed account (zb0S216C)
What's the definition of str?

Wazzak
if a is string, i cant say that: str *a ?
what you mean whats the definition of str?
closed account (zb0S216C)
miki135 wrote:
if a is string, i cant say that: str *a ? (sic)

Any type-specifier can be a pointer. So, str *a is valid, assuming str exists.

str isn't a standard type-specifier. Since str isn't standard, it must be a user-defined type. So, what is its type?

Wazzak
Last edited on
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.
Last edited on
string type is declared in string library. To include it you need to write #include <string> !

To use string class RTFM in http://cplusplus.com/reference/string/

p.s.
1
2
3
std::string a; // if NOT: using namespace std;
string a; // if using that... 
a="This is a string"


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

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


the debug output:

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(6): error C2065: 'str' : undeclared identifier
1>c:\users\a\desktop\מיקי\לימודים\תוכניות\targil1\targil1\main.cpp(6): error C2065: 'a' : undeclared identifier
1>c:\users\a\desktop\מיקי\לימודים\תוכניות\targil1\targil1\main.cpp(6): error C2065: 'str' : undeclared identifier
1>c:\users\a\desktop\מיקי\לימודים\תוכניות\targil1\targil1\main.cpp(6): error C2065: 'b' : undeclared identifier
1>c:\users\a\desktop\מיקי\לימודים\תוכניות\targil1\targil1\main.cpp(6): error C2062: type 'float' unexpected
1>c:\users\a\desktop\מיקי\לימודים\תוכניות\targil1\targil1\main.cpp(7): error C2143: syntax error : missing ';' before '{'
1>c:\users\a\desktop\מיקי\לימודים\תוכניות\targil1\targil1\main.cpp(7): error C2447: '{' : missing function header (old-style formal list?)
1>c:\users\a\desktop\מיקי\לימודים\תוכניות\targil1\targil1\main.cpp(13): error C2065: 'str' : undeclared identifier
1>c:\users\a\desktop\מיקי\לימודים\תוכניות\targil1\targil1\main.cpp(13): error C2059: syntax error : ','
1>c:\users\a\desktop\מיקי\לימודים\תוכניות\targil1\targil1\main.cpp(19): error C3861: 'tester': identifier not found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Last edited on
closed account (zb0S216C)
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).

Wazzak
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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;
}


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


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

debug output:

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 ==========




closed account (zb0S216C)
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.

Wazzak
Last edited on
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?
closed account (zb0S216C)
eraggo wrote:
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.

Wazzak
[code] "Please use code tags" [/code]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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
using namespace 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 


For converting from string to a number http://www.cplusplus.com/forum/articles/9645/
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;
}

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

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

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

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

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
closed account (zb0S216C)
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.

Wazzak
Topic archived. No new replies allowed.