hi all , i need to return the value of tempdate to the main function .... any idea how i can do that ???

#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
char* incrementdate(int &func_days_int,char *tempdate)
{
    
    
    char temp_dat[11],temp_mon[8],temp_year[5];
    int cur_dat,cur_mon,cur_year;
    
    
    temp_dat[0] = tempdate[0];
    temp_dat[1] = tempdate[1];
    
    temp_mon[0] = tempdate[3];
    temp_mon[1] = tempdate[4];
    
    temp_year[0] = tempdate[6];
    temp_year[1] = tempdate[7];
    temp_year[2] = tempdate[8];
    temp_year[3] = tempdate[9];  
    
    cur_dat = atoi(temp_dat);
    cur_mon = atoi(temp_mon);
    cur_year = atoi(temp_year);     
    
    cur_dat = cur_dat + func_days_int;
    
    if (cur_dat > 31 && (cur_mon == 1 || cur_mon == 3 || cur_mon == 5 || cur_mon == 7 || cur_mon == 8 || cur_mon == 10 ))
    {
       cur_mon = cur_mon + 1;
       cur_dat = cur_dat - 31;
    }
    else if (cur_dat > 30 && (cur_mon == 4 || cur_mon == 6 || cur_mon == 9 || cur_mon == 10))
    {
       cur_mon = cur_mon + 1;
       cur_dat = cur_dat - 30;
    }
    else if ((cur_dat > 29) && (cur_mon == 2) && ((cur_year%4) == 0))
    {
          cur_mon = cur_mon + 1;
          cur_dat = cur_dat - 29;
    }
    else if ((cur_dat > 28) && (cur_mon == 2) && ((cur_year%4) != 0))
    {
          cur_mon = cur_mon + 1;
          cur_dat = cur_dat - 28;
    }    
    else if ((cur_dat > 31) && (cur_mon == 12)) 
    {
          cur_mon = 1;
          cur_dat = cur_dat - 31;
          cur_year = cur_year + 1;
    }    
    
    
    sprintf(temp_dat,"%d",cur_dat);
    sprintf(temp_mon,"%d",cur_mon);
    sprintf(temp_year,"%d",cur_year);
    
    strcat(temp_mon,"-");
    strcat(temp_mon,temp_year);
    strcat(temp_dat,"-");
    strcat(temp_dat,temp_mon);

    tempdate = temp_dat;
    
    return strdup(tempdate);
    
    
}



int main()
{
    char delay[9]="05:01:00";
    char days[3];
    int days_int;
    days[0]=delay[0];
    days[1]=delay[1];
    days_int = atoi(days);
    char temporarydate[11]="27-12-2000";
    int dd,mm,yy;
    incrementdate(days_int,temporarydate);
    cout<<"the new date is :"<<temporarydate<<"\n";
}

i have no compiler errors , but the value still remains 27-12-2000 ,it it supposed to be 1-1-2001

First of all you need to decide if the parameter has to be passed by reference and hence changed or it has to be returned by the function and collected in main. Right now you're trying to do both but nothing is complete. The pointer is being passed by value and the return value of the function is not collected.

Its because you never changed the original value of template, only changed a local copy of the pointer. On line 69 you need to copy -- see strcpy() -- the value of temp_dat into template, not just assign the pointer. Line 71 is completly unnecessary and causes a memory leak. If you want the function to return something, just return template since its a pointer anyway.

im new to c++ , how do i collect the value returned by the function ??

In You are passing tempdate by value to incrementdate. So the statement tempdate = temp_dat; makes no effect to the tempdate variable of main. So tempdate of main still contain the original string "27-12-2000"

You have two options to rectify this:
1. Pass tempdate by reference:
Either char* incrementdate(int &func_days_int,char **tempdate) and modify it as *tempdate = temp_dat 2. Use the return value of incrementdate in main:

char *newdate = incrementdate(days_int,temporarydate);
free(newdate);

Note that you are using strdup to duplicate tempdate, which needs to be freed.

thank you that worked .. im just getting started with programming ... is thr any way for to me again update the value of temporarydate using the value stored at *newdate??

Well if you are getting into programming in c++ seriously, then you need to take a look at passing arguments by value and references and returning values from functions before you try anything else.

In You are passing tempdate by value to incrementdate. So the statement tempdate = temp_dat; makes no effect to the tempdate variable of main. So tempdate of main still contain the original string "27-12-2000"

You have two options to rectify this:
1. Pass tempdate by reference:
Either char* incrementdate(int &func_days_int,char **tempdate) and modify it as *tempdate = temp_dat 2. Use the return value of incrementdate in main:

char *newdate = incrementdate(days_int,temporarydate);
free(newdate);

Note that you are using strdup to duplicate tempdate, which needs to be freed.

Third option that I previously mentioned: strcpy(template, temp_dat); parameter template can't just simply be changed to ** because its declared as a character array in main(). So passing a character array by reference makes no sense at all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.