I am having trouble compiling this program, please help. Command prompt says there is an error in the int main() and I don't know how to correct it.
Problem: Write a void function that uses two nested for loops and the modulus(%) operator to detect and print to a specified output file, the first n prime integers.

 ``12345678910111213141516171819202122232425262728293031323334`` ``````#include using namespace std; void primeGen(int n, ofstream& file); int main() { int i,j,n; bool prime; cout << "Prime Numbers between 1 and " << n << endl; { { for (i=1;i<=n;i++) prime=true; } for (j=2;j
I don't see any logic here.

 ``12345678910111213141516171819202122232425262728293031`` ``````#include using namespace std; void primeGen(int n, ofstream& file); int main() { int i,j,n; bool prime; cout << "Prime Numbers between 1 and " << n << endl; { { for (i=1;i<=n;i++) prime=true; } for (j=2;j
hi tanyacjones,

Your code is mostly correct, but the concept of nested statements is to have one within the other.
e.g
 ``12345678`` ``````for (unsigned i = 0; i < n; ++i) { for (unsigned j = 0; j < i; ++j) { if (i % j == 0) { prime = false; break; } } }``````

You also want to store the prime numbers unless you're printing them out directly. If you want to store them I suggest using something like a vector.
 ``12345678`` ``````vector prime_numbers; prime_numbers.push_back(1); prime_numbers.push_back(2); prime_numbers.push_back(3); prime_numbers.push_back(5); // etc for (unsigned i = 0; i < prime_numbers.size(); ++i) cout << "Prime number " << i << " = " << prime_numbers[i] << endl;``````

Hope that helps :)
Thanks so much for all the help! I got it to compile however when I run the program it just says "Prime numbers 1 and 4200848"

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````#include #include #include #include #include #include using namespace std; #include using namespace std; #include using namespace std; void primeGen(int n, ofstream& file); int main() { int i,j,n; bool prime; cout << "Prime Numbers between 1 and " << n << endl; { { for (i=1;i<=n;i++) prime=true; } for (j=2;j
Don't know why it compiles. But you get "Prime numbers 1 and 4200848" because:
`cout << "Prime Numbers between 1 and " << n << endl;`
And how much is n? You have not given it any value.
`int i,j,n;`

 ``123456789101112`` ``````{ { for (i=1;i<=n;i++) prime=true; } for (j=2;j

what is the purpose of "}" in your example?
This is good:
 ``12345`` ``````for (j=2;j

But this...
 ``1234567`` ``````{ { for (i=1;i<=n;i++) prime=true; } //code }``````
 Problem: Write a void function that uses two nested for loops and the modulus(%) operator to detect and print to a specified output file, the first n prime integers.

This is NOT two nested loops, your logic is wrong.
 ``123456789101112`` ``````{ { for (i=1;i<=n;i++) prime=true; } for (j=2;j

This IS Two nested loops
 ``12345`` ``````for (unsigned i = 0; i < 10; ++i) { for (unsigned j = 0; j < 10; ++j) { cout << i << " : " << j << endl; } }``````

Please see my first response as that still holds true and you have not utilised what I have explained.
Topic archived. No new replies allowed.