Given an integer, we need to find the super digit of the integer.
We define super digit of an integer x using the following rules:
If x has only 1 digit, then its super digit is x
Otherwise, the super digit of  is equal to the super digit of the digit-sum of x. Here, digit-sum of a number is defined as the sum of its digits.
Example:
super_digit(9875) = super_digit(9+8+7+5) 
= super_digit(29) 
= super_digit(2+9)
= super_digit(11)
= super_digit(1+1)
= super_digit(2)
= 2
Task:
You are given two numbers n and k. You have to calculate the super digit of p.p is created when number n is concatenated k times.
My Code to solve this problem is shown below.
#include <bits/stdc++.h>
#include <string>
#include <iostream>
#include <math.h>
using namespace std;
int superDigit(string n, int k)
{
    int res=0;
    for (int x = 0; x < n.length(); x++)
    {
        res += n[x] - '0';
    }
    res = k * res;
    if (res < 10)
        return res;
    else
        return superDigit(to_string(res),1);
}
int main() {
    string n;
    int k;
    cin >> n;
    cin >> k;
    cout << superDigit(n, k)<< endl;
    return 0;
}
The code seems to work normally for all small numbers, but when n 
is 1e100000-1 and k is 100000, the program returns the following error:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
I think it's a memory leak, but how do I fix this. Where is the leak happening?
 
    