我需要一个模拟掷骰子的程序。我写了代码,但我在每卷上得到 1 个数字。我需要得到 5 个数字。这是我的代码:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
struct Dice
{
int die[5];
};
void roll(Dice&);
void print(Dice);
int main()
{
Dice myDice;
roll(myDice);
print (myDice);
return 0;
}
void roll(Dice &num)
{
for(int i = 0; i < 5; i++)
num.die[i] = rand()%10;
}
void print(Dice num)
{
for(int i = 0; i < 5; i++)
cout << "You rolled: " << num.die[i] << endl;
}
输出应该与此类似:
You rolled 6 6 5 5 6
You rolled 5 1 1 5 3
You rolled 5 6 2 2 1
You rolled 6 4 3 4 4
You rolled 3 4 2 6 5
但我的输出是:
You rolled 6
You rolled 5
You rolled 5
You rolled 6
You rolled 3
请帮我弄清楚!