Can someone help me with this assignment please, I cant figure out why my loop is exiting early

Pages: 12
This is only my second quarter doing C++. The assignment is to steer the rat into one of three holes. The user has to enter their starting bank. So lets say they deposit 100$. Then the user places a wager. I subtract the wager from their bank then they guess the hole the cheese is in. If they guess correctly the wager is doubled if they guess incorrectly the wager is deducted from their total. They can play again until they reach 0$ in their bank or they can exit early.
My do While loop is exiting after the "do" block is executed. Ive tried changinging the while around and I just dont see whats happening. Any help would be awesome. Thank you.
Im using Visual Studio Enterprise 2019.

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

int main()
{
	int userChoice{};
	int in_handCash{};
	int bet_Amount{};
	int newTotal{};
	int threeHoles{};
        
        //Random hole generator
	srand(time(0));
	threeHoles = rand() % 3 + 1;
        
        //Main Do loop with introduction and asking user to enter their bank
	do 
	{
		cout << "Welcome to Steer the Rat!" << endl;
		cout << "-----------------------------------\n" << endl;
		cout << "Please Enter your buy-in Amount" << endl;
		cin >> in_handCash;
		cout << "Thank You, your total is: " << in_handCash << endl;
		cout << "\nPlease Enter Your Wager:" << endl;
		cin >> bet_Amount;
		
		newTotal = in_handCash - bet_Amount;
		cout << "You're new total is: " << newTotal << endl;
	} 
	while (bet_Amount >= 0 || bet_Amount <= in_handCash || in_handCash >= 0);
	
	cout << "Please choose the hole the cheese is in. 1,2, or 3" << endl;
	cin >> userChoice;
		if (userChoice < threeHoles || userChoice  > threeHoles)
		{
			cout << "Sorry you Lose! Bring More Money Next Time!!" << endl;
		}
		else
		{
			in_handCash = bet_Amount * 2;
			cout << "CONGRATS, Your New Total Is: " << in_handCash << endl;
		}
	return 0;
}
The part where your in_handCash becomes double your bet amount is after your loop (line 44). Is that what you are talking about when you say the loop is exiting early?

Your loop is from lines 22 to 33. Are you saying that the "Please choose the hole the cheese is in. 1,2, or 3" is printing sooner than you expect it to?

Are you ever seeing lines 40 or 45 printed? You should be seeing one of them printed.

userChoice < threeHoles || userChoice > threeHoles
Otherwise known as "(userChoice != threeHoles)".
Last edited on
The do should be where the while is.
While should be above return 0;

The way the do while works is it will "do" the block between do and while and at the end of the block, it will check the while(); statement, if false, it exits, but "while" the statement is true, it will go back to do and rerun the block.

Also, you should make sure newTotal isn't negative when the user enters $100 and $1000 and their new total is -$900.
Last edited on
Here i'll paste the code as it runs

Welcome to Steer the Rat!
-----------------------------------

Please Enter your buy-in Amount
100
Thank You, your total is: 100

Please Enter Your Wager:
10
You're new total is: 90
Game Over
Sorry you Lose! Bring More Money Next Time!!
No matter what I enter as the bank and the wager it exits. Isn't the while already above the return 0;? But ok i'll try switching around the while and the do
oh I see, while and do don't necessarily need to be switched here. The do just needs to be where the while is and i need to place while somewhere else above return 0; depending upon where i want to check the statement
This is still exiting the same way but i'll play with placing while in other spots.

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

int main()
{
	int userChoice{};
	int in_handCash{};
	int bet_Amount{};
	int newTotal{};
	int threeHoles{};

	srand(time(0));
	threeHoles = rand() % 3 + 1;

	cout << "Welcome to Steer the Rat!" << endl;
	cout << "-----------------------------------\n" << endl;
	while (bet_Amount >= 0 || bet_Amount <= in_handCash || in_handCash >= 0);
	{
		cout << "Please Enter your buy-in Amount" << endl;
		cin >> in_handCash;
		cout << "Thank You, your total is: " << in_handCash << endl;
		cout << "\nPlease Enter Your Wager:" << endl;
		cin >> bet_Amount;
		
		newTotal = in_handCash - bet_Amount;
		cout << "You're new total is: " << newTotal << endl;
	} 
	do 
	
	cout << "Please choose the hole the cheese is in. 1,2, or 3" << endl;
	cin >> userChoice;
		if (userChoice != threeHoles)
		{
			cout << "Sorry you Lose! Bring More Money Next Time!!" << endl;
		}
		else
		{
			in_handCash = bet_Amount * 2;
			cout << "CONGRATS, Your New Total Is: " << in_handCash << endl;
		}
	return 0;
}
Last edited on
The syntax in your last post now just doesn't make sense.
It's either
1
2
3
4
while (condition)
{
    // iteration
}

