I need help with my C++ Program loop.

So I have this assignment and I am totally lost. I have coded just about everything. What the program does is plays a game of high-low, or Acey-Ducey. My professor wants it played this way. It asks for 2 card number, then assigns the cards in an if-else statement. The user starts out with $1000. Once the 2 cards are inputted, it prompts for a bet to be enter. (The bet has to be at least $100) then it displays the bet that was entered. Now let's say the user bets $200 and they lose, well it will output "You have $800 remaining" which it does, but once it loops over again, the player SHOULD start out with $800, but instead, they always start out with $1000 again. So once it loops, it resets the funds back to $1000, as it should keep it at their remaining funds. Please help! I have posted the code. There are no comments (I understand I should have comments, but I am way behind on this assignment and am trying to get it in before Sunday night) Feel free to manipulate the code. They are all included in functions. The current code works as well, but it does not have loops as I took them out because they do not work! Thanks!

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
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

// int main includes the playGame functions that plays the entire game.


int main () {
	int funds, bet, card1, card2, card3;
	void playGame(int& funds);
    	playGame(funds);
}

// playGame includes all of the functions necessary to play.

void playGame(int&  funds) {
    bool isBetween(int card1, int card2, int card3);
    string getCardFace(int cardNum);
    string getCardSuit(int cardNum);
    int getBet(int& funds, int& bet);
	int cardNum, bet;
        int card1, card2, card3;
	cout << "Enter card number 1: ";
        cin >> card1;
        cout << "Enter card number 2: ";
        cin >> card2;
        isBetween(card1, card2, card3);
        cout << "Your cards are " << getCardFace(card1) << " of ";
        cout << getCardSuit(card1) << " and " << getCardFace(card2);
        cout << " of " << getCardSuit(card2) << "." << endl;
	cout << "Enter bet: ";
	cin >> bet;
	getBet(funds, bet);
	srand (time(NULL));
	card3 = rand() % 51 + 0;
        cout << "Card number 3 is " << getCardFace(card3) << " of ";
        cout << getCardSuit(card3) << "." << endl;
	isBetween(card1, card2, card3);
	switch(isBetween(card1, card2, card3)) { 
	case 0 :
	cout << "You lose!" << endl;
	funds -= bet;
	cout << "You have $" << funds << " remaining." << endl;
	break;
	case 1:
	cout << "You win!" << endl;
	funds += bet;
	cout << "You have $" << funds << " remaining." << endl;
}
}

// getCardSuit gets the card suit to display.

string getCardSuit(int cardNum){
        int suit = cardNum / 13;
        if (suit == 0)
        return("Hearts");
        else if (suit == 1)
        return("Diamonds");
        else if (suit == 2)
        return("Spades");
        else if (suit == 3)
        return("Clubs");
}

// getCardFace will display and determine the card face.


string getCardFace(int cardNum){
        int face = cardNum % 13;
        if (face == 0)
            return("Ace");
        else if (face == 1)
            return ("Two");
        else if (face == 2)
            return ("Three");
        else if (face == 3)
            return ("Four");
        else if (face == 4)
            return ("Five");
        else if (face == 5)
            return ("Six");
        else if (face == 6)
            return ("Seven");
        else if (face == 7)
            return ("Eight");
        else if (face == 8)
            return ("Nine");
        else if (face == 9)
            return ("Ten");
        else if (face == 10)
            return ("Jack");
        else if (face == 11)
            return ("Queen");
        else if (face == 12)
            return ("King");
}

// This function tells whether the third card will be in between the first and second. If true, will display a 1, if false
// will display a 0.

bool isBetween(int card1, int card2, int card3){
        if ((card3 < card2) && (card3 > card1))
        return (true);
        else
        return (false);
}

// Get's the players bet and displays it.

int getBet(int& funds, int& bet){
	funds = 1000;
        if (bet < 100)
        do {
        cout << "Bet must be at least $100" << endl;
        cout << "Enter bet: ";
        cin >> bet;
        } while (bet < 100);
        else
        return (bet);
        cout << "Your bet is $" << bet << "." << endl;
}

Last edited on
I don't know where your loops were, but here are some thing to look at:

Line 11: What's the point of bet, card1, card2, card3 here? They're not used in main.

Line 114: Every time you call getBet, you reset funds to $1000.

Line 12: When you call playGame, you passing an uninitialized variable (funds). Hint: Initialzie funds to $1000 at line 10 and remove line 114.

Line 36: srand() does not belong here. srand() should be called ONCE at the beginning of main().

Line 40: What's the point of this call? The return value is ignored.

General comment: function prototypes should be at the top of the program. They should not be local within various functions.

Line 123: This line will never display if the bet is initially >= $100 (the return is executed).

Line 113-124: You're inconsistent with getBet(). You declare it as an int function, but you never use the return value. If the condition at line 115 is true, you will return 0 at line 124. The function works however, because you're passing bet by reference. You should choose one method or the other. Not both.
Last edited on
Thank you so much for the help! I finally got it all to work! You're a life saver! I do have one more issue. Every time I play the game, I lose. I have the third card selected randomly (card3 = rand() % 51 + 0;) I don't know if that is an error in my coding or not. I am using a bool function, so at some point I should win, but ever time I play, I lose. I am guessing that it may just be my computer. But I would think that it would select randomly instead of the computer always winning. Thanks again!
I don't know if that is an error in my coding or not.

If it's not doing what you expect, then it's an error in your coding. The computer is only doing what you tell it to.

A few observations about your program:
1) What numbers are you entering for card1 and card2? 0-12 or 0-51? if you're only entering 0-12, 75% of the time rand() if going to generate a number between 13 and 51, so you're going to lose.

