Can anyone fix my code? 5.14 LAB*: Program: Authoring Assistant (1) Prompt the user to enter a string of their choosing.

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Can anyone fix my code? 5.14 LAB*: Program: Authoring Assistant (1) Prompt the user to enter a string of their choosing.

Post by answerhappygod »

Can anyone fix my code?
5.14 LAB*: Program: Authoring Assistant
(1) Prompt the user to enter a string of their choosing. Store
the text in a string. Output the string.

Ex:
(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 user's 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 PrintMenu() in the main() function.
Continue to call PrintMenu() until the user enters q to Quit.

Ex:
(3) Implement the GetNumOfNonWSCharacters() function.
GetNumOfNonWSCharacters() has a constant string as a parameter and
returns the number of characters in the string, excluding all
whitespace. Call GetNumOfNonWSCharacters() in the PrintMenu()
function.

Ex:
(4) Implement the GetNumOfWords() function. 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 except
for the last word in a sentence. Call GetNumOfWords() in
the PrintMenu() function.

Ex:
(5) Implement the FindText() function, 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 the PrintMenu() function, prompt
the user for a word or phrase to be found and then call FindText()
in the PrintMenu() function. Before the prompt,
call cin.ignore() to allow the user to
input a new string.

Ex:
(6) Implement the ReplaceExclamation() function.
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 the PrintMenu() function, and then output
the edited string.

Ex.
(7) Implement the ShortenSpace() function. 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.

Ex:
my code is:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
//Function prototypes
char PrintMenu(string);
int GetNumOfNonWSCharacters(string);
int GetNumOfWords(string);
void ReplaceExclamation(string &);
void ShortenSpace(string &);
int FindText(string, string);
//Main function
int main()
{
//variable decalaration
char option;
string text, phraseToFind;
//Reading text from user
cout << "Enter a sample text:"<<endl;
getline(cin, text);
//Printing text
cout << "\nYou entered: " << text<<endl;
//Loop till user wants to quit
do
{
//Printing menu
option = PrintMenu(text);
cout << "\n";
} while (option != 'Q' && option != 'q');
//system("pause");
return 0;
}
//Function that prints menu
char PrintMenu(string text)
{
char ch;
string phraseToFind;
//Printing menu
cout << "\nMENU\n";
cout << "c - Number of non-whitespace characters\nw -
Number of words\nf - Find text\nr - Replace all !'s\ns - Shorten
spaces\nq - Quit";
cout <<endl<< "\nChoose an option:"<<endl;
//Reading user choice
cin >> ch;
//Calling functions based on option selected by //user
switch (ch)
{
//User wants to quit
case 'q':
case 'Q':
exit(0);
//Counting non-whitespace characters
case 'c':
case 'C':
cout << "\n\n Number of non-whitespace characters: "
<< GetNumOfNonWSCharacters(text);
break;
//Counting number of words
case 'w':
case 'W':
cout << "\n\n Number of words: " <<
GetNumOfWords(text) ;
break;
//Counting number of occurrences phrase in //given string
case 'f':
case 'F':
cin.ignore();
cout << "\n\n Enter a word/Phrase to find: ";
getline(cin, phraseToFind);
cout << "\n\n \"" << phraseToFind << "\"
instances: " << FindText(text, phraseToFind) << "
\n";
break;
//Replacing ! with .
case 'r':
case 'R':
ReplaceExclamation(text);
cout << "\n\n Edited text: " << text;
break;
//Replacing multiple spaces with single //space
case 's':
case 'S':
ShortenSpace(text);
cout << "\n\n Edited text: " << text;
break;
default:
cout << "\n\n Invalid Choice.... Try Again \n";
break;
}
return ch;
}
//Function that count number of non space characters
int GetNumOfNonWSCharacters(const string text)
{
int cnt = 0, i;
int len = text.size();
//Looping over given text
for (i = 0; i<len; i++)
{
//Counting spaces
if (!isspace(text))
cnt++;
}
return cnt;
}
//Function that count number of words in the string
int GetNumOfWords(const string text)
{
int words = 0, i;
int len = text.size();
//Looping over text
for (i = 0; i < len; i++)
{
//Checking for space
if (isspace(text))
{
//Handling multiple spaces
while (isspace(text))
i++;
//Incrementing words
i--;
words++;
}
}
//Handling last word
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 < len; i++)
{
//Replacing ! with .
if (text == '!')
newText = '.';
}
text = newText;
}
//Function that replaces Multiple spaces with single space
void ShortenSpace(string &text)
{
int i, len = text.size(), k = 0;
string newText = "";
//Looping over string
for (i = 0; i < len; i++)
{
//Assign individual characters
//Handling multiple spaces
if (isspace(text))
{
//Replacing multiple spaces with single //space
while (isspace(text))
i++;
i--;
newText += " ";
}
else
{
newText += text;
}
}
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 phrase occurrences in the given string
for (size_t offset = text.find(phrase); offset != string::npos;
offset = text.find(phrase, offset + phrase.size()))
{
++count;
}
//Retuning count
return count;
}
My error is

Can Anyone Fix My Code 5 14 Lab Program Authoring Assistant 1 Prompt The User To Enter A String Of Their Choosing 1
Can Anyone Fix My Code 5 14 Lab Program Authoring Assistant 1 Prompt The User To Enter A String Of Their Choosing 1 (72.25 KiB) Viewed 55 times
10: Compare output ^ 0/1 Output differs. See highlights below. Special character legend I want some water. I had some water earlier, but now he has some water. Input some water a Enter a sample text: You entered: want some water. I had some water earlier, but now he has w MENU C - Number of non-whitespace characters- - Number of wordse f Find texte r - Replace all ! 'se - Shorten spacese q - Quite S Choose an option: Your output ends with Enter a word/Phrase to find: 'some water" instances: 3+ MENU C - Number of non-whitespace characters

MENU с Number of non-whitespace characters W - Number of words f - Find text r - Replace all !'s - Shorten spaces q - Quit S Choose an option: Enter a word or phrase to be found: "some water" instances: 3 с Expected output ends with MENU Number of non-whitespace characters W - Number of words f - Find text r - Replace all !'s - Shorten spaces q - Quit S Choose an option:
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply