Hi I'm new to this.
I'm a mechanical engineering student and have to learn c++, i have this book and there's a program source code.
My question is: why do we need to include ctime in this code and what is it good for?

#include <iostream>
#include <cstdlib>
[B]#include <ctime>[/B]
#include <cmath>
using namespace std;

int rand_0toN1(int n);
void draw_a_card();

char *suits[4] = { "hearts", "diamonds", "spades", "clubs"};

char *ranks[13] = {"ace", "one", "two", "three", "four", "five", "six", "seven", "eight",
                     "nine", "ten", "jack", "queen", "king"};

int main()
{
  int n, i;

srand(time(NULL));

while (1) {
             cout << "Enter no. of cards to draw ";
cout << "(0 to exit): ";

cin >> n;

if (n == 0)
break;

for(i = 1; i <=n; i++)
draw_a_card();
}
return 0;
}

void draw_a_card() {

int r;

int s;

r = rand_0toN1(13);
s = rand_0toN1(14);

cout << ranks[r] << " of " << suits[s] << endl;
}


int rand_0toN1(int n) {
return rand () % n;
}

P.S I'm using Visual Studi 2010 (c++).
Thanks for any reply.

Recommended Answers

All 11 Replies

HI fobos.
Thanks for your answer first.

Yeah i saw that thread and red it very carefully.
But I'm new to c++ programming and do not understand the most part of those codes there.
That's why I asked what does #include <ctime> do. As much as I can see all of these source codes have something to do with time and clock but my problem( soucecode) has nothing to do with clock or time. (like i said i'm new to c++ and maybe it has but i can't figure it out how).
Thanks again for your reply.

From what i have learned in the past, #includes are like prebuilt function that you can use in your source code if you want to reference it. If you dont need it, then just take it out. Im just going off what you said:

My question is: why do we need to include ctime in this code and what is it good for?

I am sorry if there is a problem with your coding, because i will not be able to answer it. If there is a problem with your code, post what the problem is and what you are trying to achieve.

No, I don't have a problem with the code.It works just as it should. Our teacher gave us some examples and we shall explain what every part of the program does.
This program displays random cards from a deck of 52 cards. It asks the user for a number (i.e. if i give the number 2) of cards and then prints cards at random (i.e. seven of spades, ace of spades).
I guess #include <ctime> has something to do with: srand(time(NULL)); but I can't figure out the connection between them.

Anyway thhanks a lot.

>> I guess #include <ctime> has something to do with: srand(time(NULL)); but I can't figure out the connection between them.


See this link...

http://www.cplusplus.com/reference/clibrary/ctime/

Scroll down a little bit and you'll see there is a function called "time".

http://www.cplusplus.com/reference/clibrary/ctime/time/


You are using a function called "time" in your program. The linker / compiler needs to be able to find that function.

"#include <ctime>" tells the linker "look in the ctime library for functions." It does and finds the function "time" and from there it compiles and links. If you didn't add that "#include" function, it would not know to look there. Try commenting it out and see if it compiles (I don't know whether it will or not. If it does, then some of the other #include statements and / or the "using namespace std" told the compiler to look in ctime).

The #include acts as a paste function for the file it targets. So when you say #include <ctime> , the file ctime is located and it's contents are pasted directly in the place of the statement.
What that does is provide you with all of the declarations and code that is found in that file.
Suppose you use a function called time_t time (time_t * t) . You do not define that function in your source file. Instead it is provided by some library on your system. However, in order to ensure that you are calling the function properly and it indeed exists the compilation process needs to verify the signature of the function (what it returns, what parameters it takes). To do this, there needs to be a declaration. In the time.h file this declaration exists.

"#include <ctime>" tells the linker "look in the ctime library for functions."

#include does not tell the linker anything - it is an instruction for the preprocessor.

First thank you very much, all of you, especially to: VernonDozier and L7Sqr.
I get the general idea what #include <ctime> does.
If you could take a look at my code please and tell me what exactly the function: srand(time(NULL)) does.

Thank you again.

>> #include does not tell the linker anything - it is an instruction for the preprocessor.


You're right. Whoops.

commented: A mature and reasonable response that did not involve name-calling or pretending the past didn't happen, putting Vern into the top 5% of internet users :) +9

First thank you very much, all of you, especially to: VernonDozier and L7Sqr.
I get the general idea what #include <ctime> does.
If you could take a look at my code please and tell me what exactly the function: srand(time(NULL)) does.

Thank you again.

>> time(NULL) does this. See link above.

Get the current calendar time as a time_t object.

The function returns this value, and if the argument is not a null pointer, the value is also set to the object pointed by timer.

In other words, it returns the number of seconds since 1970, which is a little more than 1.3 billion, I believe. Round it off to 1.3 billion.

So srand is passed 1.3 billion as a parameter and srand seeds the random number generator with 1.3 billion.

http://www.cplusplus.com/reference/clibrary/cstdlib/srand/

The pseudo-random number generator is initialized using the argument passed as seed.

For every different seed value used in a call to srand, the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand.
Two different initializations with the same seed, instructs the pseudo-random generator to generate the same succession of results for the subsequent calls to rand in both cases.

If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to rand or srand.

In order to generate random-like numbers, srand is usually initialized to some distinctive value, like those related with the execution time. For example, the value returned by the function time (declared in header <ctime>) is different each second, which is distinctive enough for most randoming needs.

I think i got it now.Knowing that the computer can't generate a random number this is used to bring the randommising process as close as possible to the real world random.

Thank you.
Cheers.

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.