First of, here is my previous post.
That should explain a fair bit. The reason I'm posting one more time, is I have changed a lot. Using more classes, let them do all the work, and the overall structure.
#include <iostream>
const int DATE_LENGTH = 6;
class Date {
private:
char *ans;
protected:
int date[DATE_LENGTH];
int year;
public:
Date() {
askForDate();
}
~Date() {
if(ans) { delete ans; }
}
void askForDate() {
using namespace std;
ans = new char[DATE_LENGTH];
cout << "Date of Birth (DDMMYY): "; cin.getline(ans, DATE_LENGTH + 1);
while (!validDate(ans)) {
cin.getline(ans, DATE_LENGTH + 1);
}
for (int i = 0; i < DATE_LENGTH; i++) { date[i] = int(ans[i] - '0'); }
year = 1900 + date[4] * 10 + date[5];
}
bool validDate(const char *d) {
return strlen(d) == 6;
}
};
class IndividSiffer : public Date {
protected:
int i1, i2, i3;
public:
IndividSiffer(int ii1, int ii2, int ii3) : i1(ii1), i2(ii2), i3(ii3) {}
void individSiffer() {
static int individ = -1;
if(year >= 1900 && year <= 1999 ){
individ++;
i1 = i2 = i3 = 0;
}
i3 = individ % 10;
if(individ < 100){
i1 = 0;
i2 = individ / 10 % 10;
}else if(individ < 10){
i1 = i2 = 0;
}else{
i1 = individ / 100 % 10;
i2 = individ / 10 % 10;
}
}
};
class KontrollSiffer : public IndividSiffer {
protected:
int k1, k2;
public:
KontrollSiffer(int kk1 = 0, int kk2 = 0, int i1 = 0, int i2 = 0, int i3 = 0) : IndividSiffer(i1, i2, i3), k1(kk1), k2(kk2) {
for (int index = 0; index < 500; index++) {
individSiffer();
generate();
}
}
void generate() {
using namespace std;
k1 = 11 - ((3*date[0] + 7*date[1] + 6*date[2] + 1*date[3] + 8*date[4] + 9*date[5] + 4*i1 + 5*i2 + 2*i3) % 11);
if(k1 == 11){ k1 = 0; }
k2 = 11 - ((5*date[0] + 4*date[1] + 3*date[2] + 2*date[3] + 7*date[4] + 6*date[5] + 5*i1 + 4*i2 + 3*i3 + 2*k1) % 11);
if(k2 == 11){ k2 = 0; }
if((i3 != 0 && i3 != 2 && i3 !=4 && i3 !=6 && i3 !=8) && (k1 != 10 && k2 !=10)){
for (int i = 0; i < DATE_LENGTH; i++) { cout << date[i]; }
cout << i1 << i2 << i3 << k1 << k2 << endl;
}
}
};
int main(int argc, char *argv[]) {
KontrollSiffer k;
return 0;
}
I'd really like some input on how I structured it, or if things should be done differently.