2) Do you really want to take the suits into consideration when comparing card values? Comparing numbers between 0 and 51 will cause suits to rank higher than face values. i.e. An ace of diamonds (13) is higher than an ace of hearts (0).

3) Your isBetween() function assumes card1 and card2 are in ascending order. If not in ascending order, the function will not work as expected.

Here's a cleaned up version of getBet()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const int MINBET = 100;

// Get's the players bet and displays it.
int getBet (int & funds)
{   int bet;

    if (funds < MINBET)
    {   cout << "You don't have enough money to continue" << endl;
        return 0;       //  End game
    }
    do 
    {   cout << "Enter bet: ";
        cin >> bet; 
        if (bet == 0)
            return 0;   //  End game
        if (bet < MINBET)
        {   cout << "Bet must be at least $" << MINBET << endl;
        }
    } while (bet <  MINBET);
    cout << "Your bet is $" << bet << "." << endl;
    return bet;
}

Last edited on
The numbers the user will enter will be 0-51. Unfortunately, the only time I can win at the game and actually add to my betting money is when I enter 0 for card1, and 51 for card2, because the rand() function can only stay within those parameters and those 2 inputs are the highest and lowest values I can enter. Other than that, the highest card I input (whether it is card1 OR card2) the card3 rand() function always seems to place a card higher than the highest card I inputted. It never falls below it. I am not fully sure how to fix the isBetween function either. I know what you're saying when you say that an ace of diamonds is not the same as an ace of hearts, but I am not sure how to correct this. And I also don't understand the ascending order either.

(I also forgot to mention that I am still new at C++. This is my first semester in computer science and I barely have class time to learn the stuff, so I try to figure it out on my own and occasionally get stumped as my teacher has us submit larger assignments with less experience)

I will post my current code below. I am still having issues with it, and I need to fix it because I also have to include a way to shuffle the card deck with arrays. The main issue I am having is with the isBetween and playGame functions! Thanks for the help!


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
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;


void playGame(int& funds);
bool isBetween(int card1, int card2, int card3);
string getCardFace(int cardNum);
string getCardSuit(int cardNum);
int getBet(int& funds, int& bet);
 

// int main includes the playGame functions that plays the entire game.

const int MINBET = 100;

int main () {
	srand (time(NULL));
	int funds = 1000, bet;
	void playGame(int& funds);
	do {
    	playGame(funds);
	} while(funds > 0);
}

// playGame includes all of the functions necessary to play.

void playGame(int& funds) {
    bool isBetween(int card1, int card2, int card3);
    string getCardFace(int cardNum);
    string getCardSuit(int cardNum);
    int getBet(int& funds, int& bet);
	int cardNum, bet;
        int card1, card2, card3;
	card3 = rand() % 51 + 0;
	cout << "Enter card number 1: ";
        cin >> card1;
        cout << "Enter card number 2: ";
        cin >> card2;
        isBetween(card1, card2, card3);
        cout << "Your cards are " << getCardFace(card1) << " of ";
        cout << getCardSuit(card1) << " and " << getCardFace(card2);
        cout << " of " << getCardSuit(card2) << "." << endl;
	getBet(funds, bet);
        cout << "Card number 3 is " << getCardFace(card3) << " of ";
        cout << getCardSuit(card3) << "." << endl;
	switch(isBetween(card1, card2, card3)) { 
	case 0 :
	cout << "You lose!" << endl;
	funds -= bet;
	cout << "You have $" << funds << " remaining." << endl;
	if (funds <= 0)
	cout << "Game over!" << endl;
	break;
	case 1 :
	cout << "You win!" << endl;
	funds += bet;
	cout << "You have $" << funds << " remaining." << endl;
}
}

// getCardSuit gets the card suit to display.

string getCardSuit(int cardNum){
        int suit = cardNum / 13;
        if (suit == 0)
        return("Hearts");
        else if (suit == 1)
        return("Diamonds");
        else if (suit == 2)
        return("Spades");
        else if (suit == 3)
        return("Clubs");
}

// getCardFace will display and determine the card face.


string getCardFace(int cardNum){
        int face = cardNum % 13;
        if (face == 0)
            return("Ace");
        else if (face == 1)
            return ("Two");
        else if (face == 2)
            return ("Three");
        else if (face == 3)
            return ("Four");
        else if (face == 4)
            return ("Five");
        else if (face == 5)
            return ("Six");
        else if (face == 6)
            return ("Seven");
        else if (face == 7)
            return ("Eight");
        else if (face == 8)
            return ("Nine");
        else if (face == 9)
            return ("Ten");
        else if (face == 10)
            return ("Jack");
        else if (face == 11)
            return ("Queen");
        else if (face == 12)
            return ("King");
}

// This function tells whether the third card will be in between the first and second. If true, will display a 1, if false
// will display a 0.

bool isBetween(int card1, int card2, int card3){
        if ((card3 > card1) && (card3 < card2))
        return (true);
        else if ((card3 < card1) && (card3 > card2))
	return (false);
}

// Get's the players bet and displays it.

int getBet (int& funds, int& bet)
{

    if (funds < MINBET)
    {   cout << "You don't have enough money to continue" << endl;
        return 0;       //  End game
    }
    do 
    {   cout << "Enter bet: ";
        cin >> bet; 
        if (bet == 0)
            return 0;   //  End game
        if (bet < MINBET)
        {   cout << "Bet must be at least $" << MINBET << endl;
        }
    } while (bet <  MINBET);
    cout << "Your bet is $" << bet << "." << endl;
    return bet;
}

Topic archived. No new replies allowed.