Do-While Statement Not Ending

Hello, I am new to programming and am trying to create the game Blackjack from scratch. So far, it works correctly, except when I try to leave the do-while by typing 'n', statement, it continues looping. I want the program to leave the do-while statement so that it can exit.

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 <ctime>
#include <iostream>
using namespace std;

int humanCards[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int computerCards[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int counter;
bool keepDrawing = true;



void DrawCard() // 0 is human, 1 is computer
{
	char yesOrNo;

	cout << "Draw a card? (y/n) ";
	cin >> yesOrNo;

	switch (yesOrNo)
	{
		case 'y':
			cout << "You drew a " << humanCards[counter] << ".\n";	
			break;
		case 'n':
			keepDrawing == false;
			break;
		default:
			cout << "Invalid input. Try again.\n";
	}
}

void DetermineCards() // Predetermine 12 card numbers for each player
{
	srand(time(0));

	for (counter = 0; counter < 12; counter++)
		{
		humanCards[counter] = rand() % 14;
		computerCards[counter] = rand() % 14;
		}
}

int main()
{
	DetermineCards();

	cout << "Let's play Blackjack!\n";

	do { DrawCard(); counter++; } while (keepDrawing == true);

	system("pause>nul");
	return 0;
}


I realize that this is not an entire blackjack game, this is only the very beginning of it. Any comments or help would be appreciated. Thanks!
1
2
3
case 'n':
     keepDrawing == false;
     break;


Here, you're doing a check for equality as opposed to an assignment.

Another thing to note is that counter is not reset to zero after DetermineCards(). This will cause an array out-of-bounds on the first draw. counter is also incrementing without bound in the loop...

At this stage I can see why you'd want the two arrays to be global (although you can pass them into DetermineCards() easily enough). However, I would recommend reworking your code such that counter and keepDrawing are not global. For instance, within DetermineCards() that counter can just be local, and you could have a local counter in main(). You might as well scrap DrawCard() and put that code in main if all it's going to do is select the next card (that would make it so you don't have to pass in keepDrawing, by reference and counter, by value into DrawCard().) You should also be sure not to increment counter if the user did invalid input.
Last edited on
Thanks for the quick response, shacktar, that solved my problem!
Topic archived. No new replies allowed.