Got a random bug that I cannot figure out. It's in the PrintMenu function. While I can select any option I want as many times, if I press 'c' then press 'q', it won't work. But, if I continue to press 'q' (total 5 times), then it quits the program. Oddly enough, if I press 'w' then 'q', I would have to press 'q' and 'enter' 4 times. I been stuck on this bug all day!
// ConsoleApplication2.cpp : This file contains the 'main'
function. Program execution begins and ends there.
//
#include "pch.h"
#include
#include
using namespace std;
using namespace std;
void GetNumOfNonWSCharacters(const string& userWord);
int GetNumOfWords(const string userWord);
int FindText(string text, string phrase);
string ReplaceExclamation(string userWord);
string ShortenSpace(string userWord);
int PrintMenu(string userString);
int main() {
string userString;
cout << "Enter a sample text:\n";
getline(cin, userString);
cout << endl << "You entered: " << userString << endl;
PrintMenu(userString);
cout << "\t IT WORKS";
return 0;
}
int PrintMenu(string userString) {
char option;
string findWord;
cout << endl << "MENU\n";
cout << "c - Number of non-whitespace
characters\n" << "w - Number of words\n";
cout << "f - Find text\n" << "r - Replace
all !'s\n" << "s - Shorten spaces\n";
cout << "q - Quit\n";
cout << endl << "Choose an option:"
<< endl;
cin >> option;
while (option != 'q' && option != 'c'
&& option != 'w' && option != 'f' && option
!= 'r' && option != 's') {
cout << "Invalid, Enter a
valid choice: ";
cin >> option;
}
while (option) {
switch (option) {
case 'c':
if (option !=
'q') {
GetNumOfNonWSCharacters(userString);
PrintMenu(userString);
}
case 'w':
if (option !=
'q') {
cout << endl << "Number of words:
";
cout << GetNumOfWords(userString) <<
endl;
PrintMenu(userString);
}
case 'f':
if (option !=
'q') {
cout << endl << "Enter a word or
phrase to be found:" << endl;
cin.ignore();
getline(cin, findWord);
cout << "\"" << findWord <<
"\" instances: ";
cout << FindText(userString, findWord)
<< endl;
PrintMenu(userString);
}
case 'r':
if (option !=
'q') {
cout << "Edited text: ";
cout << ReplaceExclamation(userString)
<< endl;
PrintMenu(userString);
}
case 'q':
break;
case 's':
if (option !=
'q') {
cout << "Edited text: ";
cout << ShortenSpace(userString) <<
endl;
PrintMenu(userString);
}
default:
return 0;
}
return 0;
}
}
void GetNumOfNonWSCharacters(const string& userWord) {
int ctr = 0;
int i;
int WORD_SIZE = userWord.size();
for (i = 0; i < WORD_SIZE; ++i) {
if (userWord.at(i) != ' ') {
ctr += 1;
}
}
cout << endl << "Number of non-whitespace
characters: " << ctr << endl;
}
int GetNumOfWords(const string userWord) {
int numWords = 0;
int i;
int STRING_SIZE = userWord.size();
for (i = 0; i < STRING_SIZE; ++i) {
if (userWord.at(i) == ' ') {
if
(userWord.substr(i, 3) == " ") {
numWords += 1;
i += 2;
}
else if
(userWord.substr(i, 2) == " ") {
numWords += 1;
i += 1;
}
else {
numWords += 1;
}
}
}
numWords += 1;
return numWords;
}
string ReplaceExclamation(string userWord) {
int i;
int STRING_SIZE = userWord.size();
for (i = 0; i < STRING_SIZE; ++i) {
if (userWord.at(i) == '!') {
userWord.at(i) =
'.';
}
}
return userWord;
}
string ShortenSpace(string userWord) {
char *newText;
int i, len = userWord.size(), k = 0;
newText = new char[len + 1];
for (i = 0; i < len; k++) {
newText[k] = userWord[i];
if (isspace(userWord[i])) {
while
(isspace(userWord[i]))
i++;
}
else {
i++;
}
}
newText[k] = '\0';
userWord = newText;
return userWord;
}
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;
}
Now the code is working, please give the thumbs up, thanks
code:
#include<iostream>
//#include
using namespace std;
using namespace std;
void GetNumOfNonWSCharacters(const string& userWord);
int GetNumOfWords(const string userWord);
int FindText(string text, string phrase);
string ReplaceExclamation(string userWord);
string ShortenSpace(string userWord);
int PrintMenu(string userString);
int main() {
string userString;
cout << "Enter a sample text:\n";
getline(cin, userString);
cout << endl << "You entered: " << userString << endl;
PrintMenu(userString);
cout << "\t IT WORKS";
return 0;
}
int PrintMenu(string userString) {
char option;
string findWord;
cout << endl << "MENU\n";
cout << "c - Number of non-whitespace characters\n" <<
"w - Number of words\n";
cout << "f - Find text\n" << "r - Replace all !'s\n"
<< "s - Shorten spaces\n";
cout << "q - Quit\n";
cout << endl << "Choose an option:" <<
endl;
fflush(stdin);
cin >> option;
while (true) {
if(option != 'q' && option != 'c'
&& option != 'w' && option != 'f' && option
!= 'r' && option != 's')
{
cout << "Invalid, Enter
a valid choice: ";
cin >> option;
}
else
{
break;
}
}
switch (option) {
case 'c':
GetNumOfNonWSCharacters(userString);
PrintMenu(userString);
break;
case 'w':
cout << endl << "Number of words: ";
cout << GetNumOfWords(userString) << endl;
PrintMenu(userString);
break;
case 'f':
cout << endl << "Enter a word or phrase to
be found:" << endl;
cin.ignore();
getline(cin, findWord);
cout << "\"" << findWord << "\" instances:
";
cout << FindText(userString, findWord) << endl;
PrintMenu(userString);
break;
case 'r':
cout << "Edited text: ";
cout << ReplaceExclamation(userString) << endl;
PrintMenu(userString);
break;
case 'q':
return 0;
break;
case 's':
cout << "Edited text: ";
cout << ShortenSpace(userString) << endl;
PrintMenu(userString);
break;
default:
cout<<"Wrong input"<<endl;
break;
}
}
void GetNumOfNonWSCharacters(const string& userWord) {
int ctr = 0;
int i;
int WORD_SIZE = userWord.size();
for (i = 0; i < WORD_SIZE; ++i) {
if (userWord.at(i) != ' ') {
ctr += 1;
}
}
cout << endl << "Number of non-whitespace characters: "
<< ctr << endl;
}
int GetNumOfWords(const string userWord) {
int numWords = 0;
int i;
int STRING_SIZE = userWord.size();
for (i = 0; i < STRING_SIZE; ++i) {
if (userWord.at(i) == ' ') {
if (userWord.substr(i, 3) == " ") {
numWords += 1;
i += 2;
}
else if (userWord.substr(i, 2) == " ") {
numWords += 1;
i += 1;
}
else {
numWords += 1;
}
}
}
numWords += 1;
return numWords;
}
string ReplaceExclamation(string userWord) {
int i;
int STRING_SIZE = userWord.size();
for (i = 0; i < STRING_SIZE; ++i) {
if (userWord.at(i) == '!') {
userWord.at(i) = '.';
}
}
return userWord;
}
string ShortenSpace(string userWord) {
char *newText;
int i, len = userWord.size(), k = 0;
newText = new char[len + 1];
for (i = 0; i < len; k++) {
newText[k] = userWord[i];
if (isspace(userWord[i])) {
while (isspace(userWord[i]))
i++;
}
else {
i++;
}
}
newText[k] = '\0';
userWord = newText;
return userWord;
}
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;
}
Got a random bug that I cannot figure out. It's in the PrintMenu function. While I...