can't make function work

I'm supposed to make a simple game of 21 (blackjack but without the suits) program using an array for the deck. I have figured out how to get the deck to shuffle. I verified this with a couple of cout statements that showed me the original deck and the shuffled deck. Now Im trying to use a "get card" function to pull the first card of the deck the first time its called and the second card the second time, and so on. Unfortunately I cant seem to set the function up correctly so that it will even compile. The errors I keep getting are:

error C2664: 'getcard' : cannot convert parameter 1 from 'int' to 'int []' Line 44 Column 1
IntelliSense: argument of type "int" is incompatible with parameter of type "int *" Line 44 Column 15

I'm sure that I'm missing something simple but I cant seem to figure it out. Would appreciate any advice.



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
  // deck2.0.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cstdlib>        // needed for rand() and srand()
#include <ctime>          // needed for time()
using std::cout; using std::endl;

int getcard(int deck []) {
	int m;
	for (m=0; m<=52; m++)
	return deck[m];
}
	


int main()
{
	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};					// An array of 52 integers. deck 52 cards
  
  int i,n,temp,x;						// variable used as array INDEX, to select which array element.

  srand(time(NULL));			// seed for random function
								// set each of the 52 cards to a random value
  
  // print all 52 cards
  for (i=0; i<52; i++){ 
    cout<<deck[i]<<" "; 
  }
	cout<<endl;cout<<endl;

for (n=0; n<1040; n++){
	int k = rand()%52;
	int l = rand()%52;
		temp=deck[k];
		deck[k]=deck[l];
		deck[l]=temp;
}	
for (i=0; i<52; i++){ 
    cout<<deck[i]<<" "; 
}
cout<<endl;cout<<endl;
cout<<getcard(x);

}



	

	
Last edited on
what is the get card function supposed to do that cant be accomplished by simply calling deck[x]?

*edit* wait i think i see what you are trying to do. you want it to return the second element in the array the second time the function is called and the third element in the array the third time the function is called ect... ?

do i have that right?
Last edited on
yes! Exactly. Right now I cant seem to get it to call the first element. THe idea is that the "dealer" is dealing from the top of the deck. Any idea on what im doing wrong.
Last edited on
here ya go. if i sat around and thought about it for a while i could probably think of a better solution than global variables. but never the less this is a solution.

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
#include "stdafx.h"
#include <iostream>
#include <cstdlib>        // needed for rand() and srand()
#include <ctime>          // needed for time()
using std::cout; using std::endl;

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 timesGetCardCalled=-1;
int getcard() {
	++timesGetCardCalled;
	return deck[timesGetCardCalled];
}

int main()
{

  int i,n,temp,x;						// variable used as array INDEX, to select which array element.

  srand(time(NULL));			// seed for random function
								// set each of the 52 cards to a random value
  
  // print all 52 cards
  for (i=0; i<52; i++){ 
    cout<<deck[i]<<" "; 
  }
	cout<<endl;cout<<endl;

for (n=0; n<1040; n++){
	int k = rand()%52;
	int l = rand()%52;
		temp=deck[k];
		deck[k]=deck[l];
		deck[l]=temp;
}	
for (i=0; i<52; i++){ 
    cout<<deck[i]<<" "; 
}
cout<<endl;cout<<endl;
cout<<getcard();

}


you dont need to pass the function anything, just call it with no arguments.
Last edited on
Thank you! That did the trick!
Topic archived. No new replies allowed.