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

In summary, the conversation is about implementing a PrintMenu() function that takes in a string as a parameter and outputs a menu of options for analyzing and editing the string. The function should return the user's chosen option. The conversation also mentions implementing various cases such as Quit, Number of non-whitespace characters, Number of words, Find text, Replace all !'s, and Shorten spaces. The main() function should call the PrintMenu() function and continue to do so until the user chooses to quit by entering 'q'. There is also mention of having difficulty understanding how to declare variables, particularly char and string.
  • #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.

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
  • #2
You've posted 2 threads on this same assignment, but it appears the posts aren't identical. Which thread do you wish to keep?
 
  • #3
this one
 
  • #4
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
 

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

What is a PrintMenu() function in C++?

A PrintMenu() function in C++ is a user-defined function that is used to display a menu or options to the user. It is commonly used in console-based programs to provide a user-friendly interface.

How do I implement a PrintMenu() function in my C++ program?

To implement a PrintMenu() function in your C++ program, you can follow these steps:

  • Declare the function prototype at the beginning of your program.
  • Define the function by specifying its return type, name, and parameters.
  • Inside the function, use cout statements to display the menu options.
  • Call the function in your main program where you want the menu to be displayed.

What is the purpose of a PrintMenu() function?

The purpose of a PrintMenu() function is to provide a user-friendly interface for the user to interact with the program. It allows the user to select from a list of options and helps in controlling the flow of the program.

Can a PrintMenu() function be customized to display different options?

Yes, a PrintMenu() function can be customized to display different options based on the specific needs of the program. You can use conditional statements or switch cases to display different menus or options to the user.

Is it necessary to use a PrintMenu() function in my C++ program?

No, it is not necessary to use a PrintMenu() function in your C++ program. It is an optional feature that can be used to enhance the user experience and make your program more interactive. However, if your program does not require user input or options, then a PrintMenu() function may not be necessary.

Similar threads

Back
Top