Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function Syntax... Need help soon, please... 1

Status
Not open for further replies.

CSpannos

IS-IT--Management
Mar 21, 2001
32
US
Hello,

I was wondering if it's possible to call the same function within itself? It's a function that searches for a string within a paragraph. I want the function to keep running over and over until the end of the paragraph is reached. I keep getting a Stack Overflow. When I looked that up in the Borland C++ Builder 5.0 help file, one reason is infinite recursion. Could this be it?

Code:
void DoSearch(char sString[],char paragrph[LINES][CHARS],int& lne,int& rsult,
              int& col,int& row)
{
 int i=0,length=0,success=0,temp1=0,temp2=0;

 //tests for EOF
 if (paragrph[row][0]!='\0')
 {
  //get length of search string
  for (length=0;sString[length]!='\0';length++);
  //skip characters till last character of search string is found on line
  //also checks for EOF
  while ((paragrph[row][col]!=sString[length])&&(paragrph[row][0]!='\0'))
  {
   col++;
   //makes sure next line is started, if needed
   if (paragrph[row][col-1]=='\0')
   {
    row++;
    col=0;
   }
   //accounts for one character being matched
   if (paragrph[row][col]==sString[length])
   {
    success=1;
   }
  }
  //checks in reverse for remaining matching characters
  temp1=col-1;
  temp2=length-1;
  for (i=1;((i<length)&&(temp1>=0)&&(temp2>=0));i++)
  {
   if (paragrph[row][temp1]==sString[temp2])
   {
    success++;
    temp1--;
    temp2--;
   }
   else
   {
    success=0;
    break;
   }
  }
  //adds to count of matching strings
  if (success==length)
  {
   rsult++;
  }
  //begins the search again if EOF is not reached
  if (paragrph[row][0]!='\0')
  {
   DoSearch(sString,paragrph,lne,rsult,col,row);    //<-- COMPILER POINTS HERE ON OVERFLOW
  }
 }
 return;
} //end of DoSearch

Thanks in advance,
Kosta

 
I think that the line:
if(paragrph[row][0]!='\0')
will always evaluate TRUE.

Check with the debugger on a small test file, maybe your current test file has some special end caracter which is not necesarily followed by a '\0' at the end. Test also for EOF in the test above.

HTH, s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
Dear Cspannos,

I was looking through your code for a while now, and I wonder whether you need a recursion or not.

To help you further I'd like you to answer a few questions:

1) What is your function exactly for:
a) Show up how many times the sstring is in paragrph?
b) Show up where the string is found first or show every place?

2)Are the params given or is it under your scope to change them?

3) what is the param int& lne for (as you don not use it in your function at all)?

4) the param char paragrph[LINES][CHARS]
Am I right that LINES and CHARS are constant defining the max limits of possible lines and lenghts of lines?

5) Is it allways given that you paragraph end with an 'empty' line? ('/0')

regards Astrid
 
Astrid,

First off, thanks for taking a look at it for me. The program's function is to search for a string within a paragraph, and return the number of occurences. I think if you look at the full code, that will answer the rest of your questions.

Code:
//header file includes
#include <iostream.h>

const int CHARS=100;
const int LINES=20;

//****************************************************************************//
//inputs the search string and paragraph
void InputData(char sString[],char oLine[],char paragrph[LINES][CHARS],int& lne)
{
 int col=0,empty=0,row=0;

 //re-initialize all character arrays
 sString[0]='\0';
 oLine[0]='\0';
 for (empty=0;empty<LINES;empty++)
 {
  paragrph[empty][0]='\0';
 }
 //input search string
 cout<<&quot;\nEnter the string you would like to search for (20 chars. max):   &quot;;
 cin.getline(sString,21,'\n');
 //input paragraph
 cout<<&quot;\nEnter a paragraph (&quot;<<LINES<<&quot; lines max, hit the 'Enter' key twice &quot;
     <<&quot;to finish):&quot;<<endl<<endl;
 oLine[0]='a';
 for (row=0;(row<LINES)&&(oLine[0]!='\0');row++)
 {
  //input one line of paragraph...
  cin.getline(oLine,CHARS+1,'\n');
  //...and transfer it to a double dimensional array
  for (col=0;oLine[col]!='\0';col++)
  {
   paragrph[row][col]=oLine[col];
  }
  paragrph[row][col]='\0';
 }
 lne=row;
 return;
} //end of InputData


