Making A Function/Loops

I'm completely new to C++ and doing my best to learn as I go along, I am currently working on a programme to score a blackjack hand and so far so good I think, I just need to loop the program back around to pick the next card.

Here is my code so far with an indication of where I would like it to loop back around to:


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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <ctime>

using namespace std;

int main()


{
	srand(time(NULL));
	
	char stickortwist, playAgain, dealtCard[13][10] = {"ace", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king"};
	int score, suitSelector, cardSelector;

	score = 0;


	cout << "Welcome to BLACKJACK!\n\n\n";
	cout << "Press 'T' to recieve you first card!\n\n";
	cin >> stickortwist;

	//Loop back to here.
	{
		if (stickortwist == 'T' || 't')
			cout << "Your card is the ";
	
		cardSelector = (rand() % 13);
	
			cout << dealtCard[cardSelector];
			cout << " of ";

		suitSelector = (rand() % 4);

		{
			if (suitSelector == 0)
				cout << "spades";

			else if (suitSelector == 1)
				cout << "hearts";

			else if (suitSelector == 2)
				cout << "diamonds";

			else if (suitSelector == 3)
				cout << "clubs";
		}
	
	if (cardSelector == 1)
		score = score + 2;

	else if (cardSelector == 2)
		score = score + 3;

	else if (cardSelector == 3)
		score = score + 4;

	else if (cardSelector == 4)
		score = score + 5;

	else if (cardSelector == 5)
		score = score + 6;

	else if (cardSelector == 6)
		score = score + 7;

	else if (cardSelector == 7)
		score = score + 8;

	else if (cardSelector == 8)
		score = score + 9;

	else if ((cardSelector == 9) || (cardSelector == 10) || (cardSelector == 11) || (cardSelector == 12))
		score = score + 10;

	else if ((cardSelector == 0) && (score <= 10))
		score = score + 11;

	else if ((cardSelector == 0) && (score > 10))
		score = score + 1;

	cout << "\n\n\nYour current black jack score is " << score << "\n\n\n";
	
	{
		if (score > 21)
			cout << "You are bust!\n\n\n";
		
		else if (score == 21)
			cout << "CONGRATULATIONS YOU HAVE REACHED 21!\n\n\n";
	
		else if (score < 21);
			cout << "You have yet to reach 21";
	}
	
	if (score < 21);
	cout << "\n\n\n Would you like to stick (S) or Twist (T)?\n\n\n";
	cin >> stickortwist;
	
	{
		if ((stickortwist == 'T') || (stickortwist == 't'))
			return main(); // Loop back from this point for the start of the next card.
			
	}

	if ((score >= 21) || (stickortwist == 'S') || (stickortwist == 's'))
			cout << "The game has finished. Would you like to play again? (Y/N)";
			cin >> playAgain;

		{
			if (playAgain == 'Y' || playAgain == 'y')
				return main (); 
			else if (playAgain == 'N' ||playAgain == 'n')
				return 0;
		}
	}
}

At the moment I have it successfully looping back to the beginning of the program but this resets the score to 0. I think I need to make the main body of code into a function but I'm not entirely sure how to do that or how to initialise the loop. Any help would be greatly appreciated!
Last edited on
http://cplusplus.com/doc/tutorial/control/ Scroll down to either the while loop, or the do while loop
Yeah, throw in a variable that's altered when the desired condition is achieved and just have it as the while condition; something like:

int i = 0;
while (i == 0)
{
//Stuff
if (//condition to end//)
{i = 1;}
}
Here's how I usually do it:

1
2
3
while (1==1) {
	if (cin>>stickortwist) {
		//blah blah etc.}} 


I don't know I might be wrong though.
Got it working as a while loop, thanks for the help!
Topic archived. No new replies allowed.