chris_t_morin
May 29th, 2008, 12:42 AM
Hi. I'm completely new to C++ (and to this site) and I am having trouble with the following code:
#include <iostream>
#include <fstream>
using namespace std;
long cnum=1;
const unsigned long fin = 100000;
const unsigned long group = 100;
unsigned long cursec=0;
ofstream destination("results.txt");
bool checkPrime (unsigned long num)
{
for (int i=2;i<num;++i)
{
if ((num % i) == 0)
return false;
}
return true;
}
class sector
{
public:
sector ()
{
destination << (cursec*group)+1 << " - " << (cursec+1)*group << " ";
++cursec;
}
~sector ()
{
destination << "/n";
}
};
void fill ()
{
int primes=0;
int a;
a = (int) group;
bool sector[a];
for (int i=0;cnum<=(cursec*(group+1));++i)
{
if (checkPrime(cnum))
{
++primes;
sector[i]=true;
}
}
destination << primes;
}
int main ()
{
for (;cnum<=fin;)
{
sector asect;
fill();
}
}
What I want the program to do is write a file that shows how many prime numbers are in a given size of numbers (100) over a large amount of numbers (100000).
I can compile the program but when I run it it crashes.
There is no real purpose to tis program other than being a learning exercise and helping me understand why this doesn't work would be much appreciated (along with tips on my coding style).
#include <iostream>
#include <fstream>
using namespace std;
long cnum=1;
const unsigned long fin = 100000;
const unsigned long group = 100;
unsigned long cursec=0;
ofstream destination("results.txt");
bool checkPrime (unsigned long num)
{
for (int i=2;i<num;++i)
{
if ((num % i) == 0)
return false;
}
return true;
}
class sector
{
public:
sector ()
{
destination << (cursec*group)+1 << " - " << (cursec+1)*group << " ";
++cursec;
}
~sector ()
{
destination << "/n";
}
};
void fill ()
{
int primes=0;
int a;
a = (int) group;
bool sector[a];
for (int i=0;cnum<=(cursec*(group+1));++i)
{
if (checkPrime(cnum))
{
++primes;
sector[i]=true;
}
}
destination << primes;
}
int main ()
{
for (;cnum<=fin;)
{
sector asect;
fill();
}
}
What I want the program to do is write a file that shows how many prime numbers are in a given size of numbers (100) over a large amount of numbers (100000).
I can compile the program but when I run it it crashes.
There is no real purpose to tis program other than being a learning exercise and helping me understand why this doesn't work would be much appreciated (along with tips on my coding style).