or
1
2
3
4
5
do
{
    // iteration
}
while (condition);

The only difference is that a "do-while" loop is always executed at least once.

If we go back to your first post, I think you just want to move lines 36 to 46 to be inside the loop. I guess you probably also want threeHoles = rand() % 3 + 1; to be in the loop as well, if you want a different random number each time.
Last edited on
I threw it all in the do while block and its still exiting after the user enters a wager. So is there something wrong with my while statement?

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

int main()
{
	int userChoice{};
	int in_handCash{};
	int bet_Amount{};
	int newTotal{};
	int threeHoles{};



	cout << "Welcome to Steer the Rat!" << endl;
	cout << "-----------------------------------\n" << endl;
	while (bet_Amount > 0 || bet_Amount <= in_handCash || in_handCash > 0);
	{
		srand(time(0));
		threeHoles = rand() % 3 + 1;
		cout << "Please Enter your buy-in Amount" << endl;
		cin >> in_handCash;
		cout << "Thank You, your total is: " << in_handCash << endl;
		cout << "\nPlease Enter Your Wager:" << endl;
		cin >> bet_Amount;
		
		newTotal = in_handCash - bet_Amount;
		cout << "You're new total is: " << newTotal << endl;
		cout << "Please choose the hole the cheese is in. 1,2, or 3" << endl;
		cin >> userChoice;
		if (userChoice != threeHoles)
		{
			cout << "Sorry you Lose! Bring More Money Next Time!!" << endl;
		}
		else
		{
			in_handCash = bet_Amount * 2;
			cout << "CONGRATS, Your New Total Is: " << in_handCash << endl;
		}
	
	} 
	do  
	
	return 0;
}
That doesn't even compile. Surely you get an error when you try to compile that?

See my previous post for valid do/do-while loop syntax.

PS: Don't put srand(time(0)); in the loop. Just call it once at the beginning of main. srand seeds rand, and it only needs to be seeded one time.
Last edited on
That definitely compiled. Yeah the guy told me to switch the do and while. So I did, but I have since changed it back because I think it just makes more sense in my head. But ok, i think i see what youre saying. it makes sense that part would also be in the block.
This is the output, It doesnt change no matter what I do.
Welcome to Steer the Rat!
-----------------------------------

Please Enter your buy-in Amount
100
Thank You, your total is: 100

Please Enter Your Wager:
10
You're new total is: 90
Game Over
Sorry you Lose! Bring More Money Next Time!!

So i added a cout above everything right underneath where i declare my variables, just to see what would happen and it didnt output it. All it did was that exact thing in my previous post.
Yeah, like I said, that doesn't compile.

You're probably running an older exe file.
Clean your project and try rebuild. Manually delete your exe files if need be.
Last edited on
So im guessing all these edits ive been doing have been giving me the same oiutput because no matter what ive been getting the same output. If anyone has any ideas, I'm currently googling it. Build "project name" is now greyed out for some reason
Here is what I get when I run your code. Exactly what one would expect given the code you gave us.

1
2
3
4
5
6
7
8
9
10
11
Please Enter your buy-in Amount
100
Thank You, your total is: 100

Please Enter Your Wager:
90
You're new total is: 10
Welcome to Steer the Rat!
-----------------------------------

Please Enter your buy-in Amount 



Also, I see this bug I told you about:
1
2
3
4
5
6
7
8
9
10
11
12
13
Please Enter your buy-in Amount
1
Thank You, your total is: 1

Please Enter Your Wager:
10
You're new total is: -9
Welcome to Steer the Rat!
-----------------------------------

Please Enter your buy-in Amount

 


Reread my earlier comment carefully. I mean put the while(); statement the line above return.

