int prim(int x, int y,int i, int j){
for (i=2; x>0;i++ ) {
bool isPrime;
for (j=2; j<i ; j++) {
if (i%j==0) {
isPrime = false;
}
}
if (isPrime) {
--x;
return i;
}
}
}
int main()
{
int a;
int b;
int c=2;
int d=2;
int x;
cout << "Enter a number" <<endl;
cin>>a;
cout << "Enter a number" <<endl;
cin>>b;
x=prim(a,b,c,d);
cout << x <<endl;
return 0;
}
I made a couple of changes to your code. I used a void function instead of int because there are cases (perhaps the majority) where there are more than 1 prime number between the two numbers the user inputs. I tried to add comments where I made some changes
#include <iostream>
#include <cmath>
usingnamespace std;
void prim(int x, int y) // void function and removed the two extra parameters that were only used in the for loops
{
bool isPrime;
for (int n =x;n<=y;n++) // loops between the two numbers the user inputs
{
isPrime = true; //set isPrime as true unless it is not prime, which you find with the next loop
for (int i=2;i<n-1;i++) // checks for primes within the previous loop
{
if (n%i==0)
{
isPrime = false;
}
}
if (isPrime)
{
cout << n << endl; //replaces your return from the int function
}
}
}
int main()
{
int a;
int b; // removed additional variables only used in for loops in function
cout << "Enter a number" <<endl;
cin>>a;
cout << "Enter a number" <<endl;
cin>>b;
prim(a,b); // function call
return 0;
}