Hello doilin,
The reason your function sometimes does nothing is because of the way you are using rand():
rand() % x;
gives you a number between 0 and (x-1), inclusive - so when you call
1 2
|
mycard->rank = rand() % 14;
mycard->type = rand() % 5;
|
mycard.rank can evaluate 0 - 13 and
mycard.type can evaluate 0-4
All your if conditionals catch ranks of 1 - 14 and types from 1 - 4.
You will never get a king this way, and whenever a 0 gets generated
by either rand() statement, you will get no card - it just slips past all the if clauses uncaught.
Check the reference on this website (
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/) or run a little tester program that prints or counts the results of your rand() calls to see what is happening under the hood.
Hope this helps!
PS for ideas, maybe check out enum in c++, which could clean up your code in this program.