conversion from 'char*' to non-scalar type 'Date' requested

Hello. I am trying to create a segment of code that will tell me the days between current local time and given time inputted by the user. My code is here below but I am struggling to convert the local time into the formula to calculate days between.

Error message is because my Date dt1, is not correctly wrote in date form (DAY, MONTH, YEAR)


I am new to this and lost, all help is appreciated thank you.

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
 #include <iostream>
#include <ctime>
using namespace std;

struct Date 
{ 
    int d, m, y; 
}; 
  
const int monthDays[12] = {31, 28, 31, 30, 31, 30, 
                           31, 31, 30, 31, 30, 31}; 
  
int countLeapYears(Date d) 
{ 
    int years = d.y; 
  
    if (d.m <= 2) 
        years--; 
  
    return years / 4 - years / 100 + years / 400; 
} 

  int getDifference(Date dt1, Date dt2) 
{ 
  
    long int n1 = dt1.y*365 + dt1.d; 
  
    for (int i=0; i<dt1.m - 1; i++) 
        n1 += monthDays[i]; 
  
    n1 += countLeapYears(dt1); 
  
  
    long int n2 = dt2.y*365 + dt2.d; 
    for (int i=0; i<dt2.m - 1; i++) 
        n2 += monthDays[i]; 
    n2 += countLeapYears(dt2); 
  
    return (n2 - n1); 
} 

int main( )

 {
time_t ttime = time(0);
char* dt = ctime(&ttime);

cout << "The current local date and time is: " << dt << endl;


 
  
    Date dt1 = ctime(&ttime); 
    Date dt2 = {31, 2, 2020}; 
  
    cout << "You have " << getDifference(dt1, dt2) << " days remaining" << endl;
  
 if (getDifference(dt1, dt2) < 100)
 
    cout << "limited time remaining" ;
 
  
  
  
    return 0; 
}
Last edited on
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
#include <iostream>
#include <sstream>
#include <ctime>
using namespace std;

struct Date { int d, m, y; };

const int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int countLeapYears(Date d) {
    int years = d.y;
    if (d.m <= 2) --years;
    return years / 4 - years / 100 + years / 400; 
} 

int getDifference(Date dt1, Date dt2) {
    long int n1 = dt1.y * 365 + dt1.d;
    for (int i = 0; i < dt1.m - 1; ++i) n1 += monthDays[i];
    n1 += countLeapYears(dt1);
    long int n2 = dt2.y * 365 + dt2.d;
    for (int i = 0; i < dt2.m - 1; ++i) n2 += monthDays[i];
    n2 += countLeapYears(dt2);
    return n2 - n1;
}

int to_month_num(string& mon) {
    const char *months[]
    {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
    for (size_t i = 0; i < sizeof months / sizeof months[0]; ++i)
        if (mon == months[i]) return i + 1;
    return 0;
}

int main() {
    time_t ttime = time(0);
    const char* dt = ctime(&ttime);
    cout << "The current local date and time is: " << dt << endl;

    string month, temp;
    int d, m, y;
    istringstream iss(dt);
    iss >> temp >> month >> d >> temp >> y;
    m = to_month_num(month);

    Date dt1 = {d, m, y};
    Date dt2 = {31, 2, 2020};
    cout << "You have " << getDifference(dt1, dt2) << " days remaining\n";
}

> Error message is because my Date dt1, is not correctly wrote in date form (DAY, MONTH, YEAR)
you went for gas for your motorcycle and end up buying peanuts
now you'll have to make biodiesel

perhaps you should use localtime() or gmtime() instead of ctime()
Thank you very much! I suppose creating a biodiesel would help prevent global warming anyway... LOL.

Topic archived. No new replies allowed.