I am trying writing a program that will output all prime numbers less than or equal to the entered value. The math in the prime function is correct, but I cannot figure out how to return the output of 'cout << i << " ";'. How can I fix this?

#include <iostream>

using namespace std;

int prime(int n);

int divisible(int n, int d);

int main()

{

int n;

cout << "Please enter a positive integer.\n";

cin >> n;

prime(n);

system("pause");

return 0;

}

int prime(int n)

{

for (int i = 2; i < n; i++){

for (int d = 2; d < i; d++){

if (n % d == 0){

break;

}

else if (i % d != 0){

cout << i << " ";

break;

}

}

}

return;

}

#include <iostream>

using namespace std;

int prime(int n);

int divisible(int n, int d);

int main()

{

int n;

cout << "Please enter a positive integer.\n";

cin >> n;

prime(n);

system("pause");

return 0;

}

int prime(int n)

{

for (int i = 2; i < n; i++){

for (int d = 2; d < i; d++){

if (n % d == 0){

break;

}

else if (i % d != 0){

cout << i << " ";

break;

}

}

}

return;

}

Your return function does not return anything.

Try returning something or a variable.

`return`;

Try returning something or a variable.

`return` n;

well to return cout you will have to return an ostream not an int. Though I don't know why you are. Also to return you put

` ``return` something;

Can you explain what exactly you are trying to return from the function? An array of prime numbers? Then you'd have to return an int * ( int [] ) or a STL container such as a vector or array.
Topic archived. No new replies allowed.