Hello, I am trying to write a function using cstdarg. My function will return true if a number of terms are found in order. E.G:searchTermsInOrder("hello everybody how are you all today", 3, "hello", "you", "today") would return true. Here is my code:
My issue is that every time I run my code I get a strange access violation error. Does anybody know whats causing this, and if so, what I should do to fix it? Thankyou
Okay. If I set them to const char* I get an assertion error regarding an invalid null pointer, and if I keep them as strings and pass three string objects as parameters I get another access violation.
The function you wrote shows that you are not using GCC compiler ( in gcc it wud not have managed to compile ) ... anyways here is the code corrected :
bool searchTermsInOrder(string s_string, int i_numberOfTerms, ...)
{
int i_termsSearched = 0;
int i_termScanPosition = 0;
va_list v_parameters;
va_start(v_parameters, i_numberOfTerms);
string s_currentSearchTerm = va_arg(v_parameters, char*);
for(int i_iterator = 0; i_iterator < s_string.length() ; i_iterator ++)
{
if(s_currentSearchTerm[i_termScanPosition] == s_string[i_iterator])
i_termScanPosition ++;
else
i_termScanPosition = 0;
if(i_termScanPosition == s_currentSearchTerm.length())
{
i_termScanPosition = 0;
i_termsSearched ++;
if (i_numberOfTerms == i_termsSearched) /* this will stop this for loop when your search is completed */
{
va_end(v_parameters);
returntrue;
}
s_currentSearchTerm = va_arg(v_parameters, char*);
}
}
va_end(v_parameters);
returnfalse;
}
access violation error are occurred when you try to get a argument (via va_arg ) which was never passed ( in this case it was because you were trying to get fourth variable argument )
Also check out that last if statement you are using in your function