Function: int findText(const string toFind, string usrStr) always returns 0

Hello. I have a code. My function int findText(const string toFind, string usrStr)always returns 0.
Can somebody help me find what I did wrong?
Thanks in advance.

Here is code:


#include <iostream>
#include <string>
using namespace std;

char PrintMenu(string toFind);
int getNumOfNonWSCharacters(const string usrStr);
int getNumOfWords(const string usrStr);
int findText(const string toFind, string usrStr);
void replaceExclamation(string& usrStr);
void shortenSpace(string& usrStr);


int main()

{
char choice;
string usrStr;
string toFind;
cout << "Enter a sample text :" << endl;
getline(cin, usrStr);
cout << endl;
cout << "You Entered: " << usrStr << endl;


while (choice != 'q'){

PrintMenu(usrStr);

cout << "Choose an option:" << endl;
cin >> choice;

if(choice == 'c' || choice == 'C')
{
getNumOfNonWSCharacters(usrStr);
cout << "Number of non-whitespace characters: " << getNumOfNonWSCharacters(usrStr) << endl;
}


else if (choice == 'w' || choice == 'W')
{
getNumOfWords(usrStr);
cout << "Number of words: " << getNumOfWords(usrStr) << endl;
}


else if (choice == 'f' || choice == 'F')
{
string usrStr;
string toFind;
cin.ignore();
cout << "Enter a word or phrase to be found: " << endl;
getline(cin, usrStr);
///cin >> usrStr;
findText(toFind, usrStr);
cout << "\"" << usrStr << "\" instances: " << findText(toFind, usrStr) << endl;
}


else if (choice == 'r' || choice == 'R')
{
replaceExclamation(usrStr);
}


else if (choice == 's' || choice == 'S')
{
shortenSpace(usrStr);
}

}

return 0;
}




/*********************************/
char PrintMenu(string toFind)
{
///char choice;

cout << endl;
cout << "MENU" << endl;
cout << "c - Number of non-whitespace characters" << endl;
cout << "w - Number of words" << endl;
cout << "f - Find Text" << endl;
cout << "r - Replace all !'s" << endl;
cout << "s - Shorten Spaces" << endl;
cout << "q - Quit" << endl;
cout << endl;
///return;
}


/*********************************************/
int getNumOfNonWSCharacters(const string usrStr)
{
int NumOfNonWS = 0;
int i;
for (i = 0; i < usrStr.length(); i++)
{
if(!isspace(usrStr.at(i)))
{
NumOfNonWS++;
}
}
return NumOfNonWS;
}



/*****************************************/
int getNumOfWords(const string usrStr)
{
int wordsCount = 0;

for (int i = 1; i < usrStr.size() - 1; i++)
{
if (usrStr.at(i) == ' '&& usrStr.at(i-1) != ' ')
{
wordsCount++;
}
}
return wordsCount +1;
}


/******************************************/
int findText(const string toFind, string usrStr)
{
int words = 0;
for (int i = 0; i < toFind.size(); i++)
{
int j = 0;
if (toFind[i] == usrStr[j])

{
while (toFind[i] == usrStr[j] && j < usrStr.size())
{
j++;
i++;
}
if (j == usrStr.size())
{
words++;
}
}
}
return words;
}


/*************************************/
void replaceExclamation(string& usrStr)
{
for (int i = 0; i < usrStr.length(); i++)
{
if (usrStr.at(i) == '!')
{
usrStr.at(i) = '.';
}
}
cout << "Edited text: " << usrStr << endl;
}


/****************************************************/
void shortenSpace(string& usrStr)
{
for (int i = 0; i < usrStr.size() - 1; i++)
{
if (isspace(usrStr.at(i)) && isspace(usrStr.at(i + 1)))
{
usrStr.erase(i, 1);
i--;
}
}
cout << "Edited text: " << usrStr << endl;

}


Last edited on
What are you trying to find in that function?

Do you realize that a std::string has quite a few functions to aid in "finding" things inside the string?

Here is my assignment for this function:

(5) Implement the function findText(), which has two strings as parameters. The first parameter is the text to be found in the user provided sample text, and the second parameter is the user provided sample text. The function returns the number of instances a word or phrase is found in the string. In main, if the user selects option f, prompt the user for a word or phrase to be found and then call findText(). Make sure the order of arguments in your call matches the order of the parameters. Also be sure there are no \n characters left in the read buffer before you try to read the user's search string. If there are, get rid of them with cin.ignore(). Use the following prototype: (3 pts)

int findText(const string toFind, string usrStr)

Ex:

Enter a word or phrase to be found:
more
"more" instances: 5
Here is my full assignment

Assignment: Text Editor
(1) Prompt the user to enter a string of their choosing, ending with a newline. Store the text in a string. Output the string. (1 pt)
Ex:

Enter a sample text:
We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!

You entered: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!

(2) Print a menu that outputs a menu of user options for analyzing/editing the string. Each option is represented by a single character. Implement a loop that prints the menu, reads the user's choice, and loops as long as the choice is not 'q'. (3 pts)

MENU
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit

Choose an option:

(3) Implement the function getNumOfNonWSCharacters(). getNumOfNonWSCharacters() has a constant string as a parameter and returns the number of characters in the string, excluding all whitespace. The funciton isspace(c) will be useful here. Use the following prototype: (4 pts)

int getNumOfNonWSCharacters(const string usrStr)

Ex:

Number of non-whitespace characters: 181

(4) Implement the function getNumOfWords(). getNumOfWords() has a constant string as a parameter and returns the number of words in the string. Hint: Words end when a space is reached, so if you see a space, and the character before it is not a space, then add 1 to your word count. The last word in the paragraph will likely not have a space after it, so just add an extra word to your total if the last character in the paragraph is not a space. Use the following prototype: (3 pts)

int getNumOfWords(const string usrStr)

Ex:

Number of words: 35

(5) Implement the function findText(), which has two strings as parameters. The first parameter is the text to be found in the user provided sample text, and the second parameter is the user provided sample text. The function returns the number of instances a word or phrase is found in the string. In main, if the user selects option f, prompt the user for a word or phrase to be found and then call findText(). Make sure the order of arguments in your call matches the order of the parameters. Also be sure there are no \n characters left in the read buffer before you try to read the user's search string. If there are, get rid of them with cin.ignore(). Use the following prototype: (3 pts)

int findText(const string toFind, string usrStr)

Ex:

Enter a word or phrase to be found:
more
"more" instances: 5

(6) Implement the function replaceExclamation(). replaceExclamation() has a string parameter and updates the string by replacing each '!' character in the string with a '.' character. replaceExclamation() DOES NOT output the string. Call replaceExclamation() in main, and then output the edited string. Use the following prototype: (3 pts)

void replaceExclamation(string& usrStr)

Ex.

Edited text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue.

(7) Implement the function shortenSpace(). shortenSpace() has a string parameter and updates the string by replacing all sequences of 2 or more spaces with a single space. shortenSpace() DOES NOT output the string. Call ShortenSpace() in the PrintMenu() function, and then output the edited string. Use the following prototype: (3 pts)

void shortenSpace(string& usrStr)

Ex:

Edited text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!
Note: The shortenSpace() function is difficult, so I am going to give you a code snippet that uses a function not mentioned in the book:

for (i = 0; i < usrStr.size() - 1; i++)
{
if(isspace(usrStr.at(i)) && isspace(usrStr.at(i+1)))
{
usrStr.erase(i,1);
i--;
}
}
Topic archived. No new replies allowed.