Help! What's wrong here?

This is a discussion on Help! What's wrong here? within the C++ Programming forums, part of the General Programming Boards category; Hi friends. Sorry for my bad english. I want to make a code that compute date diff between two dates. ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1

    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.
    Attached Files Attached Files

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    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.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Man cin and cout are C++ functions... Please report to the right board.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,699
    cin and cout are not functions. They are objects.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21