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:
My C++ code so far (screenshots):
C++ (text for copy and paste):
#include
// 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": Other test which "passed," even though I don't think it should have: 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!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started