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 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top