0

The second while loop is throwing an exception in xstring line 1441 on the second iteration, which it should not do. This is because on the first pass:

Testthis->NAME[n] = 'B'
Testthis->NAME[n+1] = 'O'
Testthis->NAME = "BOD MQL"

are all verified from the debug cout statements. I am stumped on this one.

struct Node * ProbableMatch(struct Node * Look_in, int MaxNodes, char Findthis[64]){
system("cls");
int i=0,proba=100,CurrentHigh=100;
size_t pos1=1,n1=0,n2=0,pos2=1;
//char s[64];
struct Node * CurrentHighProb;
struct Node * Testthis;
CurrentHighProb=new(Node);
Testthis=new(Node);
Testthis=Look_in;
//for(i=0;i==MaxNodes;i++)
while(!Testthis || i!=ccounter)
{
    gotoxy(0,0);
    int n=0;
    n1=sizeof(Look_in->NAME);
    n2=sizeof(Findthis);
    string str1;
    char str2[64];
    cout<<Testthis->NAME[n]<<endl<<Testthis->NAME<<endl;
    cout<<Testthis->NAME[n+1]<<endl;

    while(Testthis->NAME[n]!='\0'){  //mannually coverts the strings since I am having hell with the inbuilt functions
        cout<<Testthis->NAME[n];
        str1[n]=Testthis->NAME[n];
        if(Testthis->NAME[n+1]=='\0'){str1[n+1]='\0';}//makes NULL terminated strings
        n++;
    }//end of while

    //strcpy_s(cstr, str1.length());
    //strcpy_s(str1,sizeof(Look_in->NAME),Look_in->NAME);
    //strcpy_s(str2,sizeof(Findthis),Findthis);
    //char * cstr1 = new char[str1.length()+1];
    cout<<str1.compare(pos1,n1,Findthis,0,n2)<<" is the value of the string compare "<<endl;
    proba=str1.compare(pos1,n1,Findthis,0,n2);//compares Findthis to the varibles read from the database from string matches

    //DEBUG STUFF
    cout<<endl<<sizeof(Look_in->NAME)<<" sizeof(Look_in->NAME)"<<Look_in->NAME<<endl;
    cout<<sizeof(Findthis)<<" sizeof(Findthis)"<<Findthis<<endl;
    cout<<sizeof(str1)<<" "<<str1<<" string1   string2 "<<str2<<" "<<sizeof(str2)<<endl;
    //cout<<sizeof(str1)<<" reinterpret_cast< char *>(str1)"<<endl;
    system("PAUSE");
    cout<<"\n Probability of "<<Look_in->NAME<<" matching "<<Findthis<<" is "<<proba<<" ."<<endl;
    //ENDOFDEBUG FOR FUNCTION
    Testthis->prob=proba;
    if(proba==0)
    {
        cout<<"proba equals zero, match found returning "<<Look_in<<endl;
        //delete[] cstr1;
        return Testthis;  // returns the pointer of the varible that best matched the search
    }
    if(proba<CurrentHigh){
        CurrentHigh=proba;
        CurrentHighProb=Testthis;
    }
    i++;
    Testthis=Testthis->next;
}
cout<<"Perfect match not found after "<<i<<" number of searches, returning this highest probable match "<<CurrentHighProb<<" "<<proba<<endl;

system("PAUSE");
//delete[] cstr1;
return CurrentHighProb;
}
Dave
  • 10,964
  • 3
  • 32
  • 54
John
  • 13
  • 5
  • `Testthis=new(Node); Testthis=Look_in;` Hello, memory leaks. Consider getting a [good introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) if you are interested in learning how to write correct C++ code. – James McNellis Jan 03 '12 at 19:18
  • Thanks but at this point I am just trying to get the function to find the correct entry, after that is done I will switch to just passing the head pointer that is deleted when the program terminates. – John Jan 03 '12 at 21:14

1 Answers1

1
string str1;

declares a zero-length std::string.

str1[n]=Testthis->NAME[n];

references the (non-existent) n'th element of str1.

Perhaps you mean to create a string with a definite length:

string str1(n1, ' ');

Or, perhaps you mean to allocate the string data during the copy:

str1.push_back(Testthis->NAME[n]);

There are other errors in your code, but this one likely produced the exception you describe.

You can avoid all of these errors simply. Replace the inner while loop with this:

str1 = Testthis->NAME;
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Thanks. The last time I did any programming was in 98' – John Jan 03 '12 at 19:25
  • This `str1 = Testthis->NAME;` gives me a conversion error, cannot convert char[64] to std::string[64], should those data types be compatable? – John Jan 03 '12 at 19:34
  • Hmm. In the code you posted, you declared `str1` as `std::string str1;`. Did you change that to `std::string str1[64]`? In the first case, the types are compatible. In the second, they are not. – Robᵩ Jan 03 '12 at 19:41