Conflict with Yahtzee program

Hello, I have a strange error inside of a Yahtzee program I have been developing. Everything included inside of this code is designed to meet requirements set for the project, such as arrays and functions.

The program should roll five "dice" and show which number each die landed on, and the total of the dice added together. If all 5 dice come up as matching numbers, the program will indicate a "Yahtzee" and record the straight in a counter.

The user can choose between rolling all 5 dice in one go or individually. Because there are two different types of rolls, the function that handles individual rolls calls the data within the function that rolls them all to rewrite the values. Finally, the program will create a text document with a simple message when it runs.

Here is the 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <fstream>   //for files
#include <stdlib.h>  //for rand/srand
#include <time.h>    //for time
using namespace std;

int rollDice();
int rollSingle();
int straight = 0; // A counter to track how many straights have been rolled
void display(int num);
bool askToReroll();
bool singleReroll();

int main() {

  srand(time(NULL));
  int total;
  bool rollAgain;
  bool singleRoll;

  cout << "Welcome to the Yahtzee challenge. See if you can roll 5 of a kind." << endl;

 do {
 total = rollDice();
   display(total);
   rollAgain = askToReroll();

  } while (rollAgain == true);

 do {
   total = rollSingle(y);
   display(total);
   singleRoll = singleReroll();

  } while (rollAgain == false && singleRoll == true);

// Creation of Yahtzee file

  ofstream out("Yahtzee.txt");
  out << "Thank you for taking the Yahtzee challenge." << endl;
  out.close();

  return 0;
 }

 int rollDice() {
 int die[5] = {0};

  int t = 0;
   t += rand() % 6 + 1;
   cout << t << endl;
   die[0] = t;

   int u = 0;
   u += rand() % 6 + 1;
   cout << u << endl;
   die[1] = u;

   int v = 0;
   v += rand() % 6 + 1;
   cout << v << endl;
   die[2] = v;

    int w = 0;
   w += rand() % 6 + 1;
   cout << w << endl;
   die[3] = w;

   int x = 0;
   x += rand() % 6 + 1;
   cout << x << endl;
   die[4] = x;

   int y = t+u+v+w+x;
   return y;
 }

int rollSingle(int t, int u, int v, int w, int x, int y) {
int die[5] = {t, u, v, w, x};

int response1;
cout <<"Roll the first number again? Type 1 to roll again, type anything else to decline." <<endl;
cin >> response1;

if (response1 = 1) {
 int t = 0;
   t += rand() % 6 + 1;
   cout << t << endl;
   die[0] = t;
}

else {
  cout << t << endl;
}

int response2;
cout <<"Roll the second number again? Type 2 to roll again, type anything else to decline." <<endl;
cin >> response2;

if (response2 = 2) {
 int u = 0;
   u += rand() % 6 + 1;
   cout << u << endl;
   die[1] = u;
}

else {
  cout << u << endl;
}

int response3;
cout <<"Roll the third number again? Type 3 to roll again, type anything else to decline." <<endl;
cin >> response3;

if (response3 = 3) {
 int v = 0;
   v += rand() % 6 + 1;
   cout << v << endl;
   die[2] = v;
}

else {
  cout << v << endl;
}

int response4;
cout <<"Roll the fourth number again? Type 4 to roll again, anything else to decline." <<endl;
cin >> response4;

if (response4 = 4) {
 int w = 0;
   w += rand() % 6 + 1;
   cout << w << endl;
   die[3] = w;
}

else {
  cout << w << endl;
}

int response5;
cout <<"Roll the fifth number again? Type 5 to roll again, anything else to decline." <<endl;
cin >> response5;

if (response5 = 5) {
 int x = 0;
   x += rand() % 6 + 1;
   cout << x << endl;
   die[4] = x;
}

else {
  cout << x << endl;
}

 y = t+u+v+w+x;
 return y;
}

  void yahtzee(int t, int u, int v, int w, int x, int y) {
  if (t==u && u==v && v==w && w==x && x==y)
  cout <<"Yahtzee! " << endl;
  int straight = straight + 1;

}

  void display(int num) {
  cout << "Your total is: " << num << endl;
  cout << "You have rolled straights " << straight << " times." << endl;
  }


bool askToReroll() {
  char yesno;
  cout << "Roll again? (y/n): ";
  cin >> yesno;

  if (yesno == 'y')
    return true;

  return false;

}

bool singleReroll() {
  char yesno;
  cout << "Roll one number instead? (y/n): ";
  cin >> yesno;

  if (yesno == 'y')
    return true;

  return false;

}


This piece of code causes trouble:
 
total = rollSingle(y);


To display the total of the dice roll for the single roll, the program should call for variable y in the rollSingle function. However, when I try to state that the value should be called to be used for the total, I get this message: error: 'y' was not declared in this scope.

