? random date generator in class won't work ?

Sep 30, 2010 at 7:22pm
so i'm trying to implement a random generating date so that everytime it is called a new random date will pop up! but it seems like everytime i run it, my program just shows the presaved memory as the variable (which should be the random number) i will post my code below! any tips would be appreciated!

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
// date.h
#ifndef DATE_H
#define DATE_H

#include <ctime>
#include <cstdlib>


class Date
{

private:
	int day;
	int month;
	int year;

public:
    Date(){
       srand(time(NULL));


    }//end Date construct
    void getDate(int , int , int );

    void printDate();

};

#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//date.cpp
#include "date.h"
#include <iostream>


using namespace std;

void Date::getDate(int day, int month, int year)
{

         day = rand() % 30 + 1;
         month = rand() % 11 + 1;
         year = 1900 + rand() % 69 + 20;

}
void Date::printDate()
{
    cout << month << '/' << day << '/' << year << endl;
}

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

#include "address.h"
#include "date.h"




using namespace std;

int main(){


    

    Date d;
//    d.getDate();
    d.printDate();


    return 0;


please don't be afraid to shoot down my whole code...i'm quite a beginner :)

the number it keeps outputting as this simple date is this "100/400/2147348480" so obviously that's not the parameters that i wanted my randoms to be in lol
Last edited on Sep 30, 2010 at 8:07pm
Sep 30, 2010 at 8:31pm
You aren't calling getDate().

Also note that srand( time( NULL ) ); will generate the same sequence of random numbers if time(NULL) returns
the same value (it returns seconds, so you cannot run your program more than once per second).
Sep 30, 2010 at 8:41pm
it's giving me this wack error that says :error: no matching function for call to 'Date::getDate()' on the line 17 of the main...or the getDate call...???
Sep 30, 2010 at 8:49pm
nevermind! i figured it out! thanks jsmith! i appreciate the information on the psuedo random
Topic archived. No new replies allowed.