mardi 9 mai 2017

How can I use the output of a rand function, simulating 2 die rolls?

I created 2 functions, each function will roll a dice, then I output the result of each dice in the main function. How can I add the results of each dice together? (I made a comment on the line in the main function)

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

int diceroll1();
int diceroll2();

int main() {


    cout << "you rolled a.\n";
    cout << diceroll1(); cout << endl;
    cout << "and a\n";
    cout << diceroll2(); cout << endl;
    cout << "for a total of:\n";
    cout << diceroll1 + diceroll2 // idk how to sum these two values, and output them, as they are going to be different values each time. 

    system("PAUSE");
    return 0;
}

int diceroll1() {

    int roll1;
    srand(time(0));
    const int numberOfRolls = 1;

    for (int i = 0; i < numberOfRolls; i++) {

        roll1 = 1 + (rand() % 6);
        return roll1;


    }

}
int diceroll2() {

    int roll2;
    srand(1+time(0));
    const int numberOfRolls = 1;

    for (int i = 0; i < numberOfRolls; i++) {

        roll2 = 1 + (rand() % 6);
        return roll2;

    }
}




Aucun commentaire:

Enregistrer un commentaire