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.
#include <iostream>
usingnamespace 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;
}
#include <iostream>
usingnamespace 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:
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.