*This is an assignment*
*instructions for original assignment attached*
Ok, I am trying to get the program to calculate the number of times one goes broke and the number of time one reaches the goal. I want the program to run a number of trails, originally it was going to be 1000, but I kept it short for testing.
Can anyone explain what I am doing wrong I know it has to do with the mess of a loop that I have created. Also any advice?
Thanks in advance.
Code:
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL)); // Seed the RNG once, at the start of the program
int n, m, flip, trial;
int broke = 0;
int goal = 0;
cout << "Gamblers' Ruin" << endl;
cout << "Please enter the amount of money you wish to bet." << endl;
cin >> n;
cout << "You entered $" << n << " dollars." << endl;
cout << endl;
cout << "Please enter the goal amount you wish to quit at." << endl;
cin >> m;
cout << "You entered: " << m << endl;
cout << endl;
for (trial = 1; trial <= 100; trial++)
{
while(n == 0 || n != m)
{
if(rand()%2 == 0)
{
cout << "Heads" << endl;
n++;
cout << endl;
}
else
{
cout << "Tails" << endl;
n--;
cout << endl;
}
cout << n;
cout << endl;
}
if (n == 0)
{
broke++;
}
else if (n == m)
{
goal++;
}
}
cout << broke;
cout << endl;
cout << goal;
cout << endl;
system("pause");
return 0;
}
I am wanting the program here to run 100 trials and for every trial display the number of times one will go broke or reach there goal depending on the initial amount that is bet.
So for one trail say you bet $5 dollars. Then the coin is flipped and depending on heads $1 will be added, or tails $1 deduction. This will run until the user either loses all their money, or reaches the goal. So if the user does lose all their money then increment broke, or if they reach their goal increment goal.
This will run 100 times so the output should finally be something like this:
Goal: 43
Broke: 57
a total of 100 trails.
I am actually getting the trial to run 100 times and that is it. Goal and Broke are not being incremented like they are supposed to be, and this is wrong I am having trouble trying to get the loops to work in properly.
I think that the design of the loop is crappy.
I am wanting the program here to run 100 trials and for every trial display the number of times one will go broke or reach there goal depending on the initial amount that is bet.
Say n = 5 and m = 10. I get tails 5 straight times, now n == 0.
Your while loop doesn't stop if n == 0, since n != m. Now say I get tails again, n is now -1, so gain gets increased. So in other words, you can be broke (more than broke, you owe money) and gain at the same time!
Once n gets to 0, that while loop should be stopped, since you have nothing left.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; March 18th, 2012 at 12:18 AM.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.