Please assist :)

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.
Thanks in advance,
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream> 
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;j++)
{
if (!(i%j)) 
prime=false;
}

}

if (prime)

cout << i << endl;


system("pause");
return 0;
} 
I don't see any logic here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream> 
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;j++)
        {
            if (!(i%j)) 
                prime=false;
        }

    }

    if (prime)

    cout << i << endl;

    system("pause");
    return 0;
}
hi tanyacjones,

Your code is mostly correct, but the concept of nested statements is to have one within the other.
e.g
1
2
3
4
5
6
7
8
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.
1
2
3
4
5
6
7
8
vector<unsigned> 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"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include<cmath>
#include <iostream> 
using namespace std; 

#include <iostream> 
using namespace std; 

#include <iostream> 
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;j++)
        {
            if (!(i%j)) 
                prime=false;
        }

    }

    if (prime)

    cout << i << endl;

    system("pause");
    return 0;
}
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;

1
2
3
4
5
6
7
8
9
10
11
12
{
        {
            for (i=1;i<=n;i++)
                prime=true;
        }
        for (j=2;j<i;j++)
        {
            if (!(i%j)) 
                prime=false;
        }

    }


what is the purpose of "}" in your example?
This is good:
1
2
3
4
5
for (j=2;j<i;j++)
        {
            if (!(i%j)) 
                prime=false;
        }

But this...
1
2
3
4
5
6
7
{
        {
            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.
1
2
3
4
5
6
7
8
9
10
11
12
{
        {
            for (i=1;i<=n;i++)
                prime=true;
        }
        for (j=2;j<i;j++)
        {
            if (!(i%j)) 
                prime=false;
        }

    }


This IS Two nested loops
1
2
3
4
5
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.