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

Discussion Overview

The discussion centers around implementing a PrintMenu() function in a C++ program that prompts users for options related to string analysis and editing. Participants are addressing issues with variable declaration, function calls, and the overall structure of the program.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • The original poster (OP) expresses confusion about how to properly declare variables, particularly for char and string types.
  • The OP shares a code snippet and asks for help with the PrintMenu() function and its integration with other functions.
  • There is a mention of needing to implement a Quit option before other menu options, indicating a potential design consideration.
  • The OP notes that they are having trouble with function calls and the expected return types, particularly in relation to GetNumOfNonWSCharacters.
  • Another participant points out that the OP has posted multiple threads on the same assignment, suggesting a need for clarity and organization in their inquiries.

Areas of Agreement / Disagreement

There is no consensus on the specific issues raised by the OP, as the discussion primarily revolves around clarifying the OP's confusion rather than resolving any technical disagreements.

Contextual Notes

The OP's code includes multiple function declarations and calls, but there are indications of missing implementations and potential misunderstandings regarding function parameters and return types.

Who May Find This Useful

Students learning C++ programming, particularly those struggling with function implementation and variable declaration.

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
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 5 ·
Replies
5
Views
7K