I tried to define y as an integer, but if I try to say total = rollSingle(int y); , I receive this message:
error: expected primary-expression before 'int'.

I am not sure if it is because I cannot call an integer for this bit of my code or otherwise. I need a way to call y from the rollSingle function so that the total of my dice roll registers as that value.
The error is exactly where the compiler is telling you.

You say : total = rollSingle(y);

But at top where you declare your function rollSingle(), it has no arguments. Therefore there is a conflict.

Your declarations, definitions, parameters of the callee function must match.
Last edited on
I think I understand what you are trying to say. Does the original declaration need to include a reference to y, such as: total = rollSingle(y); ?

EDIT: I have changed my program to something that does compile, but the second half is not functioning right. It does not let me roll individual numbers.

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <iostream>
#include <fstream>   //for files
#include <stdlib.h>  //for rand/srand
#include <time.h>    //for time
using namespace std;

int rollDice();
int straight = 0; // A counter to track how many straights have been rolled
void display(int num);
bool askToReroll();
bool singleReroll();

int main() {

  srand(time(NULL));
  int total;
  bool rollAgain;
  bool singleRoll;

  cout << "Welcome to the Yahtzee challenge. See if you can roll 5 of a kind." << endl;

 do {
   total = rollDice();
   display(total);
   rollAgain = askToReroll();

  } while (rollAgain == true);

 do {
   int rollSingle(int t, int u, int v, int w, int x);
   display(total);
   singleRoll = singleReroll();

  } while (rollAgain == false && singleRoll == true);

// Creation of Yahtzee file

  ofstream out("Yahtzee.txt");
  out << "Thank you for taking the Yahtzee challenge." << endl;
  out.close();

  return 0;
}

int rollDice() {
 int die[5] = {0};
 int t = 0;
   t += rand() % 6 + 1;
   cout << t << endl;
   die[0] = t;

   int u = 0;
   u += rand() % 6 + 1;
   cout << u << endl;
   die[1] = u;

   int v = 0;
   v += rand() % 6 + 1;
   cout << v << endl;
   die[2] = v;

   int w = 0;
   w += rand() % 6 + 1;
   cout << w << endl;
   die[3] = w;

   int x = 0;
   x += rand() % 6 + 1;
   cout << x << endl;
   die[4] = x;

   int y = t+u+v+w+x;
   return y;
 }

int rollSingle(int t, int u, int v, int w, int x) {
int die[5] = {t, u, v, w, x};

int response1;
cout <<"Roll the first number again? Type 1 to roll again, type anything else to decline." <<endl;
cin >> response1;

if (response1 = 1) {
 int t = 0;
   t += rand() % 6 + 1;
   cout << t << endl;
   die[0] = t;
}

else {
  cout << t << endl;
}

int response2;
cout <<"Roll the second number again? Type 2 to roll again, type anything else to decline." <<endl;
cin >> response2;

if (response2 = 2) {
 int u = 0;
   u += rand() % 6 + 1;
   cout << u << endl;
   die[1] = u;
}

else {
  cout << u << endl;
}

int response3;
cout <<"Roll the third number again? Type 3 to roll again, type anything else to decline." <<endl;
cin >> response3;

if (response3 = 3) {
 int v = 0;
   v += rand() % 6 + 1;
   cout << v << endl;
   die[2] = v;
}

else {
  cout << v << endl;
}

int response4;
cout <<"Roll the fourth number again? Type 4 to roll again, anything else to decline." <<endl;
cin >> response4;

if (response4 = 4) {
 int w = 0;
   w += rand() % 6 + 1;
   cout << w << endl;
   die[3] = w;

}

else {
  cout << w << endl;
}

int response5;
cout <<"Roll the fifth number again? Type 5 to roll again, anything else to decline." <<endl;
cin >> response5;

if (response5 = 5) {
 int x = 0;
   x += rand() % 6 + 1;
   cout << x << endl;
   die[4] = x;
}

else {
  cout << x << endl;
}

 int y = t+u+v+w+x;
 return y;
}

  void yahtzee(int t, int u, int v, int w, int x, int y) {
  if (t==u && u==v && v==w && w==x && x==y)
  cout <<"Yahtzee! " << endl;
  int straight = straight + 1;

  }

  void display(int num) {
  cout << "Your total is: " << num << endl;
  cout << "You have rolled straights " << straight << " times." << endl;
  }


bool askToReroll() {
  char yesno;
  cout << "Roll again? (y/n): ";
  cin >> yesno;

  if (yesno == 'y')
    return true;

  return false;

}

bool singleReroll() {
  char yesno;
  cout << "Roll one number instead? (y/n): ";
  cin >> yesno;

  if (yesno == 'y')
    return true;

  return false;

}
 


This time I believe I need to change the code within the do{ rollSingle() to run the single dice roll.
Last edited on
Topic archived. No new replies allowed.