Yahtzee Program help

I am brand new to c++, I have been taking a c++ class for about a month, and our current assignment is to program Yahtzee. My current problem is that I am having a problem when I ask the user if they would like to keep or reroll certain numbers. It will reroll every number no matter what I input. Could someone help me so I can continue coding this? My current code is as follows

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
#include <iostream>
#include <time.h>
#include <stdlib.h>

using namespace std;

int main () {

int rand ( void );
int dice[5];
int turn;
char reroll[5];
dice[0]=rand() % 6+1;
dice[1]=rand() % 6+1;
dice[2]=rand() % 6+1;
dice[3]=rand() % 6+1;
dice[4]=rand() % 6+1;
turn=1;



while (turn<=2) {
cout << "Turn #" << turn;
cout << "Your first roll is: " << dice[0] << dice[1] << dice[2] << dice[3] << dice[4] << " (Type 'y' to re-roll or 'n' to stay)";
cin >> reroll[0] >> reroll[1] >> reroll[2] >> reroll[3] >> reroll[4];
if (reroll[0]='y'){
dice[0]=rand() % 6+1;}
if(reroll[0]='n'){
dice[0];}
if (reroll[1]='y'){
dice[1]=rand() % 6+1;}
if (reroll[2]='y'){
dice[2]=rand() % 6+1;}
if (reroll[3]='y'){
dice[3]=rand() % 6+1;}
if (reroll[4]='y'){
dice[4]=rand() % 6+1;}
cout << "Your second Roll is " << dice [0] << dice[1] << dice [2] << dice[3] << dice[4] << " (Type 'y' to re-roll or 'n' to stay)";



dice[0]=rand() % 6+1;
dice[1]=rand() % 6+1;
dice[2]=rand() % 6+1;
dice[3]=rand() % 6+1;
dice[4]=rand() % 6+1;
turn++;}


return 0;
}


it is obvious that this code is not finished, it does not keep score and it does not continue on to the third roll for each turn, but I simply need help with saving some numbers and rerolling others before I move on to those.
Could you explain briefly what the game is meant to do. I assume from your comment you would pretty much go through that middle section once more, so that you can re-roll each dice twice if you want. But how does Yahtzee scoring work? My only understanding of Yahtzee is from that one southpark episode. And he just wins everytime straight away.

Meerkat
Basically in Yahtzee you roll 5 dice (or random numbers from 1-6 in this case) and based upon combinations of those dice, such as a pair of ones, a straight, or all dice reading the same, you gain points. When playing you are given the option to re roll some of your dice twice, and that it what I am having issues with. The user will type ynynn and that would mean they want to re roll dice[0] and dice[2], but the program simply generates a new value for all of the dice, and I am not sure why.
ah I see. Immediately after you do cin >> reroll[0] >> reroll[1] >> reroll[2] >> reroll[3] >> reroll[4]; check the values of each of your rerolls. And make sure that they are what you think that they should be. If you don't know any better way just do cout <<"reroll[0]: "<< reroll[0] << endl; for each one.

Meerkat
My reroll array values are all outputting what they are supposed to be, yet my numbers still do not reflect that.
if (reroll[0]='y'){


Do not mix up assignment operator and the equals comparison operator.

I believe above should be if (reroll[0] == 'y'){

Last edited on
That fixed it, thank you very much.
Topic archived. No new replies allowed.