#include <iostream>
#include <cstdlib>
#include <random>
#include <stdlib.h>
#include <time.h>
using namespace std;
class CGamer
{
private:
int account;
int guess_grid_num;
int *num_space = new int[guess_grid_num];
public:
void addMoney(int money)
{ /* add acccount's money */
account += money;
}
void subMoney(int money)
{ /* subtract acccount's money */
account -= money;
}
int callAccount(void)
{ /* return Account money */
return this->account;
}
int *randomCreat(void)
{ /* Guess number */
for (int i = 0; i < guess_grid_num; i++)
{
*(num_space + i) = rand() % (6) + 1;
}
return num_space;
}
CGamer(int account, int guess_grid_num)
{
this->account = account;
this->guess_grid_num = guess_grid_num;
}
~CGamer()
{
delete[] num_space;
cout << "Gamer had been remove..." << endl;
}
};
int main(void)
{
int banker_account = 0;
int gamer_account = 0;
int guess_grid_num = 0;
int sample_count = 0;
int bet = 100;
int win_time = 0; /* Gamer's win time*/
int loss_time = 0; /* Gamer's loss time*/
int *answer = new int[guess_grid_num];
int *gamer_num_space = NULL;
srand(time(NULL));
std::random_device rd;
std::default_random_engine gen = std::default_random_engine(rd());
std::uniform_int_distribution<int> dis(1, 6);
cout << "Pls input the innitial money the banker has :";
cin >> banker_account;
cin.get();
cout << "Pls input the innitial money the gamer has :";
cin >> gamer_account;
cin.get();
cout << "Pls input the grid's number you guess :";
cin >> guess_grid_num;
cin.get();
CGamer gamer(gamer_account, guess_grid_num);
cout << "Pls input the sample count :";
cin >> sample_count;
cin.get();
for (int i = 0; i < sample_count; i++)
{
cout << "-------------------------------------" << endl;
cout << endl;
gamer_num_space = gamer.randomCreat();
for (int j = 0; j < guess_grid_num; j++)
{
*(answer + j) = dis(gen);
}
for (int k = 0; k < guess_grid_num; k++)
{
cout << *(answer + k) << " " << *(gamer_num_space + k) << endl;
if (*(gamer_num_space + k) == *(answer + k))
{
gamer.addMoney(bet);
banker_account -= 100;
win_time++;
}
else
{
gamer.subMoney(bet);
banker_account += 100;
loss_time++;
}
}
cout << "Gamer Account :" << gamer.callAccount() << endl;
cout << "Banker Account :" << banker_account << endl;
cout << endl;
cout << "-------------------------------------" << endl;
}
cout << "-------------------------------------" << endl;
cout << "Gamer's win time :" << win_time << endl;
cout << "Gamer's loss time :" << loss_time << endl;
cout << "Gamer Account :" << gamer.callAccount() << endl;
cout << "Banker Account :" << banker_account << endl;
system("pause");
return 0;
}
我正在用C++來計算骰子的機率
不過到了這一段輸入完之後
cout << "Pls input the grid's number you guess :";
cin >> guess_grid_num;
cin.get();
便中途停止了,我不知道是什麼原因,還煩請大大告訴我。
我的編譯器是VS Code,沒有跳出原因,
又或是我沒有設定,煩請指教,謝謝您。
程式跳出是在 std:random_device 這列
而
std::random_device 在 C++11 才有支援
先確定你的 Visual Studio Code 設定成 C++11 之後
再看可不可以 work