Cant get functions to work

I have 2 functions that I cant seem to get to work together. One sets up an aray of numbers and shuffles the numbers. The other one pulls the numbers a number in order of index each time its called. (basically the first time its called it returns the number in index 0. The second time its called it returns the number in index 1 and so on) I (with some help) got the functions to work when written as one function, however the assignment requires that they work as two separate functions, though they can work together, if that makes sense. Anyway the errors I keep getting are :

error C2109: subscript requires array or pointer type
IntelliSense: expression must be a pointer to a complete object type

These are both on line 37. I'm not sure what to do to fix it. I'm still very new to programing. Please help.

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
  #include "stdafx.h"
#include <iostream>
#include <cstdlib>       
#include <ctime>          


using std::cout; using std::endl;
using std::cin;



int deck();
int getcard();

int deck()     //sets up the deck and shuffles it
{
	int deck[52]={2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,
		10,10,10,10,11,11,11,11};
	int i,p,temp;

		srand(time(NULL));			// seed for random function

	for (p=0; p<520; p++){
		int k = rand()%52;
		int l = rand()%52;
			temp=deck[k];
			deck[k]=deck[l];
			deck[l]=temp;
		return(deck[i]);
}
}

	int getCardCalled=-1;       //deal function, incriments every time a card is drawn
	int getcard() {
		++getCardCalled;
		deck();
	return(deck[getCardCalled]);
}


	int main()	//main is for testing to see if the two functions work together			
{
			cout<<endl;cout<<endl;
	int i;
	
	for (i=0; i<52; i++){        //to print out shuffled deck
		cout<<deck()<<" "; 
}
			cout<<endl;cout<<endl;
		cout<<getcard();		//to see if it increments correctly
			cout<<endl;
		cout<<getcard();
			cout<<endl;
		cout<<getcard();
			cout<<endl;
		cout<<getcard();
			cout<<endl;cout<<endl;

		cout<<" Blackjack"<<endl;
			cout<<endl;cout<<endl;
}
The issue is that the array that you're referring to is a variable in the function deck. It is not available in getcard.
How would I define the array in getcard so that it can use the values in the array?
You need to define the array in function main() and then pass it as a parameter to the other functions which need it.
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
#include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout; using std::endl;
using std::cin; using std::time;
using std::srand; using std::rand;

void setup(int deck[]);

int getcard(int deck[]);

int main()
{
    int deck[52];    // define the deck here
    setup(deck);     // pass it as a parameter to the other functions

    cout << endl << endl;

    // Print out shuffled deck
    for (int i=0; i<52; i++)
    {
        cout << deck[i] << " ";
    }
    cout << endl;

}

void setup(int deck[] ) // sets up the deck and shuffles it
{
    // initialise the local array
    int cards[52] =
        {2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,
         10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11};

    // copy from local array to main deck
    for (int i=0; i<52; i++)
         deck[i] = cards[i];

    // display it (optional)
    for (int i=0; i<52; i++)
        cout << deck[i] << " ";
    cout << endl;

    srand(time(NULL));			// seed for random function

    // Shuffle the deck
    for (int p=0; p<520; p++)
    {
        int k = rand()%52;
        int l = rand()%52;
        int temp    = deck[k];
        deck[k] = deck[l];
        deck[l] = temp;
    }
}

Last edited on
Please don't spam the forum with multiple threads for the same question. This had already been answered in your previous thread at:

www.cplusplus.com/forum/beginner/112317/
Topic archived. No new replies allowed.