Pattern magic solution in cpp language

#include <bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin >> n;
int r;

for (r = 0; r < n; r = r + 1)
{
    if (r == 0)
    {
        for (int i = 1; i <= 2 * n - 1; i = i + 1)
        {
            cout << '*';
        }
    }

    else
    {
        for (int i = 1; i <= n - r; i = i + 1)
        {
            cout << '*';
        }
        for (int i = 1; i <= (2 * r) - 1; i = i + 1)
        {
            cout << " ";
        }
        for (int i = 1; i <= n - r; i = i + 1)
        {
            cout << '*';
        }
    }
    cout << endl;
}
for (r = n - 2; r >= 0; r = r - 1)
{
    if (r != 0)
    {
        for (int i = 1; i <= n - r; i = i + 1)
        {
            cout << '*';
        }
        for (int i = 1; i <= (2 * r) - 1; i = i + 1)
        {
            cout << " ";
        }
        for (int i = 1; i <= n - r; i = i + 1)
        {
            cout << '*';
        }
    }
    else
    {
        for (int i = 1; i <= 2 * n - 1; i = i + 1)
        {
            cout << '*';
        }
    }
    cout << endl;
}

return 0;

}

what is the problem with this code ? why is it failing the test case?