How to make a concatenation between a string and another primitive type in a function return?

Asked

Viewed 38 times

0

I’m doing a function that returns all class information ContaBanco.h, as, the current balance, owner, type, etc, but the compiler understands as a sum and not as concatenation. How can I solve this?

The code:

string statusConta() { 
    return "\nDono: " + getDono() + "\nAberta: " + getStatus() + "\nNumero: " + getNumConta() + "\nTipo: " + getTipo() + "\nSaldo: " + getSaldo();
}
  • 2

    Tried to use the function to_string?

2 answers

0

If you are using a compiler that supports C++14 you use the suffix "s" to indicate a string.

Complementing: for numeric types, use to_string to convert to string. Assuming getDono, getStatus, return numerical values, spin like this:

...
using namespace std::string_literals;
...
...
string statusConta()
  return "\nDono"s + to_string(getDono())...

If you are using a compiler that does not support the suffix "s" for strings, then you can convert "xxx" (which is a pointer of type const char *) to string like this:

...
using namespace std::string_literals;
...
...
string statusConta()
  return string("\nDono") + to_string()getDono()...

0

Uses to_string:

string statusConta() { 
   return "\nDono: " + to_string(getDono()) + "\nAberta: " + to_string(getStatus()) + "\nNumero: " + to_string(getNumConta()) + "\nTipo: " + to_string(getTipo()) + "\nSaldo: " + to_string(getSaldo());
}

Browser other questions tagged

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