Three Seperate Functions

Basically, I have this code that has three separate functions, but it's all under the int main(). What I want to know is how I can separate the three functions without receiving syntax errors and losing all my code?

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>

using namespace std;

//Initializes the two dimensional array
 int first_die = 6;
 int second_die = 6;
 int myRoll = 0;
 int roll[6][6];

 bool retry= true;
 char replay;

int main()
{

{
srand (time(NULL));
}
 while (retry)
{
 //Ask user to input the seed value
 unsigned short seed;
	 cout << "Enter the seed";
	 cin >> seed;
	 srand(seed);


//Simulate two dice rolls 
//while asking user to input number of dice rolls
	{	
 cout << "How many times would you like to roll?";
 do
    {
      cin >> myRoll;
    } while ((myRoll < 0) || myRoll >= RAND_MAX);


 for (int row = 0; row < 6; row++)
 {
	 for (int column = 0; column < 6; column++)
		roll[row][column] = 0;
 }
 for (int i = 0; i < myRoll; i++)
	{
		first_die = rand() % 6;
        second_die = rand() % 6;
    
		roll[first_die][second_die]++;
		roll[6][6] = roll[first_die][second_die];
    }
 //print output of dice rolls in 2D array
 cout.setf(ios::fixed);
 cout.setf(ios::showpoint);
 cout.precision(1);

 cout << setw(5) << "";
 cout << setw(5) << "1";
 cout << setw(5) << "2";
 cout << setw(5) << "3";
 cout << setw(5) << "4";
 cout << setw(5) << "5";
 cout << setw(6) << "6\n";

	for(int row = 0; row < 6; row++)
	{
		cout<< "\n" << setw(5) << row +1;
		for(int column = 0; column < 6; column++)
		{
			cout<< setw(5)<< roll[column][row];
		}
		cout<<endl;
	
	}
 }
//How to roll again, if user desires
 
{
	cout << "Try again?(y/n)"<<endl;
	cin>>replay;
	if(replay!='y')
 {
 retry=false;
	}
}

}
return 0;
}
Please share the code with errors, than we can help you out from there!
I'll give you one tip, you should use #define variables for the dimension arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#define ROWS 6
#define COLS 6

int array[ROWS][COLS];

int main(){

   //somewhere later in the code
   for(int i=0; i<COLS; ++i){
      for(int j=0; j<ROWS; ++j){
          //do whatever
      }

}


this way you have only one point of control, which is good design.

theoretically, const ints can work as well, but from what I understand it wouldn't be compiler portable, as some compilers accept consts int array dimensions and some don't. so just use the #define to macro your dimensions.
Last edited on
closed account (Dy7SLyTq)
ill give you a tip. no you shouldnt. use const functions or ints. dont #define
1
2
const int a = 8;
int array[a];


not all compilers accepts this, and is not in the definition of the C++ language, even though its a wildly usable technique
What compilers don't? every book i've read suggested using constants, and i don't see a reason not too ( unless of course the compiler didnt except it )
When I did this on it's own function, it kept repeating without going to the next one (in this case the dice roll sequence).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{

{
srand (time(NULL));
}
 while (retry)
{
 //Ask user to input the seed value
 unsigned short seed;
	 cout << "Enter the seed";
	 cin >> seed;
	 srand(seed);
closed account (Dy7SLyTq)
firstlast, a function is not the same thing as a loop.
I know, but I'm saying that I had to have it under one function in order for the loop to carry out.
just a couple of questions to the OP. do you know how to properly work with functions? and why did you put random brackets around the time seed to random?
Barely, and I forgot to omit those from earlier.
Topic archived. No new replies allowed.