Need help with C++ program (Implement a PrintMenu() function)

  • Context: C/C++ 
  • Thread starter Thread starter elmessican
  • Start date Start date
  • Tags Tags
    C++ Function Program
Click For Summary
SUMMARY

The discussion centers on implementing a PrintMenu() function in C++ that outputs a menu for string analysis and editing. The function accepts a string parameter and prompts the user for a single-character option, looping until the user chooses to quit by entering 'q'. Key issues highlighted include the correct declaration of variables, particularly char and string, and the need to implement various functions such as GetNumOfNonWSCharacters() and GetNumOfWords() to support the menu options.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with functions and return types in C++
  • Knowledge of string manipulation in C++
  • Basic input/output operations in C++ using cin and cout
NEXT STEPS
  • Implement the GetNumOfWords() function to count words in a string
  • Develop the ReplaceExclamation() function to replace exclamation marks in a string
  • Learn about error handling in C++ to manage invalid user inputs
  • Explore C++ string methods for efficient string manipulation
USEFUL FOR

C++ developers, programming students, and anyone looking to enhance their skills in string manipulation and user input handling in C++ applications.

elmessican
Messages
6
Reaction score
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.

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:
Technology news on Phys.org
You've posted 2 threads on this same assignment, but it appears the posts aren't identical. Which thread do you wish to keep?
 
this one
 
elmessican said:
this one

Okay, I deleted the other. And I would ask that in the future, please use unique thread titles, specific to the questions being asked. Having several threads with the exact same title can be confusing. Thanks! :D
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
Replies
10
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 14 ·
Replies
14
Views
34K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
Replies
2
Views
2K