Click to See Complete Forum and Search --> : [RESOLVED] Simple program crashes


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).

sunil_cguru
May 29th, 2008, 12:57 AM
for a start..your function fill(..) will go into an infinite loop since you dont increment cnum inside your for loop.

chris_t_morin
May 29th, 2008, 01:11 PM
Thanks. I made a few changes and the only thing is that the program does an infinite loop in the checkPrime function.


#include <iostream>
#include <fstream>
using namespace std;

unsigned long cnum=1;
const unsigned long fin = 100;
const unsigned long group = 10;
unsigned long cursec=0;


ofstream destination("results.txt");



bool checkPrime ()
{
for (int n=2;n<cnum;++n)
{
cout << "In checkPrime \n";
if ((cnum % n) == 0)
return false;
}
return true;
}


class sector
{
public:
sector ()
{
cout << "sector instance created \n";
destination << (cursec*group)+1 << " - " << (cursec+1)*group << " ";
++cursec;
}

~sector ()
{
cout << "sector instance destroyed \n";
destination << "\n";
}
};

void fill ()
{
cout << "fill entered \n";
int primes=0;
int a;
a = (int) group;
bool sector[a];
for (int i=0;cnum<=(cursec*(group+1));++i)
{
if (checkPrime())
{
cout << "(In fill) checkPrime was true, now changing numbers";
++primes;
++cnum;
sector[i]=true;
system("pause");
}
}
destination << primes;
}



int main ()
{
for (;cnum<=fin;)
{
cout << "first for loop entered\n";
sector asect;
fill();
}
}


All of the "cout"s are there so that I know where the program is. I'll take them out when I get the program to work.

chris_t_morin
May 29th, 2008, 01:38 PM
Nevermind, I got it.
the
++cnum
was in the wrong place.

thanks again.

Lindley
May 29th, 2008, 04:48 PM
You should learn how *not* to use global variables. They make debugging much harder.

chris_t_morin
May 29th, 2008, 06:44 PM
i figured that.
how about functions though, should I keep them in classes or make them global (if that is what a function witout a class is called).

Lindley
May 29th, 2008, 09:00 PM
Well, that depends on the function. If a group of functions operate on a common set of data, and logically operations on that data should only occur through those functions, then you have a good candidate for a class. The question to ask is, "does it make sense to think of this data as an object?"

If the answer is no, or if there is no consistent bit of data, but the functions are still related, consider using a namespace instead. Or just make them global if you wish, there's no harm in it.

chris_t_morin
May 29th, 2008, 09:05 PM
Thanks