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.