### 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

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include #include #include 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.

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include #include #include 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.