#include <iostream> // Is it too much to ask for your headers?
#include <cstdlib>
#include <ctime>
usingnamespace std;
void rollDice(int rollResults[])
{
srand((unsignedint)time(nullptr)); // rewritten
// rollResults[3]; // No, No, No!
for (int i = 0; i < 3; i++) // <3 , not <= 3
{
rollResults[i] = rand() % 6 + 1;
cout << rollResults[i] << "\n"; // This would be better in the calling function
}
}
void printRoll()
{
int user[3];
rollDice(user);
// Your output really ought to go here ...
}
int main()
{
printRoll(); // <====== Proper function call
}
#include <iostream>
#include <random>
#include <vector>
void rollDice(std::vector<int>& rolls)
{
// create a pre-defined random engine
// seeding it with the non-deterministic random engine
std::default_random_engine rng(std::random_device {} ());
// create a distribution to simulate a die's pips
std::uniform_int_distribution<int> dis(1, 6);
// 'cast the die' for each element in the vector
for (size_t itr { }; itr < rolls.size(); ++itr)
{
rolls[itr] = dis(rng);
}
}
void printRoll()
{
// create a vector of known size
std::vector<int> rolls(3);
rollDice(rolls);
// use a range-based for loop to iterate through the vector
for (constauto& itr : rolls)
{
std::cout << itr << ' ';
}
std::cout << '\n';
}
int main()
{
printRoll();
}