'to_string' was not declared in this scope
#include <iostream>
int main() {
int num = 42;
std::string str = std::to_string(num);
std::cout << "Converted string: " << str << std::endl;
return 0;
}
Explanation:
#include <iostream>
: This line includes the necessary header file for input and output operations in C++.int main() {
: This line declares the main function, the starting point of the C++ program.int num = 42;
: This line declares an integer variable namednum
and initializes it with the value 42.std::string str = std::to_string(num);
: This line converts the integernum
to a string using thestd::to_string
function and assigns the result to a string variable namedstr
.std::cout << "Converted string: " << str << std::endl;
: This line prints the converted string to the standard output (console).return 0;
: This line indicates that the program has executed successfully, and themain
function returns 0.}
: Closing brace indicating the end of themain
function and the end of the program.