//****************************************************************************//
//do search
void DoSearch(char sString[],char paragrph[LINES][CHARS],int& lne,int& rsult,
              int& col,int& row)
{
 int i=0,length=0,success=0,temp1=0,temp2=0;

 //get length of search string
 for (length=0;sString[length]!='\0';length++);
 //skip characters till last character of search string is found on line
 //also checks for EOF
 while ((paragrph[row][col]!=sString[length])&&(paragrph[row][0]!='\0'))
 {
  col++;
  //makes sure next line is started, if needed
  if (paragrph[row][col-1]=='\0')
  {
   row++;
   col=0;
  }
  //accounts for one character being matched
  if (paragrph[row][col]==sString[length])
  {
   success=1;
  }
 }
 //checks in reverse for remaining matching characters
 temp1=col-1;
 temp2=length-1;
 for (i=1;((i<length)&&(temp1>=0)&&(temp2>=0));i++)
 {
  if (paragrph[row][temp1]==sString[temp2])
  {
   success++;
   temp1--;
   temp2--;
  }
  else
  {
   success=0;
   break;
  }
 }
 //adds to count of matching strings
 if (success==length)
 {
  rsult++;
 }
 return;
} //end of DoSearch


//****************************************************************************//
//output the results
void OutputResults(char sString[],int& rsult)
{

 cout<<&quot;\n\n---------------------------------------------&quot;<<endl
     <<&quot;The paragraph contains &quot;<<rsult<<&quot; occurrences of the&quot;<<endl
     <<&quot;search string, \&quot;&quot;<<sString<<&quot;\&quot;.&quot;<<endl;
 return;
}



//****************************************************************************//
//****************************************************************************//
void main()
{
 char oneLine[CHARS],paragraph[LINES][CHARS],searchString[21];
 int line=0,result=0,theColumn=0,theRow=0;

 InputData(searchString,oneLine,paragraph,line);
 while (paragraph[theRow][0]!='\0')
 {
  DoSearch(searchString,paragraph,line,result,theColumn,theRow);
 }
 OutputResults(searchString,result);
} //end of main
//****************************************************************************//
//****************************************************************************//


I changed a few things to the DoSearch function. I made the recursive call in main() instead. Under Windows,I don't get the Stack Overflow anymore, but now the program just hangs. If I try to compile it under Linux, I get a core dump : Segmentation Fault. I don't get it. I have all the null terminators in the right spots, I think?

Any help would be greatly appreciated.

-Kosta
 
dea Kosta,

wirting a new one was easier :)

/* as your function shall only deliver one value I take the return value
this allows also constructions like
if!(countOccur(char sString[],char paragrph[LINES][CHARS], const int maxlines)) cout << &quot;not in string&quot;;
no further params are required
*/
int countOccur( char sString[],char paragrph[LINES][CHARS], const int maxlines)
{
int result=0,row=0,col=0,temprow=0,tempcol=0,i=0,length=0;
bool found=false;

// calculate length only ones
for (length=0;sString[length]!='\0';length++);
for (row=0,col=0; !(paragrph[row][col]=='\0' &&(paragrph[row+1][0]=='\0' ));col++)
// for loop is stopped if the current char is '\0' and the next line starts with '\0'

{
if (paragrph[row][col]=='\0') // skip end of line
{
row++;
col=0;
}
//cout << row <<&quot;,&quot; << col<< endl;

if (paragrph[row][col]==sString[0]) // compare current char with first of sString
{
found = true;
temprow=row;
tempcol=col+1; // take next char
for (i=1;i<length && temprow < maxlines;i++,tempcol++)
// for loop temprow < maxlines do not search more rows than available
{
if (paragrph[temprow][tempcol]=='\0')
{
temprow++; //skipline if neccesary
tempcol=0;
}
// if something differs we did not find the right one
if (paragrph[temprow][tempcol]!=sString)
{
found = false;
}
}
if (found)
{
result++;
found = false; // reset for next possibility
}
}


}
return result;
}



//****************************************************************************//
//****************************************************************************//
void main()
{
char oneLine[CHARS],paragraph[LINES][CHARS],searchString[21];
int line=0,result=0,theColumn=0,theRow=0;

InputData(searchString,oneLine,paragraph,line);
// while (paragraph[theRow][0]!='\0')
{
// changed call here
result=countOccur(searchString,paragraph,LINES);
}
OutputResults(searchString,result);
} //end of main
//****************************************************************************//
//****************************************************************************//
 
Dear kosta

there was an error produced by TGML

in the line before the text becomes italic
it should be

if (paragrph[temprow][tempcol]!=sString)
{
found = false;
}

regards Astrid
 
Hey,

Thank you so much for all your help. It works perfectly, and is whole lot more clear than mine was. I know this was a real simple little program, but for a beginner like me, it was driving me nuts.

Thanks again,
-Kosta
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top