Random Problem

Hopefully the last stupid question I post for this program. I hope I havent bugged any one yet.

Im having some mystery trouble with my RNG.

Do you see anything wrong with this section of code:
1
2
3
4
int deal;

    srand(time(0));
    deal = rand() % 52;



I am sure that code is valid. After spending 15 minutes trying to "fix" these 2 lines. I finally copied/pasted that code from a working program. Yet it still will not compile and the only errors I am receiving are related to that section of code.
1
2
3
4
14 Black Jack.cpp expected constructor, destructor, or type conversion before '(' token 
14 Black Jack.cpp expected `,' or `;' before '(' token 
15 Black Jack.cpp expected constructor, destructor, or type conversion before '=' token 
15 Black Jack.cpp expected `,' or `;' before '=' token 


I have check, rechecked, checked again, and checked once more the references. I have used the code in other programs. I am 85% certain it is correct. Why am I getting errors?

Here is my Main function BlackJack.Cpp:
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int i, j, k, l;
int intCard = 0;
int cardDelt;
int cardsDelt = 52;
int dealerCard;
int playerCard;
int deal;

    srand(time(0));
    deal = rand() % 52;


#include "cardClass.cc"
#include "setCardSuit.cc"
#include "setCardFace.cc"
#include "setCardValue.cc"

    Card deck[52];
    Card dealerHand[9];
    Card playerHand[9];
    void setDeck()
    {
        for (int i=0; i <52; i++)
        {
            intCard = i;
            deck[i].setCard();            
        }   
    
    }
#include "dealCards.cc"
#include "gamePlay.cc"
    
int main()
{
while (true)    
{
    if (cardsDelt > 40)
    {
        setDeck();
    }    
dealerCard = -1;
playerCard = -1;

gamePlay();
    
cin.get();   
}
}        

Its an uncommented mess right now, I know, sorry about that. Im still new to programming. Any extra advice you can offer me would be awesome.

Its almost 5am. Ive been working on this program more than 8hours. Its time for bed b4 I head to work at 9. Again I hope I am not pushing my limit on stupid questions. Thank you for any help you can offer.
srand(time(0));

Function call at global scope; impossible. Move those two lines to the beginning of main() and the problem should be solved.
Last edited on
Thank you Scipio. You solved me problem. I dont believe that was mentioned in any of my resources.

I am still a tad confused by C++'s behavior. Shouldnt I have to seed srand before calling a rand()?

1
2
3
4
5
6
7
8
9
#include "dealCards.cc" // calles rand()
#include "gamePlay.cc" // calls dealCards()
    
int main()
{
    srand(time(0));  // rand is seeded
    deal = rand() % 52;

    gamePlay(); // Deals/displays the cards 
Well, srand() is the seed. time(NULL) returns the number of seconds past since the first day of 1970 or something like that. By using that as argument for srand(), you seed the rand() function. This means that the seed is different everytime you run the program, unless you run the program more than once a second.
Topic archived. No new replies allowed.