From main to functions

Im new to programming and i dont know how to make my code into functions, basically i want to keep my main function as clear as possible. The code generates random dices from 1-6.I will be using this code for monopoly, thats why there is double option in my code. Thanks for help

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
41
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;



int main()
{
int dice1,dice2 = 0;
bool bl_double;
srand(time(NULL));
dice1=6;
dice2=6;

cout<<"You rolled "<< dice1 << " and " << dice2 << " You move "<< dice1+dice2 <<" spaces."<< endl;
if(dice1 == dice2)
{
cout<<"You rolled a DOUBLE!!!" << endl;
bl_double = true;
while(bl_double)
{
dice1=rand()%6+1;
dice2=rand()%6+1;
cout<<"You rolled "<< dice1 << " and " << dice2 << " You move "<< dice1+dice2 <<" spaces."<< endl;
if(dice1 != dice2)
{
bl_double=false;
}
else
{
cout<<"You rolled a DOUBLE!!!" << endl;
}


}
}

cin.ignore();
return 0;
}
You could write a function named roll_dices() which returns a Dices structure.

A structure is used to store two dice rolls at the same time.

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
#include <cstdlib>
#include <ctime>
#include <iostream>

struct Dices
{
    int one; // Dice One
    int two; // Dice Two
};

Dices roll_dices()
{
    Dices d;

    d.one = std::rand() % 6 + 1;
    d.two = std::rand() % 6 + 1;
    return d;
}

bool check_double(Dices d)
{
    if (d.one == d.two)
        return true;

    return false;
}

int main()
{
    std::srand(std::time(NULL));

    Dices d = roll_dices();

    std::cout << "You rolled: " << d.one << " and " << d.two << "!\n";

    if (check_double(d))
        std::cout << "You rolled a double!\n";
}


http://ideone.com/icNale
thank you that will work !
Topic archived. No new replies allowed.