The string reverse is 0 characters long so when you try assigning characters to it using operator[], you write out of bounds, making your program have undefined behavior.
One way to solve the problem is to create it with the same length as name:
string reverse(name.size(),' ');
or, to instead leave it as is, and add characters to it:
string reverse;
reverse.reserve(name.size()); // reserve space (may make it a little faster)
for (int i=0;i<count;i++){
reverse.emplace_back(name[count-i-1]); // or push_back
}
Another option could be to use std::copy and copy it in reverse using the std::strings reverse iterators:
#include <algorithm>
#include <iterator>
std::copy(name.rbegin(), name.rend(), std::back_inserter(reverse));
or, if you don't need to keep the original content in name, you could reverse it directly:
#include <algorithm>
std::reverse(name.begin(), name.end()); // reverses name itself
reverse.resize(name.length());.std::stringcan take iterators for constructor. Consider declaringstring reverse(name.crbegin(), name.crend());.