I'm new in programming. I try to insert the letter 't' in a string with a for loop. Compiler is fine, no error but when I try to see what is in my variable with cout <<, it's empty.
It is. Default constructor is called and lol now is an empty string with the size of 0.
So it does not have any letters inside so any attempt to access them with operator[] can lead to disaster.
Either use lol.push_back('t'); in a loop to add letters to the end, or make use of one of the many string constructors:
1 2 3 4
int comp;
std::cout << "number of letter : ";
std::cin >> comp;
std::string lol(comp, 't');
, or use a .resize() member:
1 2 3 4 5
std::string lol;
int comp;
std::cout << "number of letter : ";
std::cin >> comp;
lol.resize(comp, 't');