iT邦幫忙

0

C++不明原因中途中止

c++
  • 分享至 

  • xImage
#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,沒有跳出原因,
又或是我沒有設定,煩請指教,謝謝您。

淺水員 iT邦大師 6 級 ‧ 2019-02-01 03:11:20 檢舉
把 num_space 的 new 挪到 constructor 做吧。
宣告那邊還不知道 guess_grid_num 是多少。
a000114 iT邦新手 5 級 ‧ 2019-02-02 21:47:12 檢舉
好的,可是我記得C++不是有支持動態記憶體配置嗎?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2019-02-01 10:29:16
最佳解答

程式跳出是在 std:random_device 這列

std::random_device 在 C++11 才有支援
先確定你的 Visual Studio Code 設定成 C++11 之後
再看可不可以 work

a000114 iT邦新手 5 級 ‧ 2019-02-02 21:46:15 檢舉

Thanks,我後來改用一般的random函數就可以了~

我要發表回答

立即登入回答