简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM