I've been coding in c++ for over a day now and I am currently stuck on this particular problem. I want to enter a integer and produce every prime numbers that are smaller than the given integer but the output always has random numbers that aren't prime. Here's the code:
#include <iostream>
#include <math.h>
using namespace std;
int check(int x){
bool prime = true;
if (x <= 1){
prime = false;
return 0;
}
for (int i = 2; i < x; i++){
if (x % i == 0){
prime = false;
return 0;
}
}
if (prime = true){
return x;
}
}
int main(){
int r;
cout << "Enter range: ";
cin >> r;
int primes[r];
int n = 0;
for (int i = 0; i < r; i++){
if (check(i) != 0){
primes[n] = i;
n++;
}
else{
}
}
for (int i : primes){
cout << i << endl;
}
}
Help me fix this problem. Thanks.