Question: Hi, I need help with an error I keep receiving on my C++ project! The project is about string manipulation using user input and printing

Hi, I need help with an error I keep receiving on my C++ project! The project is about string manipulation using user input and printing menu. I keep getting an error when the user wants to find a phrase or word within the user-inputted text/string. I'm having issues with my FindText function. Any help would be much appreciated!

Instructions to the project:

Hi, I need help with an error I keep receiving on my

C++ project! The project is about string manipulation using user input and

printing menu. I keep getting an error when the user wants to

My C++ code so far (screenshots):

find a phrase or word within the user-inputted text/string. I'm having issues

with my FindText function. Any help would be much appreciated! Instructions to

the project: My C++ code so far (screenshots): C++ (text for copy

and paste): #include #include using namespace std; // Functions char printMenu(string); int

GetNumOfNonWSCharacters(string); int GetNumOfWords(string); void ReplaceExclamation(string&); void ShortenSpace(string&); int FindText(string, string); // Main

C++ (text for copy and paste):

#include #include using namespace std;

// Functions char printMenu(string); int GetNumOfNonWSCharacters(string); int GetNumOfWords(string); void ReplaceExclamation(string&); void ShortenSpace(string&); int FindText(string, string);

// Main function int main()

{

// Variable declaration char option;

string text, phraseToFind;

// Reading text from user cout

getline(cin, text);

// Printing text by user cout

// A do-while loop of user choosing different menu options until user wants to quit the menu do { // Printing menu option = printMenu(text); cout

system("pause"); return 0; }

// Function that prints menu char printMenu(string text)

{

char ch; string phraseToFind;

// Printing menu options cout

cout

cout

// Reading user choice cin >> ch;

// Calling functions based on option selected by user switch (ch)

{

// If user chooses to quit the menu case 'q':

case 'Q':

exit(0);

// Counting all non-whitespace characters case 'c':

case 'C':

cout

break;

// Counting number of words in text case 'w':

case 'W':

cout

break;

// Counting number of occurrences of words in given string case 'f':

case 'F':

cin.ignore();

cout

getline(cin, phraseToFind);

cout

break;

// Replacing ! with . case 'r':

case 'R': ReplaceExclamation(text); cout

break;

// Replacing any multiple spaces with single space case 's':

case 'S': ShortenSpace(text); cout

break;

default: // Error message if user enters a key not associated with the menu options cout

break; } return ch; }

// Function that counts number of non-space characters int GetNumOfNonWSCharacters(const string text)

{

int cnt = 0, i;

int len = text.size();

// Looping over user-inputted text for (i = 0; i

{

// Counting all spaces if (!isspace(text[i]))

cnt++;

}

return cnt;

}

// Function that counts number of words in the string int GetNumOfWords(const string text)

{

int words = 0, i;

int len = text.size();

// Looping over text/phrase for (i = 0; i

{

// Checking for all spaces if (isspace(text[i]))

{

// Handling multiple spaces while (isspace(text[i]))

i++;

// Incrementing words words++; } else { i++; } }

// Handling last word in phrase words = words + 1;

return words; }

// Function that replaces ! with . void ReplaceExclamation(string& text)

{ string newText = text;

int i, len = text.size();

// Looping over string for (i = 0; i

{ // Replacing ! with . if (text[i] == '!')

newText[i] = '.'; } text = newText; }

// Function that replaces multiple spaces with single space void ShortenSpace(string& text) { char *newText;

int i, len = text.size(), k = 0;

newText = new char[len + 1];

// Looping over string for (i = 0; i

// Assign individual characters newText[k] = text[i];

// Handling multiple spaces if (isspace(text[i]))

{

// Replacing multiple spaces with single space while (isspace(text[i])) i++; } else { i++; } }

newText[k] = '\0';

text = newText; }

// Function that counts the occurrences of given phrase in a given text int FindText(string text, string phrase) { int count = 0;

if (phrase.size() == 0)

return 0;

// Counting number of word occurrences in the given string for (size_t offset = text.find(phrase); offset != string::npos; offset = text.find(phrase, offset + phrase.size())) { ++count; } return count; }

Error I'm receiving when trying to "Find word or phrase":

function int main() { // Variable declaration char option; string text, phraseToFind;

Other test which "passed," even though I don't think it should have:

// Reading text from user cout getline(cin, text); // Printing text by

In general, I'm trying to figure out what went wrong with my code. I think the error comes from the way I wrote the function for FindText:

Either here (which is located towards the end of my code):

// Function that counts the occurrences of given phrase in a given text int FindText(string text, string phrase) { int count = 0;

if (phrase.size() == 0)

return 0;

// Counting number of word occurrences in the given string for (size_t offset = text.find(phrase); offset != string::npos; offset = text.find(phrase, offset + phrase.size())) { ++count; } return count; }

Or here in the middle of my code:

// Counting number of occurrences of words in given string case 'f':

case 'F':

cin.ignore();

cout

getline(cin, phraseToFind);

cout

break;

Thanks again!

Feb 14: Regarding the printMenu() function This function will consist of the following steps print the menu receive an end user's choice of action (until it's valid) call the corresponding function based on the above choice (1) Prompt the user to enter a string of their choosing (Hint: you will need to call the getline0 function to read a string consisting of white spaces.) 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'11 continue our quest in space. There wil be more shuttle flights and more shuttle crews and, yes, here our hopes and our journeys continue! more volunteers, more civilians, more teachers in space. Nothing ends (2) Implement a PrintMenu function, which has a string as a parameter, outputs a menu of user options for analyzing/editing the string and returns the users entered menu option. Each option is represented by a single character. If an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call PrintMenu0 in the main function. Continue to call PrintMenu0 until the user enters q to Quit. (3 pts) Ex: MENU cNumber of non-whitespace characters W- Number of words f Find text r - Replace all 's s -Shorten spaces -Quit Choose an option

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!