Accessing a Void difficulty

Hello! I'm having trouble accessing a void that has variables inside of it. I would like to keep it similar to the other Voids (playOneHand()), but any advice would be very helpful!

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
#include "BlackJack.h"

void blackjack::BlackJack()
{
	cout << "Welcome to BlackJack!\n";

	playOneHand();

	cout << "Returning to Menu...";

	int frameWait = 5000;
	std::this_thread::sleep_for(std::chrono::milliseconds(frameWait));

	ClearScreen();
}

void blackjack::playOneHand()
{
	
}

void blackjack::initializeDeck(int deck[])
{
	
}

BlackJack.h
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
#ifndef _BLACKJACK_H_
#define _BLACKJACK_H_

#include <iostream>
#include <thread>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <limits>

using namespace std;

class blackjack
{
public:
	void BlackJack();

	void initializeDeck(int deck[]);
	
	void playOneHand();
	
	void ClearScreen()
	{
		int n;
		for (n = 0; n < 10; n++)
			printf("\n\n\n\n\n\n\n\n\n\n");
	}

};

#endif 

Game.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
54
55
56
57
58
59
60
61
62
63
#include "BlackJack.h"
#include "Hangman.h"
#include "HangmanEasy.h"
#include "HangmanHard.h"
#include "Snake.h"

using namespace std;

int main()
{
	bool MenuRepeat = true;
	int UserSelection;

	do
	{
		cout << endl;
		cout << "Welcome! Please pick one of the following options. \n1. Play BlackJack 
                               \n2. Play Hangman \n3. Play Snake \n4. Quit \nOption: ";
		cin >> UserSelection;
		cout << endl;

		switch (UserSelection)
		{
			
		case 1:
		{
			blackjack bl;
			bl.BlackJack();
			break;
		}

		case 2:
		{
			hangman hm;
			hm.Hangman();
			break;
		}

		case 3:
		{
			snake sk;
			sk.Snake();
			break;
		}

		case 4:
		{
			MenuRepeat = false;
			break;
		}

		default:
		{
			cout << "Incorrect Option. Please either play 
                                       one of the games or Quit." << endl;
			break;
		}

		}
	} while (MenuRepeat);

	return 0;
}
Last edited on
Could you re-phrase the question please and point out where you have a problem. Preferably without using the term 'void; I think you intend it as a synonym for 'function' which it is not.

You can't access variables declared inside functions from outside the function.

Either declare variables as pointers to new memory or declare variable outside the function and pass pointer to function.

variables inside the function are local to the function, and will die when function finishes execution.
Last edited on
The problem is that I don't know how to access the 'initializeDeck(int deck[])' from the 'playOneHand()' function. If I make int deck[] function separate from the initializeDeck, I would need to declare the size of the array, which I don't want to do.

Basically, what I'm saying is, is there a way to enter initializeDeck(int deck[]) after entering the playOneHand() function? (Which I can access since it doesn't have any variables inside of it).
Maybe make the deck a member of class blackjack, then you don't have to pass the deck as a parameter to initializeDeck() and you can call it right from playOneHand()
Because then I would have to call the size of the array, which I preferably wouldn't want to do. I mean, I'll look into it, but is there really a solution to this?
Don't get hung up on access to the array. Think of it this way, if you need access to the array, then you almost always need access to its size also.
Topic archived. No new replies allowed.