Question
(C++)Recieving system and timeoout errors only on one software. I am wrting a string manipulation code to post as an assignment on a textbook software
(C++)Recieving system and timeoout errors only on one software.
I am wrting a string manipulation code to post as an assignment on a textbook software called zyBooks. My code runs fine on any IDE that I try, but whenever I post on zyBooks I get one of three errors: either an out of bounds error, a timeout error, or some traceback/memory error related to python. I contacted tech support but so far they haven't been much help. I'll attach a copy of my code as well as a couple of the errors I've recieved.
Code:
#include
int main() {
int GetNumOfNonWSCharacters(string); int GetNumOfWords(string); int FindText(string, string); string ReplaceExclamation(string); string ShortenSpace(string); char printMenu(); char option; string text, phraseToFind; cout << "Enter a sample text:"; getline (cin, text); cout << "You entered:" << text; while(1) { option = printMenu(); switch(option) { case 'q': case 'Q': exit(0); case 'c': case 'C': cout << "Number of non-whitespace characters:" << GetNumOfNonWSCharacters(text); break; case 'w': case 'W': cout << "Number of words:" << GetNumOfWords(text); break; case 'f': case 'F': cout << "Enter a word or phrase to find: "; getline(cin, phraseToFind); cout << phraseToFind << " instances: " << FindText(text, phraseToFind); break; case 'r': case 'R': text = ReplaceExclamation(text); cout << "Edited text: " << text; case 's': case 'S': text = ShortenSpace(text); cout << "Edited text: " << text; break; default: cout << "Invalid choice... Try again."; break; } } return 0; }
char printMenu() { char ch; cout << " \t Menu Options: "; cout << " \t c - Number of non-whitespace characters \t w - Number of words \t f - Find text \t r - Replace all !'s \t s - Shorten spaces \t q - Quit"; cout <<" \t 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 i; int words; int len = text.size();
for (i = 0; i < len;) { if (isspace(text[i])) { while (isspace(text[i])) i++; words++; } else { i++; } } return words; }
string ReplaceExclamation(string text) { string newText = text; int i, len = text.size(); for (i = 0; i < len; i++) { if (text[i] == '!') newText[i] = '.'; } return newText; }
string 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] = '\0'; return newText; }
int FindText (string text, string phrase) { int count = 0; if (phrase.size() == 0) return 0; for(size_t offset = text.find(phrase); offset != std::string::npos; offset = text.find(phrase, offset + phrase.size())) { ++count; } return count; }
Errors:
Traceback (most recent call last): File "./run", line 20, in
Submission failed. Please verify your code and try 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