I wouldn't do it with two loops, for starters. The following is untested, but intended to show the general approach I would try to use ....
Code:
#include <vector>
#include <iostream>
#include <iomanip>
using std::vector;
using std::cout;
using std::cin;
using std::endl;
using std::setw;
typedef std::vector<long>::iterator prime_iterator;
int main()
{
vector<unsigned long> prime;
unsigned long range;
// ask the user to input the range
cout << "I wanna know the prime numbers from 1 to : ";
cin >> range;
prime.push_back( 2L ); // 2 is the first one to sieve.
prime.push_back( 3L);
for ( unsigned long i = 3; i <= range; i += 2)
{
bool not_prime = true;
for (prime_iterator j = prime.begin() + 1; not_prime && j != prime.end(); ++j)
{
not_prime = ((i % (*j)) == 0);
}
if (not_prime) prime.push_back(i);
}
for (prime_iterator i = prime.begin(), end = prime.end(); i != end; ++i)
{
for (j = 0; j < 5 && i != end; ++j, ++i)
{
cout << setw(12) << *i;
}
cout << '\n';
}
return 0;
}