How to pass string as parameter in C++

Asked

Viewed 1,569 times

0

Whoa, guys, chill out?

Next, programming teacher passed a little project and I need to pass a string as parameter in C++. How do I? I’ve seen it on the Internet and none of it worked.

I tested it like this:

void text_to_morse(std::string *frase, int tamanho);

Thus:

void text_to_morse(string& frase, int tamanho);

Thus:

void text_to_morse(char frase[], int tamanho);

Thus:

void text_to_morse(const char* frase, int tamanho);

And so:

void text_to_morse(const std::string &frase, int tamanho);

How to do it anyway? The function will translate a phrase that the user typed into morse code and save to a file.

  • Any one that works for you. The problem isn’t knowing which one to use, it’s knowing what you need.

1 answer

1


You should do it in the c++ style, where you don’t need the size parameter, just call the size() method to get the string size. And in the end return the string after conversion.

std::string text_to_morse(std::string frase){
     //converte a string frase
     return frase;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.