Header and Source File errors

I'm struggling to understand where I'm going wrong, I guess I don't fully understand the concept of needing a header and a source file (why don't the just put the declaration and the definition in the one cpp or header file?)

Anyway, I'm getting the following errors and despite looking at them (and googling ten's of pages) for the past day I don't get where I'm going wrong.

datetime.cpp: In member function 'bool DateTime::validate_data(int, int, int, int, int, int)':
datetime.cpp:31:49: error: expected primary-expression before '>' token
datetime.cpp:35:1: warning: control reaches end of non-void function [-Wreturn-type]
datetime.h:21:27: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
In file included from sample_DT_app.cpp:3:0:
datetime.h:21:27: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]


DateTime.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #ifndef DATETIME_H
#define DATETIME_H

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

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); 
    void exit(string);
    string EXIT_FAILURE = "Incompatible Data!";
    }; 
 
#endif 


DateTime.cpp
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
#include "datetime.h"


 
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 << "Works ok"; // strftime();
 }
 
 void DateTime::exit(string EXIT_FAILURE){
     cout << EXIT_FAILURE << endl; 
 }
 
 
 
 bool DateTime::validate_data(int y, int m, int d, int h = 0, int min = 0, int s = 0) {
    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;
    }
    DateTime::exit(EXIT_FAILURE);
}


Main.cpp
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
#include <iostream>
#include "datetime.h"

using namespace std;

int main(){
    DateTime DT1(2015, 1, 2, 3, 4, 5);
    DateTime DT2(2015, 1, 2, 3, 4);
    DateTime DT3(2015, 1, 2, 3);
    DateTime DT4(2015, 1, 2);
    

       
    DT1.display();
    cout << endl;
    DT2.display();
    cout << endl;
    DT3.display();
    cout << endl;
    DT4.display();
    cout << endl;
    
    return 0;
}


I'm really getting nowhere so would appreciate any help.
Many thanks,
Sarah
Change m => 12 to m >= 12.


You probably should choose a different name for your string variable because EXIT_FAILURE is the name of a standard macro defined in <cstdlib> so if this header happens to be included in your code it will break.

http://en.cppreference.com/w/cpp/utility/program/EXIT_status
Last edited on
Hi,

Line 31 in DateTime.cpp

If any one of those conditions is true, the checking of the rest of the conditions is abandoned. This is called short circuiting, and is logically correct.

I would test each of them separately with an if, else if, else if .... else chain. return false; if any of the conditions are false. Make sure that something is returned for all cases, that is why you have a warning at the moment.

Months > 12 don't make sense, similar for the others. Your conditions need to compare both low and high allowed values there, in one condition.

Also compile with at least c++11, preferable c++14.
Last edited on
Peter87- we're required to use exit(EXIT_FAILURE) in our code but I've been unable to figure out how I use it correctly so thought it was just the name of the variable I need *facepalm* Guess I'm off to learn more about that
- so I've changed it to
1
2
3
    return false;
    cout << "Incompatible data!" << endl;
    exit(EXIT_FAILURE);;


TheIdeasMan- I'll change my if to a chain, that makes sense. I'm not sure what you mean by compare both high and low allowed values in one condition? (or are you meaning like this?
1
2
3
4
5
if ( y >= 1970 && y <= 2020){
        return true;
    } else if (m >= 1 && m <= 12){
        return true;
etc etc

I'm also not sure what it means by compiling with c++11 or c++14. I'm using the program which we have been told to use so I don't think I can change that...

Thanks so much for your help.
Last edited on
I'm not sure what you mean by compare both high and low allowed values in one condition?


1
2
3
else if (month >= 1 && month <=12) {
    return true;
}
OK so I've been able to get it working for the most part, but I don't understand what I'm doing wrong with the validate_data function, it's not exiting the program when there's an incorrect input.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 bool DateTime::validate_data(int y, int m, int d, int h, int min, int s) {
    if ( y >= 1970 && y <= 2020){
        return true;
    } else if (m >= 1 && m <= 12){
        return true;
    } else if (d >= 1 && d <= 31){
        return true;
    } else if (h >= 0 && h <= 23){
        return true;
    } else if (min >= 0 && min <= 59){
        return true;
    } else if (s >= 0 && s <= 59){
        return true;
    } else
    //return false;
    cout << "Incompatible data!" << endl;
    exit(EXIT_FAILURE);
}
When you are checking the timestamp data values, if any of them are valid, the function returns true. So, if the year is valid, you return true in line 3, and nothing else gets checked.

Instead, you only want to return true if all values are true. So, instead of chaining else if statements, you need to nest the if statements.

What you've written is essentially a large "OR" statement for the validity of all of the fields. You really need a large "AND" statement.
Line 2 will return true when y is correct no matter if the other values or wrong or not.

You only check m if y is already incorrect and so on.


Line 32 in the original DateTime.cpp would return the correct value if you replace || with &&.
Last edited on
Thanks so much everyone for your help and advice! I've got it working, I definitely couldn't have done it without your help.
Topic archived. No new replies allowed.