Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is C++ i am not sure on why my code is not creating the correct answer here

ID: 3862866 • Letter: T

Question

This is C++ i am not sure on why my code is not creating the correct answer here it seems to be duplicating choose an option and i am not sure why.

Code

#include <iostream>
#include <string>

using namespace std;

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

int main()
{
char option;
string text, phraseToFind;

cout << "Enter a sample text:"<<endl;
getline(cin, text);

cout << " You entered: " << text;

while(1){
option = printMenu();

switch(option) {
case 'q':
case 'Q': return 0;
  
case 'c':
case 'C': cout << "Number of non-whitespace characters:" << GetNumOfNonWSCharacters(text)<<endl;
break;
  
case 'w':
case 'W': cout << "Number of words:" << GetNumOfWords(text) <<endl;
break;
  
case 'f':
case 'F': cin.ignore(); cout << "Enter a word or phrase to be found: "; getline(cin, phraseToFind);
cout << " "" << phraseToFind << ""instances:" << FindText(text, phraseToFind) << endl;
break;

case 'r':
case 'R': ReplaceExclamation(text); cout << "Edited text:" << text <<endl;
break;
  
case 's':
case 'S': ShortenSpace(text); cout << "Edited text:" << text <<endl;
break;
  

}
}
cout <<endl<<endl;

return 0;
}

char printMenu()
{
char ch;

cout <<" MENU";
cout << " c - Number of non-whitespace characters w - Number of words f - Find text r - Replace all !'s s - Shorten spaces q - Quit"<<endl;
cout <<" Choose an option: ";

cin >> ch;

return ch;
}

int GetNumOfNonWSCharacters(const string text){
int cnt = 0, i;
int len = text.size();

for(i=0; i<len; i++) {
if(!isspace(text[i]))
cnt++;
}

return cnt;
}

int GetNumOfWords(const string text){
int words = 0, i;
int len = text.size();

for(i=0; i<len;){
if(isspace(text[i])){

while(isspace(text[i]))
i++;

words++;
}
else{
  
i++;
}
}

words = words + 1;
return words;
}

void ReplaceExclamation(string& text){
string newText = text;
int i, len = text.size();

for(i=0; i<len; i++){
if(text[i] == '!')
newText[i] = '.';
}
  
text = newText;
}

void ShortenSpace(string& text) {

char *newText;
int i, len = text.size(), k=0;
newText = new char[len+1];

for(i=0; i<len; k++) {

newText[k] = text[i];

if(isspace(text[i])){
while(isspace(text[i]))

i++;
}
else{
  
i++;
}
}
  
newText[k] = '';
text = newText;
}

int FindText(string text, string phrase) {
  
int count = 0;

if (phrase.size() == 0)
return 0;

for(size_t offset = text.find(phrase); offset != string::npos; offset = text.find(phrase, offset + phrase.size())){
++count;
}

return count;
}

Errors

6. Compare output We'll continue our guest in space. There will be more shuttle flights and r Input n Spaces g Quit. Choose an option: Number of non-whitespace characters: 181e Your MENU output ends. c Number of non-whitespace characters with w Number of words f Find text r Replace all 's s Shorten spaces g Quit Choose an option Number of non-white space characters 181 MENU c Number of non-whitespace characters Expected w Number of words output f Find text ends. r Replace all 's with s Shorten spaces g Quit Choose an option:

Explanation / Answer

YOU JUST NEED TO ADD return 0; AFTER EVERY CASE

#include <iostream>
#include <string>
using namespace std;
char printMenu();
int GetNumOfNonWSCharacters(string);
int GetNumOfWords(string);
void ReplaceExclamation(string&);
void ShortenSpace(string&);
int FindText(string, string);
int main()
{
char option;
string text, phraseToFind;

cout << "Enter a sample text:"<<endl;
getline(cin, text);

cout << " You entered: " << text;

while(1){
option = printMenu();

switch(option) {
case 'q':
case 'Q': return 0;

case 'c':
case 'C': cout << "Number of non-whitespace characters:" << GetNumOfNonWSCharacters(text)<<endl;
return 0;
break;

case 'w':
case 'W': cout << "Number of words:" << GetNumOfWords(text) <<endl;
return 0;
break;

case 'f':
case 'F': cin.ignore(); cout << "Enter a word or phrase to be found: "; getline(cin, phraseToFind);
cout << " "" << phraseToFind << ""instances:" << FindText(text, phraseToFind) << endl;
return 0;
break;

case 'r':
case 'R': ReplaceExclamation(text); cout << "Edited text:" << text <<endl;
return 0;
break;

case 's':
case 'S': ShortenSpace(text); cout << "Edited text:" << text <<endl;
return 0;
break;

}
}
cout <<endl<<endl;

return 0;
}

char printMenu()
{
char ch;

cout <<" MENU";
cout << " c - Number of non-whitespace characters w - Number of words f - Find text r - Replace all !'s s - Shorten spaces q - Quit"<<endl;
cout <<" Choose an option: ";

cin >> ch;

return ch;
}

int GetNumOfNonWSCharacters(const string text){
int cnt = 0, i;
int len = text.size();

for(i=0; i<len; i++) {
if(!isspace(text[i]))
cnt++;
}

return cnt;
}

int GetNumOfWords(const string text){
int words = 0, i;
int len = text.size();

for(i=0; i<len;){
if(isspace(text[i])){
while(isspace(text[i]))
i++;

words++;
}
else{

i++;
}
}

words = words + 1;
return words;
}

void ReplaceExclamation(string& text){
string newText = text;
int i, len = text.size();

for(i=0; i<len; i++){
if(text[i] == '!')
newText[i] = '.';
}

text = newText;
}

void ShortenSpace(string& text) {

char *newText;
int i, len = text.size(), k=0;
newText = new char[len+1];

for(i=0; i<len; k++) {

newText[k] = text[i];

if(isspace(text[i])){
while(isspace(text[i]))

i++;
}
else{

i++;
}
}

newText[k] = '';
text = newText;
}

int FindText(string text, string phrase) {

int count = 0;

if (phrase.size() == 0)
return 0;

for(size_t offset = text.find(phrase); offset != string::npos; offset = text.find(phrase, offset + phrase.size())){
++count;
}

return count;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote