Infinite loop...
Dec 23, 2012 at 2:24am UTC
Hi guys!
Currently I'm making a "Find" function using an infinite loop. But when I run code, it always fails.
I have no idea because I looked and checked function code errors many times....
Here, the code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#define EXITPOINT -1
//////////////////////////////////////////////////////////////////////////////////////////////
inline int Find(const char *string, int nStart = 0, unsigned int stringlen = 0, char *strTemp = 0, bool bAcceptAll = false ) const {
unsigned size = Text.size(); //Get current string size
if (stringlen == false )stringlen = strlen(string);
if (stringlen == false || size < stringlen)return EXITPOINT;bool bTemp;
if (!strTemp){strTemp = new char [stringlen+1];bTemp = true ;}else bTemp = false ;
size-=stringlen-1; //Avoid crash error
//////////////////////////////////////////////////////////////////////////////////
for (unsigned int i = nStart;i < size;i++){
for (unsigned int j =0;j < stringlen;j++)
strTemp[j] = (char )Text[i+j].chr;strTemp[j]=0; //Buffer copy
//////////////////////////////////////////////////////////////////////////////////
if (bAcceptAll == true ){
if (stricmp(strTemp,string) == 0){if (bTemp)delete strTemp;return i;}}else
if (strcmp(strTemp,string == 0)){if (bTemp)delete strTemp;return i;}
}
if (bTemp)delete strTemp; //Free memory if needed
return EXITPOINT;
}
And the code : (I don't know the error which causes the infinite program loop)
1 2 3 4 5 6 7
#define EXITPOINT -1
CopyText(Text,"aeeossvHellosv!!!" );
char *string = "os" ; //char *
int nIndex = 0;
/////////////////////////////////////////////////////////////////////////////////////////////
while ((nIndex = Find(string,nIndex,0,0),false ) != EXITPOINT)
{Delete(nIndex);Delete(nIndex);} //Delete the string "os"
(Any help would be greatly appreciated).
Last edited on Dec 23, 2012 at 4:24am UTC
Dec 23, 2012 at 2:50am UTC
What's wrong with std::string::find() or strstr()?
Dec 23, 2012 at 3:05am UTC
helios wrote:What's wrong with std::string::find() or strstr()?
No, std::string::find() and strstr() have no errors. :)
But, here are
Text and
WText structures :
1 2 3 4 5 6 7 8 9 10
struct TextData
{
unsigned short chr;
float size;
float width;
float height;
POINT pt;
};
vector <TextData> Text;
vector <TextData> WText;
So, if I used
strstr() , I would analyze the string first, then next step I have to determine the string
Index . If I changed a character element, I would re-calculate the character size, character width and character height.
It would be very helpful If someone can detect the error which causes the infinite program loop. :)
Last edited on Dec 23, 2012 at 4:56am UTC
Dec 23, 2012 at 4:28am UTC
1 2 3 4 5 6 7 8 9 10 11 12
#include <algorithm>
inline std::ptrdiff_t Find( const unsigned short * string, const std::vector<TextData>& WText,
std::size_t nStart = 0, std::size_t stringlen = 0 )
{
if (!stringlen) { auto p = string ; while (*p++) ++stringlen ; }
auto pos = std::search( WText.begin()+nStart, WText.end(), string, string+stringlen,
[] ( const TextData& t, unsigned short s ) { return t.chr==s ; } ) ;
return pos == WText.end() ? -1 : pos - WText.begin() ;
}
Dec 23, 2012 at 5:09am UTC
Thanks you, JLBorges. I compiled your code.
This is the output I've just gotten :
error C2958: the left parenthesis '(' found at 'FontClass.cpp(270)' was not matched correctly
1 2
auto pos = std::search( WText.begin()+nStart, WText.end(), string, string+stringlen,
[] ( const TextData& t, unsigned short s ) { return t.chr==s ; } ) ;
The problem is : I could not find the redundant parenthesis which causes the compiling error
the left parenthesis was not matched correctly .
Dec 23, 2012 at 6:51am UTC
An antiquated compiler without support for type inference and lambda expressions?
C++98:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
struct textdata_equals
{
bool operator () ( const TextData& t, unsigned short s ) const
{ return t.chr==s ; }
} ;
inline std::ptrdiff_t Find( const unsigned short * string, const std::vector<TextData>& WText,
std::size_t nStart = 0, std::size_t stringlen = 0 )
{
if (!stringlen) { /*auto*/ const unsigned short * p = string ; while (*p++) ++stringlen ; }
//auto pos = std::search( WText.begin()+nStart, WText.end(), string, string+stringlen,
// [] ( const TextData& t, unsigned short s ) { return t.chr==s ; } ) ;
std::vector<TextData>::const_iterator pos = std::search( WText.begin()+nStart, WText.end(),
string, string+stringlen, textdata_equals() ) ;
return pos == WText.end() ? -1 : pos - WText.begin() ;
}
Last edited on Dec 23, 2012 at 8:21am UTC
Dec 23, 2012 at 8:14am UTC
Wow! So awesome!!! Thanks you so much so much JLBorges !!!
Last edited on Dec 23, 2012 at 8:14am UTC
Topic archived. No new replies allowed.