wrong output

So im almost done with this code and i cant get this final question
whenever i output the amount of money it becomes really high or really low
but its suppose to stop when the wallet money reaches 0

"Suppose a player initially bets one dollar, but doubles his bet each time
before the dice is rolled until a win or loss is obtained. Again assume that
the player starts with $10,000, and plot the decrease or increase of the player's money over time."

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
//game.cpp code that has issues
void game::play_game4()
{
	srand(time(0));
	for(int i = 0; i <= 10000; i++)
	{
		if (wallet == 0)
		{
			break;
		}
		hand.throw_dice();
		if(hand.toss == 7 || hand.toss == 11)
		{
			wallet2++;
		}
		else if(hand.toss == 2 || hand.toss == 3 || hand.toss == 12)
		{
			wallet2--;
		}
		
		else if(hand.toss == 4 || hand.toss == 5 || hand.toss == 6 || hand.toss == 8 || hand.toss == 9 || hand.toss == 10)
        { 
			if (wallet == 0)
			{
				break;
			}
			if (wallet2 < wager2)
				{
					wager2 = wallet2;
				}
				
			point = hand.toss;
            do
            {
				if (wallet2 <= wager2)
				{
					wager2 = wallet2;
				}
				hand.throw_dice();
				if(hand.toss == point)//wins if player rolls point
				{
                    wallet2 = (wallet2 + wager2);
					wager2 = 1;
				}
                else if(hand.toss == 7)//lose if player rolls 7
				{
                    wallet2 = (wallet2 - wager2);
					wager2 = 1;
				}
                else if(hand.toss != 7 && hand.toss != point) //nothing otherwise
				{
					wager2 = (wager2 * 2);
				}
            }while(hand.toss != point && hand.toss != 7);//keep going until point or 7 is rolled
		}	
		if(i <= 10000)
		{
			if (wallet2 == 0)
			{
				break;
			}
			else 
			{
				cout << "Roll " << i << ": " << "$" << wallet2 << endl;
			}

		}
	}
}


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
//game.h
#ifndef GAME_H
#define GAME_H
#include "roll.h"
#include <ctime>

class game
{
public:
	game();
	int wallet;
	int wager;
	int wager2;
	int wallet2;
	double win;
	double loss;
	double win2;
	double loss2;
	int point;
	void play_game();
	void play_game2();
	void play_game3();
	void play_game4();
	roll hand;
	~game();
};

#endif // GAME_H 

Last edited on
Anyone?
no one wants to help put?
break statement breaks out of the innermost conditional, generally using break is a bad idea. It's hard to interpret the flow of the program
Last edited on
I don't see what's wrong so your going to have to step though the code.

What I would do is put in cout statements every time the values you are testing change. It might look like this


cout << LoopCount << " " << wager2 << endl; LoopCount++; // For Testing, remove when done.
closed account (3qX21hU5)
break statement breaks out of the innermost conditional, generally using break is a bad idea. It's hard to interpret the flow of the program


I have never heard that before (That they are a bad idea to use)... Break statements are very helpful and don't confuse the flow of the program at all in my opinion.

Also break statements don't break out of if conditionals... They will "break" out of a switch conditional and loops though.



@OP

Could you state exactly what problem you are having? You are quite vague on the problem and to be honest your code is so poorly formatted it is quite hard to browse through it (Please keep indentation consistent).

So basically we are going to need more information on exactly what you are having trouble with (The more details you provide the more we can help).

Otherwise I will just say what I usually say. Learn how to use a debugger. It will be the most important tool you have as a programmer and it is a shame that most schools don't even teach their students about debugging. If I had my way you would learn about it right after you learned about variables, loops, and control statements.

Anyways here is a tutorial on debugging with Visual Studio's http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn . If you don't have VS don't worry the knowledge can be transferred to most other IDE's also.

Take a few hours and make sure you understand them concepts and I guarantee you that you will save yourself from hundreds of hours of headaches in the future.
Last edited on
Topic archived. No new replies allowed.