CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Gambler's Ruin

Threaded View

  1. #1
    Join Date
    Oct 2011
    Location
    Tennessee
    Posts
    46

    Question Gambler's Ruin

    *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;
    }
    Again Thanks.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured