- #1
elmessican
- 6
- 0
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 Quitwhat I'm i doing wrong on this assignment?
all the cases are functions, that i haven't done yet I am just having trouble understanding how to properly declare a variable especially the char and string. Any help would be great thanks.
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 Quitwhat I'm i doing wrong on this assignment?
all the cases are functions, that i haven't done yet I am just having trouble understanding how to properly declare a variable especially the char and string. Any help would be great thanks.
Code:
#include <iostream>
#include <string>
using namespace std;
void ShortenSpace();
void GetNumOfNonWSCharacters();
void FindText();
void ReplaceExclamation();
void ShortenSpace();
void GetNumOfWords();
string GetNumOfNonWSCharacters( string numChar){
// Loop index
for(int i=0; i < numChar.length(); i++)
if(numChar[i] == ' ') numChar.erase(i,1);
return numChar.length();
}
void PrintMenu(string option){
do {
cout << "MENU" << endl;
cout << "c - Number of non-whitespace characters" << endl;
cout << "w - Number of words" << endl;
cout << "f - Find text"<< endl;
cout << "r - Replace all !'s" << endl;
cout << "s - Shorten spaces" << endl;
cout << "q - Quit" << endl;
cout << endl;
cout << "Choose an option: "<< endl;
cin >> option;
switch (option) {
case 'c':
GetNumOfNonWSCharacters();
break;
case 'w':
GetNumOfWords();
break;
case 'f':
FindText();
break;
case 'r':
ReplaceExclamation();
break;
case 's':
ShortenSpace();
break;
case 'q':
cout << "Bye" ;
break;
default: cout << option << "Choose an option: ";
cout << endl;
}
} while (option != 'q' ); return option;
}
int main() {
string userInput;
string userOptions;
string menu;
char numChar;
cout << "Enter a sample text: " << endl;
getline(cin, userInput);
cout << "You entered: " << userInput << endl;
cout << endl;
menu = PrintMenu();
numChar = GetNumOfNonWSCharacters('c');
cout << "Number of non-whitespace characters: " << numChar<< endl;
return 0;
}
Last edited: