Dice c++

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 main()
{


cout << result << endl;




cout << "Total is: " << Total << endl;
cout << "Highest dice roll: " << Highest << endl;
cout << "Lowest dice roll: " << Lowest << endl;


system("PAUSE");
return 0;
}

See tutorial about functions: http://www.cplusplus.com/doc/tutorial/functions/

Function should get data via its parameters and give result either as return value or via by reference parameters.


For example, std::accumulate is a function (or actually two overloaded function templates).
http://www.cplusplus.com/reference/numeric/accumulate/

1
2
3
4
5
6
7
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.

If you have to use plain array, then see: http://www.cplusplus.com/doc/tutorial/arrays/
As a starter, consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

unsigned DiceRoll(unsigned& lowest, unsigned& highest, unsigned DiceRolls)
{
	const unsigned sizeOfDice {6};
	unsigned total {};

	lowest = sizeOfDice + 1;
	highest = 0;

	for (unsigned i = 0; i < DiceRolls; ++i) {
		auto result {rand() % sizeOfDice + 1};

		total += result;

		if (result > highest)
			highest = result;

		if (result < lowest)
			lowest = result;
	}

	return total;
}

int main()
{
	srand(static_cast<unsigned>(time(nullptr)));

	const unsigned rolls {10};
	unsigned low {}, high {};
	const auto total {DiceRoll(low, high, rolls)};

	cout << "Total is: " << total << '\n';
	cout << "Highest dice roll: " << high << '\n';
	cout << "Lowest dice roll: " << low << '\n';
}

Registered users can post here. Sign in or register to post.