You can simply use the absolute value of n, then change the sign of the answer based on the original input n.
double myroot(double n, double k)
{
bool isNegative = n < 0;
double result = std::pow(abs(n), 1.0 / k);
return isNegative ? result * -1 : result;
}