Hello I apologise if im posting in the wrong place at all im fairly new to your forums and i was wondering if someone could help me with this bit of code im writing. Its supposed to roll 4 random d6 dice and then minus the smallest roll and print the result at the end. It seemed all working but when i ran it it just outputs 0 at the end. I can't see why as im very new to c++ and am teaching myself here, but i went and checked if the randoms where working allright and they were so im stumped?

Can anyone please figure out what is wrong with my code and any suggestions you have would be great because im eager to learn any new tricks, its bloody hard teaching yourself when you have no one at all guiding you. Trial and error sucks.

Anyways heres my code:

``````#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <stdio.h>
#include <time.h>

using namespace std;

void rolling();
void test_1();
void test_2();
void test_3();
void result();

int rolls[4];
int x;
int y;
int z;

int main()
{

srand((unsigned)time(0));

rolling();
return 0;
}

void rolling()
{
int roll_1 = rand() % 6 + 1;
int roll_2 = rand() % 6 + 1;
int roll_3 = rand() % 6 + 1;
int roll_4 = rand() % 6 + 1;

int rolls[] = { roll_1, roll_2, roll_3, roll_4 };

test_1();
return;
}

void test_1()
{

if (rolls [0] >= rolls [1]) {
x = rolls [1];
test_2();
}

else {x = rolls [0];
test_2();
}

return;
}

void test_2()
{

if (rolls [2] >= rolls [3]) {
y = rolls [3];
test_3();
}

else { y = rolls [2];
test_3();
}

return;
}

void test_3()
{

if (x >= y) {
z = y;
result();
}

else { z = x;
result();
}

return;
}

void result()
{
int r = (((rolls[0] + rolls[1]) + (rolls[2] + rolls[3])) - z);

cout << r << endl;
system ("PAUSE");
return;
}``````

Thank you very much everyone on Daniweb forums i appreciate your help and advice.

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Passmark

Hi.

you are wrong in initializing the array entities.

your code must be changed as following.
=============================
int rolls[] = { roll_1, roll_2, roll_3, roll_4 };
=============================
int rolls[4];

rolls[0] = roll_1;
rolls[1] = roll_2;
rolls[2] = roll_3;
rolls[3] = roll_4;

Good luck!!

Try using a 'for' loop.
Also, as this *should* be a very small piece of code, there is no need to declare your own functions. It should look more like this,

``````#define ROLLS	4
#define DIESIDES	6
int main()
{
int num[ROLLS];
int min = DIESIDES+1;
int total = 0;

srand((unsigned)time(NULL));

for (int c = 0; c < ROLLS; c++) {
if ((num[c] = rand() % DIESIDES + 1) < min)
min = num[c];
total += num[c];
}
printf ("%d\n", total-min);
return 0;
}``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.