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

AI Thread Summary
The discussion focuses on implementing a PrintMenu() function that takes a string parameter and displays a menu for analyzing or editing that string. The function should return the user's selected option, which is represented by a single character. If an invalid option is entered, the function should prompt the user until a valid choice is made, with the Quit option implemented first. The main function should repeatedly call PrintMenu() until the user opts to quit by entering 'q'.Participants express difficulty in declaring variables, particularly for char and string types, and seek assistance with these programming concepts. The provided code includes function declarations for various string manipulation tasks but lacks complete implementations. The user also notes confusion regarding the management of multiple threads on the same assignment, leading to a request for unique thread titles to avoid confusion.
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
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top