I was trying to write Fizz Buzz and I came across an unexpected interaction. If I do std::cout << i, it automatically converts i (int) into a string and prints it? But if assign i to a string variable, it prints a blank instead? I managed to solve it by using std::to_string, but I was just wondering why printing to_print prints a blank instead of either an integer or throwing some sort of error?
#include <iostream>
#include <string>
int main() {
for (int i = 1; i <= 10; i++) {
// prints i
std::cout << i;
}
std::cout << std::endl;
for (int i = 1; i <= 10; i++) {
std::string to_print;
to_print = i;
// prints blank rather than i
std::cout << to_print;
}
}