Age Calculator- Where am I going wrong?

I can't seem to get a grasp on things lately so I apologise for the multitude of posts.
My task is to create a program which will display the prefilled birthdate, current date & time, and the age (which needs to be suitable for the age ie either 25 years old or 3 days old).

Header file (am only able to change or add private functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#ifndef AGE_H
#define AGE_H

#include <iostream>
#include <string>
#include <ctime>
#include <sstream>

using namespace std;

class DateTime{
  public:
    DateTime(int y, int m, int d, int h = 0, int min = 0, int s = 0);
    void display();
    protected:         
    string get_string_component(char option, tm* dateStruct);
    int get_year_days(tm* dateStruct);
    struct tm DTstruct;
  private:
    bool validate_data( int y, int m, int d, int h, int min, int s); 
    }; 
   

class WeekDay : public DateTime{
    public: 
    WeekDay(int y, int m, int d, int h = 0, int min = 0, int s = 0);
    void display();
    private:
    bool validate_data( int y, int m, int d, int h, int min, int s); 
};



class Age : public DateTime  {
  public:
	// Date supplied is birth date, should be earlier than current date
	Age(int y, int m, int d, int h = 0, int min = 0, int s = 0);
	void display();
  protected:
    struct tm Astruct; //tm for current time
  private:
    string int_2_string(int);
	void error_action();
	string age;
    bool validate_data( int y, int m, int d, int h, int min, int s); 
    double calc;

	  

};	
   
#endif 



source file (have made 'today's date a set date purely for testing, this will change to current date)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "age.h"
#include <cstdlib>


Age::Age(int y, int m, int d, int h, int min, int s) : DateTime(y, m, d, h, min, s){
    Astruct.tm_year = 2015 - 1900;  // Legacy problem
    Astruct.tm_mon = 4 - 1;      // month starts from 0 not 1
    Astruct.tm_mday = 15;
    Astruct.tm_hour = 15;
    Astruct.tm_min = 17;
    Astruct.tm_sec = 15;
    Astruct.tm_wday = 0;
    Astruct.tm_yday = 0;
    Astruct.tm_isdst = 0;
    mktime (&Astruct);
    Age::error_action();
    
}


void Age::display(){
    DateTime::display() ;
    cout <<"Current date: " << DateTime::get_string_component('c', &Astruct) << endl;
 }

// when calc age, unit can be day, month or year. To differentiate, add 1,000 to age in months, add 100,000 to age in days. 
 
string Age::int_2_string(int n){
	ostringstream outstr;
	if (n > 100000){
		outstr << (n - 100000);
		return outstr.str() + " days old.";
	} else if (n > 1000) {
		outstr << (n - 1000);
		return outstr.str() + " months old.";
	} else {
		outstr << n;
		return outstr.str() + " years old.";
	}
		
}


void Age::error_action(){
     if(int n = (difftime(mktime(&Astruct), mktime(&DTstruct))) > 0){
     Age::int_2_string(n);
    } else     
    	
    cout << "Birthday must be in the past!\n";
	exit(EXIT_FAILURE);
}

 
DateTime::DateTime(int y, int m, int d, int h, int min, int s){
    DateTime::validate_data(y, m, d, h, min, s);
    DTstruct.tm_year = y - 1900;  // Legacy problem
    DTstruct.tm_mon = m - 1;      // month starts from 0 not 1
    DTstruct.tm_mday = d;
    DTstruct.tm_hour = h;
    DTstruct.tm_min = min;
    DTstruct.tm_sec = s;
    DTstruct.tm_wday = 0;
    DTstruct.tm_yday = 0;
    DTstruct.tm_isdst = 0;
    mktime ( &DTstruct );

}


string DateTime::get_string_component(char component, tm* dateStruct){
    char format[3];
    format[0] = '%';
    format[1] = component;
    format[2] = '\0';
    char ans [80];
    strftime(ans, 80, format, dateStruct);
    string strans(ans);
    return strans;
    } 
 
int DateTime::get_year_days(tm* DTstruct){
    return DTstruct->tm_yday;
    } 
 
 void DateTime::display(){
    cout << "Birthday:  " << DateTime::get_string_component('c', &DTstruct) << endl;

 }
 
 
 
 bool DateTime::validate_data(int y, int m, int d, int h, int min, int s) {
    if ( y >= 1970 && y <= 2020 && m >= 1 && m <= 12 && d >= 1 && d <= 31 && h >= 0 && h <= 23 && min >= 0 && min <= 59 &&s >= 0 && s <= 59){
        return true;
    } else
    cerr << "Incompatible data!\n" ;
    exit(EXIT_FAILURE);
}



main file (can not be changed)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include "age.h"

using namespace std;

int main(){
    
    Age AD1(1994, 12, 25, 3, 4, 5);
    Age AD2(1994, 12, 25, 3, 4);
    Age AD3(1994, 12, 25, 3);
    Age AD4(1994, 12, 25);
    
     //Age AD(2086, 1, 2)  // error
    // Age AD(1945, 1, 2)  // error
    // Age AD(2013, 0, 2)  // error
    // Age AD(2013, 13, 2)  // error
    // Age AD(2013, 1, 0)  // error
    // Age AD(2013, 1, 32)  // error
    // Age AD(2013, 13, 2, -1)  // error
    // Age AD(2013, 13, 2, 24)  // error
    // Age AD(2013, 13, 2, 3, -1)  // error
    // Age AD(2013, 13, 2, 3, 60)  // error
    // Age AD(2013, 13, 2, 3, 4, -1)  // error
    // Age AD(2013, 13, 2, 3, 4, 60 )  // error
    
    AD1.display();
    cout << endl;
    AD2.display();
    cout << endl;
    AD3.display();
    cout << endl;
    AD4.display();
    cout << endl;
    
    // suppose today is 15/04/2015
    Age AD5(2015, 4, 15);
    Age AD6(2015, 4, 1);
    Age AD7(2015, 3, 15);
    Age AD8(2014, 12, 25);
    Age AD9(2014, 1, 25);
    
    AD5.display();
    cout << endl;
    AD6.display();
    cout << endl;
    AD7.display();
    cout << endl;
    AD8.display();
    cout << endl;
    AD9.display();
    cout << endl;
    
    return 0;
}


My program compiles correctly, however it seems that my function error_action() function is what's not right. What am I missing?

Thanks for your help
Last edited on
Without knowing your issue I can only look at syntax.

In error_action your if is followed by an else statement with no curly braces. Without curly braces C++ will treat the next line as belonging to else.

So currently your function is logically equivalent to
1
2
3
4
5
6
7
8
void Age::error_action(){
     if(int n = (difftime(mktime(&Astruct), mktime(&DTstruct))) > 0){
         Age::int_2_string(n);
    } else {     
        cout << "Birthday must be in the past!\n";
    }
    exit(EXIT_FAILURE);
}


Also it doesn't look like you do anything with the string returned by Age::int_2_string in the if block.
Last edited on
That got it working (as well as it can do at the moment)- thanks so much!

Now to figure out how to get the output working properly
Topic archived. No new replies allowed.