Printing bug

Hi, I'm a beginner at C++ and currently, I'm working on a Texas Hold Em game. My problem with my code (which, btw is not complete; I also only posted part of the code) is that the line cout << "Bet, Call, or Fold?" << endl; is being printed twice. I assume this has something to do with the fact that it's in a while loop, but I don't understand exactly why it's being printed twice. Also, interestingly, it only prints twice when the player decides the bet on the first turn. Any ideas on how to make it print only once? Thanks!(P.S. The code I posted below is not the complete game; it's just the betting portion for the AI and player (which is not complete yet))

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
//betting arrays & variables

	string betting[3] = { "bet", "check", "fold" };
	string playerInput;
	string playerInput2;
	string playerInput3;
	string playerInput4;
	string flopInput;
	string turnInput;
	string riverInput;

	double betAmount;
	double aiBetAmount;

	//To implement:
	//If player bets more than AI, let AI fold.
	//Player fold option.

	//To stop:
	//AI betting same number over and over again.
	//Repeated phrase: "Bet, Call, or Fold?"

			cout << "Bet, Check, or Fold?" << endl;
			getline(cin, playerInput);

			//check
			if (playerInput == betting[1] || playerInput == "Check")
			{
				betAmount = 0;
			}

			//bet
			if (playerInput == betting[0] || playerInput == "Bet")
			{
				cout << "Please enter amount: " << endl;
				cin >> betAmount;
			}
			
			//AI has double
			if (aiHand[0].value == aiHand[1].value)
			{
				double j = ((double)(rand() % 27 + 24)) / (double)1000;
				aiBetAmount = j * p2Pool;
				cout << endl << "AI bets: " << aiBetAmount << endl << endl;
			}
			//AI has 2 of same suit
			else if (aiHand[0].suitValue == aiHand[1].suitValue)
			{
				double j = ((double)(rand() % 9 + 16)) / (double)1000;
				aiBetAmount = j * p2Pool;
				cout << endl << "AI bets: " << aiBetAmount << endl << endl;
			}
			//AI has double that are same suit
			else if (aiHand[0].value == aiHand[1].value && aiHand[0].suitValue == aiHand[1].suitValue)
			{
				double j = ((double)(rand() % 18 + 64)) / (double)1000;
				aiBetAmount = j * p2Pool;
				cout << endl << "AI bets: " << aiBetAmount << endl << endl;
			}
			//AI makes bluffs occasionally
			else
			{
				int i = rand() % 2;
				aiBetAmount = randPool[i];
				cout << endl << "AI bets: " << aiBetAmount << endl << endl;
			}
			//If AI bets more than player, prompt player to at least match bet.
			while (aiBetAmount > betAmount)
			{
				cout << "Bet, Call, or Fold?" << endl;
				getline(cin, playerInput);

				if (playerInput == betting[0] || playerInput == "Bet")
				{
					cout << "Please enter amount: " << endl;
					cin >> betAmount;
					cin.ignore(1000, '\n');
				}
				if (playerInput == "Call" || playerInput == "call")
				{
					betAmount = aiBetAmount;
					cout << "You call AI's bet of: " << aiBetAmount << endl;
				}
			}
			//If player bets more than AI, AI always calls.
			while (aiBetAmount < betAmount)
			{
				aiBetAmount = betAmount;
				cout << "AI calls your bet of: " << betAmount << endl;
			}

			cout << "You bet: " << betAmount << endl << endl;
			pot = betAmount + aiBetAmount;
			aiPool -= (pot / 2);
			p2Pool -= (pot / 2);
			cout << "The Pot: " << pot << endl << endl;
			cout << "AI's Pool: " << aiPool << endl;
			cout << "Your Pool: " << p2Pool << endl;
Last edited on
I think you should post code that we can run to reproduce the error.
I simulated only this part of the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int aiBetAmount = 100, betAmount = 10;
std::string playerInput;
while (aiBetAmount > betAmount)
{
	std::cout << "Bet, Call, or Fold?" << std::endl;
	std::getline(std::cin, playerInput);

	if (playerInput == "bet" || playerInput == "Bet")
	{
		std::cout << "Please enter amount: " << std::endl;
		std::cin >> betAmount;
		std::cin.ignore(1000, '\n');
	}
	if (playerInput == "Call" || playerInput == "call")
	{
		betAmount = aiBetAmount;
		std::cout << "You call AI's bet of: " << aiBetAmount << std::endl;
	}
}


