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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top