Hello, i'm trying to make a c++ dice, i have to declare all the 10 rolls, lowest, highest and the total value. I could make it work if i wrote all the code inside main but i have to make fucntions outside of main then declare them into main and I cant figure out how :(
#include <iostream>
#include <ctime>
using namespace std;
int DiceRoll()
{
srand(time(0));
int DiceRolls =10;
int sizeOfDice = 6;
int result = 0;
int Total = 0;
int Lowest;
int Highest = 0;
for(int i = 0; i < DiceRolls; i++ )
{
result = rand()%sizeOfDice + 1;
Total += result;
result=result;
if (result > Highest) {
Highest = result;
}
if (result < Lowest) {
Lowest = result;
}
}
};
int DiceRolls =10;
int sizeOfDice = 6;
int result();
int Total = 0;
int Lowest;
int Highest = 0;
int Total = std::accumulate( std::begin(rolls), std::end(rolls), 0 );
// does about same as:
int result = 0;
for ( auto it = std::begin(rolls); it != std::end(rolls); ++it ) {
result += *it;
}
int Total = result;
Note that rolls above an iterable container that stores the values that we want to calculate with.
You can generate values, accumulate total, and track both highest and lowest in one go without storing the values, but your homework here seems to expect you to do the generation, the accumulation and the tracking in separate functions. That means that you have to store the values. A technically logical place to store in would be std::vector, but courses usually cop out and use plain array, like they were teaching C.