How to assign a random value to two arrays

I am attempting to assign two random integers to two arrays. I will leave the entire code here, but i left what is specifically in question at the top... I know that entering the entire code is needless and excessive, but im not sure if I made a mistake somewhere in the middle of the code that is contributing the the error in the for loop.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
for (int i = 0; i < 2; i++) {
dc[i] = srand(time(0))%14;
pc[i] = srand(time(0))%14;
}



#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;

double balance = 0;
double input = 0;
char a;







int main() {
	cout << "Welcome to the Black Jack Table" << endl;
	while (1) {
		int dealer = 0;
		int player = 0;
		int pc[2] = { 0,0 };
		int dc[2] = { 0,0 };
		if (balance == 0) {
			cout << "You currently have no balance, please enter cash now if you would like to play black jack" << endl;
			cin >> input;
			balance = input + balance;
			cout << "Balance: " << balance << endl;
			input = 0;
				
		}
		else if (balance <= 0) {
			cout << "Your balance is currently negative, please enter more cash or we will have to call security: " << endl;;
			cout << "Balance: " << balance << endl;;
			cin >> input;
			balance = input + balance;
			input = 0;

		}
		else{
			system("CLS");
			cout << "Balance: " << balance << endl;;
			cout << "Enter (1) to play Black Jack, (2) to deposit more money, (3) to withdraw money from your balance, (q) to leave the table and withdraw all money" << endl;
			cin >> a;
			switch (a) {
			case '2':{
				cout << "Enter the amount you would like to deposit" << endl;
				cin >> input;
				balance = input + balance;
				input = 0;
				cout << "Balance: " << balance << endl;
				}
			case '3': {
				cout << "Enter the amount you would like to withdraw" << endl;
				cin >> input;
				balance = balance - input;
				input = 0;
				cout << "Balance: " << balance << endl;
			}
			case 'q': {
				cout << "You leave here with a balance of : " << balance << endl;
				balance = balance - balance;
				break;

			}
			case '1': {
				system("CLS");
				for (int i = 0; i < 2; i++) {
					dc[i] = srand(time(0))%14;
					pc[i] = srand(time(0))%14;
				}
				
			}
			}
		}
	}

}


Last edited on
srand(time(0)) sets the seed for random number generation, while rand() is the function that actually returns a random value.
Line 76-77: You're using srand() incorrectly. srand() is a void function (it doesn't return anything), so you can't assign it to anything. srand() should be called ONCE at the beginning of main() to initialize the random number generator. Lines 76-77, you want to use rand().
http://www.cplusplus.com/reference/cstdlib/srand/
http://www.cplusplus.com/reference/cstdlib/rand/
Topic archived. No new replies allowed.