Haley Coutts
Haley Coutts

Reputation: 31

Passing a user input string in a different function in C++

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;
}

Upvotes: 1

Views: 847

Answers (3)

RHertel
RHertel

Reputation: 23788

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;
}

Upvotes: 0

Anirudh
Anirudh

Reputation: 385

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;
}

Upvotes: 4

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520898

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;
}

Upvotes: 5

Related Questions