I apologize if this question is naive, but I seem to be having some issues with vector push_back. Following Stroustrup's book, I'm trying to write a code to determine primes [1,100]. Everything I have written thus far seems to work, it correctly determines which ones are primes, but when trying to push the primes into a vector, it seems to not be pushing all the values, or doing so incorrectly
#include <iostream> // std::cout
#include <vector> // std::vector
using namespace std;
int prime(int num) {
for (int j = num-1; j >= 2; --j) {
if (num%j != 0) {}
else if (num%j == 0) return 1;
}
return 0;
}
int main() {
vector<int> found_primes;
int eval {0};
for (int i = 1; i <= 100; ++i) {
eval = prime(i);
if (eval > 0) {}
else if (eval == 0) found_primes.push_back(i); // I think the problem is here
}
for (int j : found_primes) {
cout << found_primes[j] << '\t';
}
}
This provides the output: "2 3 5 11 17 31 41 59 67 83 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
What exactly am I missing? Thanks in advance.