Thread: Problem in Returning a string from a function

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    India
    Posts
    53

    Problem in Returning a string from a function

    Hi folks, This is my code. I'm generating random date [MMDDYYYY]. while returning date from function , i could not get correct output. How can i solve this?
    Code:
    unsigned int randr(unsigned int min, unsigned int max) 
    { 
           double scaled = (double)rand()/RAND_MAX; 
     
           return (max - min +1)*scaled + min; 
    } 
     
    char *generaterandomdate() 
    { 
        char *rdate; 
        char date[8]; 
        char d[2],y[4]; 
        unsigned int mon,day,year; 
        rdate=(char *)malloc(8); 
        FEB: 
        mon=randr(1,12); 
        day=randr(1,31); 
        if(mon==2 && day > 28) 
            goto FEB; 
        year=randr(1995,2015); 
        if(mon<10) 
        { 
            sprintf(d,"%d",mon); 
            date[0]='0'; 
            date[1]=d[0]; 
        } 
        else 
            sprintf(date,"%d",mon);
        date[2]='\0'; 
        if(day<10) 
        { 
            sprintf(d,"%d",day); 
            date[2]='0'; 
            date[3]=d[0]; 
        } 
        else 
        { 
            sprintf(d,"%d",day); 
            date[2]='\0'; 
            strcat(date,d); 
        }
        date[4]='\0'; 
        sprintf(y,"%d",year); 
        strcat(date,y); 
        date[8]='\0'; 
        rdate=date; 
        return rdate; 
     
    } 
     
    void main() 
    { 
        char date[9]; 
        int i; 
        for(i=0;i<5;i++) 
        { 
            printf("%s\n",generaterandomdate()); 
        } 
         
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well first of all you only allocated 8 bytes to rdate ... then you added a \0 at rdate[8], which isn't yours to use.
    When you create an array[8] ... the valid indices are 0 to 7 inclusive... 8 is out of bounds.

    Second you're using goto... just don't do it... *expecially* not to jump backwards.

    Third you are reallocating the rdate string every time you enter the generate random date, which you are calling 5 times... without freeing the memory you've used... This is called a memory leak... and it ain't good.

    Finally sprintf() and strcat() correctly null terminate strings, you don't need to do it for them... especially outside of array bounds.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    India
    Posts
    53
    Quote Originally Posted by CommonTater View Post

    Finally sprintf() and strcat() correctly null terminate strings, you don't need to do it for them... especially outside of array bounds.
    1. Then how to convert integer to String ? Which function should i use ?

    2. So in my program how to use concatenation ?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Instead of:
    Code:
        if(mon<10)
        {
            sprintf(d,"%d",mon);
            date[0]='0';
            date[1]=d[0];
        }
        else
            sprintf(date,"%d",mon);
    Just do:
    Code:
    sprintf(date, "%02d", mon);
    If you understand what you're doing, you're not learning anything.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Instead of:
    Code:
        if(mon<10)
        {
            sprintf(d,"%d",mon);
            date[0]='0';
            date[1]=d[0];
        }
        else
            sprintf(date,"%d",mon);
    Just do:
    Code:
    sprintf(date, "%02d", mon);
    In fact the whole thing could just be simplified to:
    Code:
    char *rdate = malloc(9);
    int mon = randr(1, 12);
    int year = randr(1995, 2015);
    int day = randr(1, mon == 2 ? 28 : 31);  // note that this doesn't account for 30-day months or leap years
    sprintf(rdate, "%02d%02d%04d", mon, day, year);
    return rdate;
    A better way for the day might be:
    Code:
    int days_per_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int day = randr(1, days_per_month[mon - 1]);
    ... which still doesn't account for leap years, but at least you won't end up with April 31st.
    Last edited by itsme86; 10-12-2011 at 09:23 AM.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Code:
        FEB:
        mon=randr(1,12);
        day=randr(1,31);
        if(mon==2 && day > 28)
            goto FEB;
    Like CommonTater said, don't use goto statements. You can easily use a while loop right here.

    Code:
    while ((mon == 2) && (day > 28)) {
        mon = randr(1, 12);
        day = randr(1, 31);
    }
    This link should help: For, While, and Do While Loops in C++ - Cprogramming.com . Also, as long as you're writing application software, it's int main(void), not void main(). Place a return 0; at the end of main() to note that the program completed successfully.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by infantheartlyje View Post
    1. Then how to convert integer to String ? Which function should i use ?

    2. So in my program how to use concatenation ?
    By making the array bigger...

  8. #8
    Registered User
    Join Date
    Oct 2011
    Location
    India
    Posts
    53
    Thank You all ! Very nice explanations and ideas ! Here i some what learned about sprintf, how to avoid goto statement and How to use arrays in good way. Once again Thank you for you all.
    Last edited by infantheartlyje; 10-12-2011 at 11:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 09-06-2011, 02:59 PM
  2. *Problem Returning A String Function* Please Help
    By impact in forum C++ Programming
    Replies: 8
    Last Post: 05-14-2007, 11:46 PM
  3. Returning a string from a function
    By tompagenet in forum C++ Programming
    Replies: 10
    Last Post: 11-12-2003, 06:39 PM
  4. returning a string from a function
    By revelation437 in forum C Programming
    Replies: 6
    Last Post: 12-14-2002, 04:21 AM
  5. returning a string from a function
    By itld in forum Linux Programming
    Replies: 5
    Last Post: 12-03-2001, 01:35 AM

Tags for this Thread