-1

クラス Dice を使用してサイコロを振るための C プログラムを作成する必要があります。主な要件は、このメインを使用して編集する必要があることです。

int main()
{
      Dice* ptrDice;
             ???
      for (int i = 0; i < 5; i++)
      {
           ????                // roll the 5 dice
           ????                // print the outcome
      }
}

ここでポインターを使用する方法がわかりません。誰か助けてくれませんか?

これが私のコードですが、機能していません:(

#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

class Dice{
  public:
    Dice();
    int getNums();
    void Roll();
  private:
    int nNums;
};

Dice::Dice(){
    nNums=5;
}
int Dice::getNums()
{
    return nNums;
}
void Dice::Roll()
{
    nNums = rand()%6 + 1;
}

int main()
{
      Dice* ptrDice = new Dice;
      ptrDice -> getNums();
      for (int i = 0; i < 5; i++)
      {
       getNums[i] = rand()%6 + 1;                // roll the 5 dice
       cout << "You rolled: ";
       cout << ptrDice->getNums() << setw(4);
       cout << endl;                             // print the outcome
      }
}

私の主な問題は、そのptrDiceを使用してメイン関数で印刷することだと思います!

4

1 に答える 1