-
Help! What's wrong here?
Hi friends.
Sorry for my bad english.
I want to make a code that compute date diff between two dates. The code:
****************
Code:
#include "stdafx.h"
#include "JurosAtualizados.h"
#include <iostream>
#include <string.h>
#include <time.h>
using namespace std;
int main(array<System::String ^> ^args)
{
char dataDivida[10];
char dataPagamento[10];
char *dataDivida_dia;
char *dataDivida_mes;
char *dataDivida_ano;
char *dataPagamento_dia;
char *dataPagamento_mes;
char *dataPagamento_ano;
struct tm date1;
struct tm date2;
cout << "Data da divida: ";
cin >> dataDivida;
cout << "Data de pagamento: ";
cin >> dataPagamento;
memset(&date1, 0, sizeof(struct tm));
memset(&date2, 0, sizeof(struct tm));
dataDivida_ano = strcpy(&dataDivida[0], &dataDivida[1]);
dataDivida_mes = strcat(&dataDivida[3], &dataDivida[4]);
dataDivida_ano = strcat(strcat(strcat(&dataDivida[6], &dataDivida[7]), &dataDivida[ 8 ]), &dataDivida[9]);
date1.tm_mday = (int)dataDivida_dia;
date1.tm_mon = (int)dataDivida_mes - 1;
date1.tm_year = (int)dataDivida_ano;
dataPagamento_dia = strcpy(&dataPagamento_ano[0], &dataPagamento_ano[1]);
dataPagamento_mes = strcat(&dataPagamento_ano[3], &dataPagamento_ano[4]);
dataPagamento_ano = strcat(strcat(strcat(&dataPagamento_ano[6], &dataPagamento_ano[7]), &dataPagamento_ano[ 8 ]), &dataPagamento_ano[9]);
date2.tm_mday = (int)dataPagamento_dia;
date2.tm_mon = (int)dataPagamento_mes - 1;
date2.tm_year = (int)dataPagamento_ano;
time_t time1, time2;
time1 = mktime(&date1);
time2 = mktime(&date2);
double dSecs = difftime(time2, time1);
double dDays = 3600.0 / (3600.0 * 24);
cout << "Date diff (in days): " << dDays << endl;
system("pause");
return 0;
}
*************
But i'm getting an error at runtime. The program crashes at line
dataDivida_ano = strcpy(&dataDivida[0], &dataDivida[1]);
Ah, BTW, why is necessary to make a memset before the strcpy/strcat operations?
memset(&date1, 0, sizeof(struct tm));
Thx in advance.
Marco.
-
Why are you copying a string to itself? I'm note sure if strcpy() will handle strings where the memory overlaps, but in any case, why not just start at the second character, and ignore the first? That is, dataDivida + 1.
The language is a barrier to me, sorry. I'm not understanding what you're trying to do to the strings. And your (int) casts probably won't work as you expect them.
The memset()s are not needed before a strcpy() - they don't affect them. They're good though, as they zero the structure, but the same can be accomplished by initializing your varaibles:
struct tm date1 = {0};
...
Finally, what do you expect this to do?
Code:
double dDays = 3600.0 / (3600.0 * 24);
You could have just set dDays to 1.0 / 24.0, but I suspect one of those 3600.0s should be a variable. Also, even if your program did not crash, you'd get the same output everytime, regardless of the input:
Code:
cout << "Date diff (in days): " << dDays << endl;
as dDays is constant.
-
Man cin and cout are C++ functions... Please report to the right board.
-
cin and cout are not functions. They are objects.