And it seems to be working fine.
Last edited on
Ok here's the code up to the first betting round (the entire code is too long to post):
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include "card.h"

int main()
{
	srand(time(NULL));

	//starting pools
	double aiPool = 1000000;
	double p2Pool = 1000;
	double pot;
	int randPool[2] = { 0, rand() % 23 + 3 };

	card deckOfCards[52];

	for (int i = 0; i < 13; i++)
	{
		deckOfCards[i].value = i + 2;
		deckOfCards[i].suit = 'S';
		deckOfCards[i].suitValue = 4;
	}
	for (int i = 0; i < 13; i++)
	{
		deckOfCards[i + 13].value = i + 2;
		deckOfCards[i + 13].suit = 'H';
		deckOfCards[i + 13].suitValue = 3;
	}
	for (int i = 0; i < 13; i++)
	{
		deckOfCards[i + 26].value = i + 2;
		deckOfCards[i + 26].suit = 'C';
		deckOfCards[i + 26].suitValue = 2;
	}
	for (int i = 0; i < 13; i++)
	{
		deckOfCards[i + 39].value = i + 2;
		deckOfCards[i + 39].suit = 'D';
		deckOfCards[i + 39].suitValue = 1;
	}

	shuffle(deckOfCards, 52);

	card aiHand[2];
	card player2Hand[2];

	int topCard = 0;

	int aiSize = 0;
	int p2Size = 0;

	for (int i = 0; i < 2; i++)
	{
		aiHand[i] = deckOfCards[topCard];
		topCard++;
		aiSize++;

		player2Hand[i] = deckOfCards[topCard];
		topCard++;
		p2Size++;
	}

	cout << "AI's Hand" << endl;
	for (int i = 0; i < aiSize; i++)
	{
		cout << "[XX]";
	}
	cout << endl << endl << "Your Hand" << endl;
	for (int i = 0; i < p2Size; i++)
	{
		cout << player2Hand[i].displayCard();
	}
	cout << endl << endl;

	card flop[3];
	card turn[1];
	card river[1];

	int flopSize = 0;
	int turnSize = 0;
	int riverSize = 0;

	//betting arrays & variables

	string betting[3] = { "bet", "check", "fold" };
	string playerInput;
	string playerInput2;
	string playerInput3;
	string playerInput4;
	string flopInput;
	string turnInput;
	string riverInput;

	double betAmount;
	double aiBetAmount;

	//To implement:
	//If player bets more than AI, let AI fold.
	//Player fold option.

	//To stop:
	//AI betting same number over and over again.
	//Repeated phrase: "Bet, Call, or Fold?"

			cout << "Bet, Check, or Fold?" << endl;
			getline(cin, playerInput);

			//check
			if (playerInput == betting[1] || playerInput == "Check")
			{
				betAmount = 0;
			}

			//bet
			if (playerInput == betting[0] || playerInput == "Bet")
			{
				cout << "Please enter amount: " << endl;
				cin >> betAmount;
			}
			
			//AI has double
			if (aiHand[0].value == aiHand[1].value)
			{
				double j = ((double)(rand() % 27 + 24)) / (double)1000;
				aiBetAmount = j * p2Pool;
				cout << endl << "AI bets: " << aiBetAmount << endl << endl;
			}
			//AI has 2 of same suit
			else if (aiHand[0].suitValue == aiHand[1].suitValue)
			{
				double j = ((double)(rand() % 9 + 16)) / (double)1000;
				aiBetAmount = j * p2Pool;
				cout << endl << "AI bets: " << aiBetAmount << endl << endl;
			}
			//AI has double that are same suit
			else if (aiHand[0].value == aiHand[1].value && aiHand[0].suitValue == aiHand[1].suitValue)
			{
				double j = ((double)(rand() % 18 + 64)) / (double)1000;
				aiBetAmount = j * p2Pool;
				cout << endl << "AI bets: " << aiBetAmount << endl << endl;
			}
			//AI makes bluffs occasionally
			else
			{
				int i = rand() % 2;
				aiBetAmount = randPool[i];
				cout << endl << "AI bets: " << aiBetAmount << endl << endl;
			}
			//If AI bets more than player, prompt player to at least match bet.
			while (aiBetAmount > betAmount)
			{
				cout << "Bet, Call, or Fold?" << endl;
				getline(cin, playerInput);

				if (playerInput == betting[0] || playerInput == "Bet")
				{
					cout << "Please enter amount: " << endl;
					cin >> betAmount;
					cin.ignore(1000, '\n');
				}
				if (playerInput == "Call" || playerInput == "call")
				{
					betAmount = aiBetAmount;
					cout << "You call AI's bet of: " << aiBetAmount << endl;
				}
			}
			//If player bets more than AI, AI always calls.
			while (aiBetAmount < betAmount)
			{
				aiBetAmount = betAmount;
				cout << "AI calls your bet of: " << betAmount << endl;
			}

			cout << "You bet: " << betAmount << endl << endl;
			pot = betAmount + aiBetAmount;
			aiPool -= (pot / 2);
			p2Pool -= (pot / 2);
			cout << "The Pot: " << pot << endl << endl;
			cout << "AI's Pool: " << aiPool << endl;
			cout << "Your Pool: " << p2Pool << endl;

And here's the header file for it to run correctly:
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
#include <string>
#include <stdlib.h>
#include <time.h>

using namespace std;

class card
{
public:
	//variables
	int value;
	char suit;
	int suitValue;

	//constructors
	card()
	{
		value = 14;
		suit = 'S';
	}
	card(int v, char s, int i)
	{
		value = v;
		suit = s;
		suitValue = i;
	}

	//functions
	string displayCard()
	{
		string displayValue = "[";
		if (value < 11)
		{
			displayValue += to_string(value);
		}
		if (value == 11)
		{
			displayValue += "J";
		}
		if (value == 12)
		{
			displayValue += "Q";
		}
		if (value == 13)
		{
			displayValue += "K";
		}
		if (value == 14)
		{
			displayValue += "A";
		}

		displayValue += "-";
		displayValue += suit;
		displayValue += "]";

		return displayValue;
	}
};

void shuffle(card deck[], int len)
{
	srand(time(NULL));
	for (int i = 0; i < len; i++)
	{
		int r = rand() % (len - i) + i;

		card temp = deck[i];
		deck[i] = deck[r];
		deck[r] = temp;
	}
}
For clarification: the bug only occurs when the player bets an amount smaller than the AI's bet, so you may need to run the program a few times in order to reproduce the error since AI bets using certain rules. Again, thanks in advance! :) (And sorry for the triple reply xD)
I've only had a cursory look.

Lines 113 - 118
1
2
3
4
5
6
7
//bet (line 113)
if (playerInput == betting[0] || playerInput == "Bet")
{
    cout << "Please enter amount: " << endl;
    cin >> betAmount;
    std::cin.ignore( 1000, '\n' ) ; // *** add this *** (throw away the new line in the input buffer)
}


Even if that fixes this particular error, your code needs a fair amount of re-factoring. Try to locate a local C++ expert who is accessible to you, and request that person to do a lightweight code review of your finished code.

When learning something new, it helps to find people who already understand it. It is much easier to learn C++ if you know people nearby who can answer questions for you, and can help you solve problems. ...

It helps to make sure that the people you think are experts really know what they are talking about. Fortunately, there is an easy way to tell, which works most of the time: An expert is someone who not only understands the things that you are trying to master, but also can explain them to you. Anyone who can't answer your questions clearly is not an expert as far as you are concerned. There are people who can explain things clearly, and still be completely wrong, but fortunately, such people are rare. - Koening and Moo in "What do you do after you say Hello World?", chapter 32, 'Ruminations on C++'
Thanks, it works now! I was guessing that the solution would be to put cin.ignore(1000, '\n'); somewhere.
Topic archived. No new replies allowed.