The code you gave us and the behavior you are giving us do not match one another. Are you compiling and running the correct file?
Last edited on
Sounds like you borked your project.

Make a new one from scratch, and copy the code into it.
Last edited on
These problems are notoriously demanding. Make very sure mine is doing the right thing because I've only checked it roughly. Just copy it if you like - but at your peril, especially because you will have learned nothing and possibly got yourself a lemon.

You'll notice especially that the condition for keeping going is very simple - no_cash => no-can_play

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
#include <cstdio>
#include <cstdlib>

#include <iomanip>
#include <ctime>
#include <iostream>

using namespace std;

int main()
{
    int userChoice{};
    int in_handCash{};
    int bet_Amount{};
    int threeHoles{};
    
    //Random hole generator
    srand(time(0));
    
    //Main Do loop with introduction and asking user to enter their bank
    
    cout << "Welcome to Steer the Rat!\n";
    cout << "-----------------------------------\n";
    cout << "Please enter your buy-in amount: ";
    cin >> in_handCash;
    cout << "Thank you, your total is: " << in_handCash << '\n';
    
    while(in_handCash > 0)
    {
        
        cout << " Please enter your wager: ";
        cin >> bet_Amount;
        
        threeHoles = rand() % 3 + 1;
        
        cout << "Please choose the hole the cheese is in. 1,2, or 3: ";
        cin >> userChoice;
        if (userChoice != threeHoles)
        {
            cout << "*** Sorry you lose!\n";
            in_handCash -= bet_Amount;
        }
        else
        {
            in_handCash += bet_Amount * 2;
            cout << "*** CONGRATS\n";
        }
        bet_Amount = 0;
        cout
        << "Your new total is: " << in_handCash
        << "\n-----------------------------------\n";
    }
    
    cout
    << "You haven't got any money left\n";
    
    return 0;
}


PS You probably need to have a line or two to stop accepting bets greater than cash_in_hand. Maybe another internal while loop for that?
Ok so I took the advice and I exited the projhect and started a new one, copy and pasted my old code. And I think i'm pretty close now. My output now looks like this:
Welcome to Steer the Rat!
-----------------------------------

Please Enter your buy-in Amount
100
Thank You, your total is: 100

Please Enter Your Wager:
10
You're new total is: 90
Please choose the hole the cheese is in. 1,2, or 3
2
CONGRATS, Your New Total Is: 20
Welcome to Steer the Rat!
-----------------------------------

Please Enter your buy-in Amount

I haven't made it this far with this yet so thank you guys very much, I've been stuck on this forever. What do you mean these problems are notoriously demanding? Its college level CS131. is the real programmer job world easier then this? Because ive been struggling a bit. Especially with this one. If they're notoriously demanding then hopefully i'm not the only one. I'll try and take it from there and clean it up. I think its going to be smooth sailing from here though. Thanks again!



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
#include <ctime>
#include <iostream>

using namespace std;

int main()
{
    int userChoice{};
    int in_handCash{};
    int bet_Amount{};
    int threeHoles{};
    
    //Random hole generator
    srand(time(0));
    
    //Main Do loop with introduction and asking user to enter their bank
    
    std::cout << "Welcome to Steer the Rat!\n";
    std::cout << "-----------------------------------\n";
    std::cout << "Please enter your buy-in amount: $";
    std::cin >> in_handCash;
    std::cout << "Thank you, your total is: $" << in_handCash << '\n';
    
    while
        (
              in_handCash > 0 &&
              std::cout << " Please enter wager: $" &&
              std::cin >> bet_Amount &&
              bet_Amount <= in_handCash
         )
        
        {
            threeHoles = rand() % 3 + 1;
            
            std::cout << "Please choose cheese-hole <1,2, or 3>: ";
            std::cin >> userChoice;
            if (userChoice != threeHoles)
            {
                std::cout << "*** Sorry you lose!\n";
                in_handCash -= bet_Amount;
            }
            else
            {
                in_handCash += bet_Amount * 2;
                std::cout << "*** CONGRATS\n";
            }
            bet_Amount = 0;
            std::cout
            << "Your new total is: $" << in_handCash
            << "\n-----------------------------------\n";
        }
    
    std::cout << "You haven't got any money left\n";
    
    return 0;
}
Pages: 12