#include <iostream>#include <vector>#include <exception>///Prime Number Calculatorclass primes
{
std::vector<int> primeNumbers; //the vector that will hold the prime numbers (not to be confused with the object which is singular)bool primechecker (int b); //checks for prime-nessvoid primedisplayer (int o); //displays the number sent to it, which should be prime (if not, something is horribly, horribly wrong)void setVector (int l); //assigns that new-found prime number to the vector that holds the prime numberspublic:
int numberPrimes; //the number of primes that the user wantsbool memoryException = false; //whether the user's computer has enough memory to keep this runningvoid calculatePrimes(); //the for loop that everything runs in
primes (int); //the constructor
};
primes::primes(int a) //constructor, for simplicity's sake
{
numberPrimes = (a-1); //assigns whatever value you send it to the number of primes you want
}
bool primes::primechecker(int b)
{
bool test = false;
for(std::vector<int>::iterator i = primeNumbers.begin();
i != primeNumbers.end();
i++) //iteratres through the vector of primes
{
if(b % *i == 0)
{
test = true; //if the number isn't prime, set test to true and exit the loop.break; //no need to check every single other number once you know it isn't prime
}
}
return test; //return whether the number is prime or not
}
void primes::calculatePrimes() //the function where it all takes place
{
int m = 2; //make sure it starts checking primes at 2 as well
primedisplayer(m); //this is so it starts off with 2try//it's a vector, so exception-handling is mandatory
{
primeNumbers.emplace_back(2); //make the first prime number in the vector
}
catch(std::bad_alloc&)
{
std::cout << "Error: No memory to allocate initial value to vector. Exiting...";
memoryException = true;
}
if (memoryException == true) //if there is no memory to assign 2 to the vector
numberPrimes = 0; //make it so the for loop will never runfor(int i = 0; i < numberPrimes; i++)
{
while(primechecker(m) == true)
m++; //as long as the number isn't prime, add 1 to it
setVector(m); //when the number is prime, add it to the vectorif(memoryException == true) //if an error occured in memorybreak; //exit out
primedisplayer(m); //displays the current value of m
}
return;
}
void primes::setVector(int l)
{
try
{
primeNumbers.emplace_back(l); //add the prime number to the end of the vector of prime numbers
}
catch(std::bad_alloc&)
{
std::cout << "Error: Ran out of memory during allocation of new prime number to array.";
memoryException = true; //set the error check to true if there's no memoroy left, so the program can exit
}
return;
}
void primes::primedisplayer(int o)
{
std::cout << o << std::endl; //displays the current value of m, which is the just-calculated prime numberreturn;
}
int main()
{
int j; //number of prime numbers.
std::cout << "Please enter the number of prime numbers you want, starting with 2. Enter 0 to\nexit.\n";
std::cin >> j; //get input
std::cout << "\n"; //who doesn't like new lines?while(true)
{
if (j > 0) //if it's greater than 0, run the program
{
primes primeNumber(j); //create the object, calling the constructer with the inputted number (if valid)
primeNumber.calculatePrimes(); //then calls the function to get the ball rollingbreak; //then exits the loop when complete
}
elseif(j < 0) //if it's less than 0, ask again
{
std::cout << "Number is less than zero. Please try again.\n";
std::cin >> j;
std::cout << "\n"; //with another complimentary new line (I mean really, can you ever go wrong with new lines?)
}
else//if it's zero, exit out (or if it's invalid input, like a letter)break;
}
return 0;
}

If you caculate 1000 pime numbers and then ask to calculate 1001, it will calculate first 1000 all over again.
I suggest adding static vector of prime numbers and integer last checked number to your class

void primes::setVector(int l)
{
try
{
primeNumbers.emplace_back(l); //add the prime number to the end of the vector of prime numbers
}
catch(std::bad_alloc&)
{
std::cout << "Error: Ran out of memory during allocation of new prime number to array.";
memoryException = true; //set the error check to true if there's no memoroy left, so the program can exit
}
return;
}

It would be better to let the exception propagate to the calling code, which would simplify your code and move the error handling where it should be: outside of the class. If kept as-is, you need to remove the ability for the calling code to arbitrarily set the memoryException member (and, on a related note, numberPrimes should also not be a public member.)