Sieve of Eratosthenes prime number finder up to n in C++ I'be been learning c++ recently and as an exercise I wanted to write a program to find numbers up to n using sieve of Eratosthenes. 

I'm new to c++ and programming in general so I'm not sure if this is wildly inefficient or not. I would love some feedback for my code performance wise or better programming practices in general.

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;cmath&gt;
using namespace std;


int square(int a){
    return a*a;
}


int main(){

    cout &lt;&lt; "Enter a number ";
    int MAXIMUM;
    cin&gt;&gt; MAXIMUM;
    int sqrtMaximum = 0;

    //populate allPrimes from 1-MAXIMUM
    vector&lt;int&gt; allPrimes(MAXIMUM);
    for(int i = 1; i&lt;MAXIMUM; i++){
        allPrimes[i] = i;
    }

    //get the square root closest to MAXIMUM (&gt;=)
    if(square(sqrt(MAXIMUM)) &lt; MAXIMUM){
        sqrtMaximum = sqrt(MAXIMUM)+1;
    }
    else{sqrtMaximum = sqrt(MAXIMUM);}


    vector&lt;int&gt; primesOfSqrt = {2,3};

    //adding numbers to list of prime numbers under sqrt
    bool prime = true;
    for(int i = 3; i&lt;sqrtMaximum; i+=2){
        prime = true;
        for(int x : primesOfSqrt){
            if(i % x == 0){
                prime = false;
                break;
            }
        }
        if(prime){
            primesOfSqrt.push_back(i);
        }
    }
    for(int x:primesOfSqrt){cout&lt;&lt;x&lt;&lt;"|||||";}cout&lt;&lt;endl;//print prime numbers under sqrt

    //removing multiples of the prime numbers found under the sqrt
    int factor = 0;
    allPrimes[1] = 0;
    for( int j : primesOfSqrt){
        factor = MAXIMUM/j;
        for(int x = 2; x &lt;= factor; ++x){
            allPrimes[j*x] = 0;
        }
    }
    for(int x = 1; x&lt;MAXIMUM; x++){
        if(allPrimes[x]!=0){cout&lt;&lt;allPrimes[x]&lt;&lt;'\t';}
    }
    char ch; cin&gt;&gt;ch;
}


I used 2 vectors to keep track of prime numbers, one for prime numbers less than the sqrt of n and another to remove multiples of those prime numbers, which I did by setting it to 0, but for bigger numbers i would be setting certain indexes to 0 multiple times, which would only increase the bigger the number gets. I'm not sure how to go about optimizing it or if using a different container would be best so any pointers there would be appreciated as well.

lastly I'm not sure if I am actually doing sieve of eratosthenes right, I'm pretty sure I am, but if I'm not I would like to know.
What you are doing here is first calculating primes under sqrt and then crossing out the multiples like sieve says. However the standard approach is to calculate primes as you go and cross out multiples of each prime :

vector&lt;bool&gt; isPrime(MAXIMUM, true);   //we only need bool, less memory usage

isPrime[0] = false;
isPrime[1] = false;

for (int i = 4; i &lt; MAXIMUM; i+=2) { // even numbers are handled first for efficiency
    isPrime[i] = false;
}

for(int i = 3; i &lt; MAXIMUM; i+=2){
    if (isPrime[i]) {
        //the usual is for(int j = i * 2; j &lt; MAXIMUM; j += i) { but I am using a bit more optimized loop
        for(int j = i * i; j &lt; MAXIMUM; j += 2 * i) {
            isPrime[j] = false;
        }
    } 
}

for(int x = 1; x&lt;MAXIMUM; x++){
    if(isPrime[x]){cout&lt;&lt; x &lt;&lt;'\t';}
}


This way code is much shorter and if you try it, also much faster.