1

I am very new to C++ and I am having a difficult time passing by reference/value?

I am attempting to pass the user input string from the get_sentence function into the replace_message function and replace the characters with underscores, but I it will not do that?

I am sorry if this is not specific enough, but I could really use some help.

int main() {
    string hidden, public_string;
    get_sentence();
    replace_message(hidden);
    return 0;
}

string get_sentence() {
    string hidden;
    cout << "Enter a message: ";
    getline (cin, hidden);
    return hidden;
}         

string replace_message(string &hidden) {
    string public_string;
     hidden = public_string;
    for(int i=0; i< public_string.length(); i++) {
      if(public_string[i] != ' ')
         public_string[i] = '_';
    }
    cout << "The message is" << public_string << endl;
    return public_string;
}
3
  • Is C++ your first OO language? If yes, I'd recommend learning something easier to grasp the basics of procedural programming and OO paradigm. I'd go for C#, but Java would work as well. With C++, you can run in all sorts of seemingly weird problems. Commented Feb 18, 2016 at 7:55
  • At my school they start us with C++ so everything will seem easier afterward. Commented Feb 18, 2016 at 8:02
  • Well... it certainly will, there is no arguing with that. Good luck then! Commented Feb 18, 2016 at 8:06

3 Answers 3

5

Modify your main() method to use the result which is returned from the get_sentence() function:

int main() {
    string hidden, public_string;

    hidden = get_sentence();      // store user message here
    replace_message(hidden);      // and pass it here

    return 0;
}

You should also modify your replace_message() function to use the input you pass to it:

string replace_message(string &hidden) {
    string public_string = hidden;

    for (int i=0; i< public_string.length(); i++)
    {
        if (public_string[i] != ' ')
            public_string[i] = '_';
    }

    cout << "The message is" << public_string << endl;

    return public_string;
}
Sign up to request clarification or add additional context in comments.

2 Comments

What is the use of returning public_string by replace_message if the returned string isn't stored in main? It would seem to make more sense if the function was void.
@RHertel Good point. He could consider making the function void, or if he actually intends to use the returned string later in his software project, he could leave it as is.
4
int main(){
string hidden;

hidden = get_sentence();

replace_message(hidden);

return 0;
}

You function is returning a value but you have to store that in a variable. Your replace_message function is also buggy because you are assigning and unintialized string public_string to your original message in the variable hidden. Do it as follows

void replace_message(string &hidden){

for(int i=0; i< hidden.length(); i++)
{
  if(hidden[i] != ' ')
     hidden[i] = '_';
}

cout << "The message is" << hidden << endl;
}

Comments

0

You could also avoid the loop and use std::regex_replace instead:

#include <iostream>
#include <regex>
#include <string>
using namespace std;
string get_sentence() {
    string hidden;
    cout << "Enter a message: ";
    getline (cin, hidden);
    return hidden;
}         

string replace_message(string &hidden) {
  regex non_blank("\\S"); // selects everything but whitespace characters
  string public_string = regex_replace(hidden, non_blank, "_");
  return public_string;
}

int main() {
    string hidden, public_string;
    hidden = get_sentence();
    public_string = replace_message(hidden);
    cout << "The message is: " << public_string << endl;
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.