Convert Int to String in C++

Asked

Viewed 7,637 times

0

How do I make for the String resultado receive the value int of cont3 and the value char of c[cont]?

resultado=" ";
resultado=resultado+to_string(**cont3**)+to_string(**c[cont1**]);

I tried to use the to_string() but it didn’t solve my problem.

  • 1

    Take a look at this question in the English OS, you have several examples, simple to understand: https://stackoverflow.com/questions/191757/how-to-concatenate-a-stdstring-and-an-int

1 answer

2


You can do it this way:

#include <iostream>
#include <string>

int main()
{
  std::string resultado = " ";
  int n = 1;
  char l = 'a';

  resultado = resultado + std::to_string(n) + l;

  std::cout << resultado;
}

Result is already a string, n is of type int so it is converted with std::to_string and l is char and can be concatenated direct.

resultado = resultado + std::to_string(n) + l;

Upshot

This code is for C++11

  • You must leave the warning that you must be c++11

  • @Isac, I put, thank you

  • I did that, but I got the following error: 'to_string' was not declared in this Scope

  • Did you include the lib string? And its version is the 11?

  • I got it here, thank you very much!!

Browser other questions tagged

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