Need help with C++

I need to do a program similar to this in C++,this is currently in java. Any idea on how to do it?

Put the code you need help with here.
[public static void main(String[] args) {

//declare some variables
int stake = 50;
int betValue = 1;
int target = 250;

// do the gambler ruin simulation
while (stake > 0 && stake < target) {

// make bet
if (Math.random() > 0.5) {
stake = stake + betValue;
System.out.println("win, stake now: " + stake);

}
else {
stake = stake + betValue;
System.out.println(" lose, stake now");
}
}


}
}
]

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
#include <iostream>
#include <stdlib.h>
#include <time.h>

using std::cout;

int main(void)
{
  int stake = 50, betValue = 1, target = 250;

  srand (static_cast <unsigned> (time(0)));

  while (stake > 0 && stake < target) 
  {
    // http://stackoverflow.com/questions/686353/c-random-float-number-generation
    float r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
    if (r > 0.5)
    {
      stake = stake + betValue;
      cout << "win, stake now: " << stake << "\n\n";
    }  
    else
    {
      stake = stake + betValue;
      cout << " lose, stake now: " << stake << "\n\n";
    }

  }
  system("pause");
return 0;
}
Last edited on
Also, article on how to generate random number: http://www.cplusplus.com/articles/EywTURfi/
Only System.out.println and Math.random don't exist in C++, and main looks different, everything else is same.
Topic archived. No new replies allowed.