Magic square functions I'm writing a program for magic square and I'm a relative beginner to C++ and programming. I was hoping someone could help me find alternate or better ways to implement my code. This is the main function. I also entered a function to check if the input is odd, max 19, and to quit.

Is there a way to not have back-to-back for  and if statements?

#include &lt;iostream&gt;
#include &lt;limits&gt;
#include &lt;iomanip&gt;

using namespace std;
int oddfunction(int &amp;a);
int endfunction(int &amp;exit);
int maxfunction();  
int main()
{ 
    int r, c, d, e, z=0;
    int *f = new(nothrow) int;
    int *g= new(nothrow) int;
    int magic[120][120];
    cout &lt;&lt; "EML 2032 Final Project: Magic Square (odd numbers) " &lt;&lt; endl;

    while(*g != 0)
    {
        cout &lt;&lt; "\n\nMagic Squares for integers less than 20. ";
        for ( r=0 ; r&lt;120 ; r++ )
        for ( c=0 ; c&lt;120 ; c++ )
        magic[r][c] = 0;
        *f = oddfunction(*f);
        z = *f;
        cout &lt;&lt;"\n\n";
        d = 0 ; 
        e = (z-1) / 2;
        magic[d][e] = 1;
        for ( r=2 ; r &lt;= z * z ; r++ )
        {
            d--; 
            e++;
            if ( d &lt; 0 &amp;&amp; e == z )
            {
                d  = d + 2;
                e--;
            }
            if ( d &lt; 0 ) { d = z - 1; } if ( e &gt;= z ) { e = 0; } if ( magic[d][e] != 0 )
            {
                d = d + 2;
                e = e - 1;
            }
            magic[d][e] = r;
        }
        for(r=0;r &lt; z;r++)
        {
            for(c = 0; c &lt; z; c++)
            {
                cout &lt;&lt; setw(4) &lt;&lt; magic[r][c];
            }
            cout &lt;&lt; endl;
        }
        endfunction(*g);   
   }
   cout &lt;&lt; endl;
   cout &lt;&lt; "Have a nice day!        THE END \n";
   system("pause");
   return 0;
}

int maxfunction() 
{
    int c=0;
    cin &gt;&gt; c;
    while(c &gt;= 20)
    {
        cin.clear();
        cin.ignore(numeric_limits&lt;int&gt;::max(),'\n');
        cout &lt;&lt; "Input an odd integer up to 19.\n";
        cin &gt;&gt; c;
    }
    return c;
}
int oddfunction(int &amp;a) 
{
    int cont=0;
    while(cont == 0)
    {
        cout &lt;&lt; "Input an odd number: " &lt;&lt; endl;
        a = maxfunction();
        if(a % 2 == 0 )
        {
            if(a % 2 == 0 )
            {
                cout &lt;&lt; "That wasn't odd! (odd numbers aren't divisible by 2)\nEnter any number to continue:" &lt;&lt; endl;
                a = maxfunction();
            }
            else
            {
                a = maxfunction();
                cont++;
            }
        }
        else
        {
            cont++;
        }
    }
    return a;
}
int endfunction(int &amp;exit)
{
    int h=0;
    int i=2032;
    h = exit;
    cout &lt;&lt; "\n-Input 2032 to quit... \n-Input any other number to continue..." &lt;&lt; endl;
    cin &gt;&gt; h;
    if(h == i)
    {
        exit = 0;
        return exit;
    }
    else
    {
        exit = 1;
        return exit;
    }
}
Naming

Avoid single-letter identifiers. r, c, d, e, z - who are those guys? What's their purpose? I need to thoroughly study the code to understand that, and then I'd immediately forgot them. Make my life easier.

oddfunction is really get_odd_number
Spacing

if ( d &lt; 0 ) { d = z - 1; } if ( e &gt;= z ) { e = 0; } if ( magic[d][e] != 0 )


is not possible to read. Sparing lines will not make your program run any better, but makes understanding and maintaining it much harder. I hope it is a copy-paste problem.
Redundancy

f and g do not have a right to exist. Especially they do not have right to exist as pointers. Also, you should make up your mind and return the result e.g. of oddfunction either by reference or by return value.
Separation of responsibilities

oddfunction prompts for an odd number, and calls maxfunction, which also prompts for an odd number, this time stating the limit. Double maintenance problem right away. Make one function to deal with IO (prompt and read), and another with business logic (validation). The same validation function shall recognize 2032 as a termination condition.

Along the same line, separate output from the calculations.
The algorithm is not reviewed, mostly due to the naming problem. An introductory comment (or a link to the algorithm) would also be helpful.