Ive made a simple class to generate the HCF of an array of numbers number given by the user. Whenever I input more than 10 numbers to find the HCF of it generates the HCF but after it says press any key to continue, Windows pops up with a this program has encountered a problem and needs to be shut down. I've gone through my whole code but I can't find the problem. Can you guys help? Thanks in adance.
Zayd
Here's the header file:
#ifndef HCF_H
#define HCF_H
/*
Highest Common Factor Class
*/
class HCF
{
public:
unsigned long buffer[];
unsigned long HCFans;
int MAX_FACTORS;
int index;
HCF();
~HCF();
int addFactor(unsigned long);
unsigned long getHCF();
unsigned long euclidHCF(unsigned long, unsigned long);
};
#endif
The class cpp file:
#include "hcf.h"
HCF::HCF()
{
HCFans = 1;
index = 0;
const int MAX_FACTORS = 25;
unsigned long buffer[MAX_FACTORS];
}
HCF::~HCF()
{
}
int HCF::addFactor(unsigned long factor)
{
if (index <= MAX_FACTORS)
{
unsigned long prevIndex = index;
buffer[index] = factor;
index = prevIndex + 1;
return 1;
}
if (index >= MAX_FACTORS)
{
return 0;
}
}
unsigned long HCF::getHCF()
{
unsigned long euclidAns;
euclidAns = euclidHCF(buffer[0], buffer[1]);
for (int i = 0; i < index; i++)
euclidAns = euclidHCF(euclidAns, buffer);
return euclidAns;
}
unsigned long HCF::euclidHCF(unsigned long a, unsigned long b)
{
if(b == 0)
return a;
else
return euclidHCF(b, (a % b));
}
And the main.cpp file
#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include "hcf.h"
using namespace std;
int main(int argc, char *argv[])
{
HCF h1;
h1.addFactor(1000);
h1.addFactor(432322);
h1.addFactor(432295);
h1.addFactor(20400);
h1.addFactor(5000);
h1.addFactor(343223400);
h1.addFactor(500143304);
h1.addFactor(50423200);
h1.addFactor(50003423);
h1.addFactor(500042636);
h1.addFactor(500042141);
h1.addFactor(500042243);
h1.addFactor(500042234);
h1.addFactor(500042334);
h1.addFactor(500042234);
h1.addFactor(500042234);
h1.addFactor(500042234);
h1.addFactor(500042334);
h1.addFactor(500042234);
h1.addFactor(500042234);
h1.addFactor(500042234);
h1.addFactor(500042234);
h1.addFactor(500042234);
h1.addFactor(500042234);
h1.addFactor(500042234);
cout << h1.getHCF() << endl;
h1.~HCF();
system("PAUSE");
return 0;
}