Need help in spotting error

I'm creating a dice roll program and my teacher asked us to change our program around a # of different times. This time she wanted us to use specific functions in different ways. I'm having trouble spotting where my logic is off as I can't seem to get the rolls of the 2 players to differ. They stay the same. My program worked fine when everything wasn't included in the function. below is the current program and I will also post the original. Can someone help me find where my logic is off?
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 <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


int rolldice(int numrounds);//functions defined later
void EnterRounds(int& numRounds);
void OutputScore(int& player1score, int& player2score);



//this program will roll dice randomly for two users and determine the winner based on who has the higher score
int main()
{
        int rounds;
        srand(time(0)); //allows for random numbers of diceroll
        cout<<"Welcome to the dice game. The rules are as follows: \n"; //rules


        cout<<"1. Roll the two dice and add up the total. \n";


        cout<<"2. If you roll a 7 or an 11, your turn is over and it is now the other players turn. \n";


        cout<<"3. If the other player rolls a 7 or an 11, then the scores are tallied and the player with the highest score wins.\n";

        cout<<"Please enter the number of rounds that you and the other player want to play and press enter.\n" ;

        cin>>rounds;
        //EnterRounds(rounds);
        //cout<<rounds<<endl;
        rolldice(rounds);
        /*if(overallscore2 > overallscore1)//if statement that determines the winner based on the scores
        {
                cout<<"Player 2 is the winner.\n";
        }       
        else if(overallscore2 < overallscore1)
        {
                cout<<"Player 1 is the winner.\n";
        } */
        return 0;
}

int rolldice(int numrounds)
{
        int overallscore1 =0, overallscore2 = 0, playerscore=0;
        cout<<"Number of rounds entered = "<<numrounds<<endl;
        for(int r = 0; r < numrounds; r++)
        {
                int die_1=0, die_2=0, total_score1=0, total_score2=0; //variables for calculating player scores
         do
                {
                        playerscore += total_score1;
                        die_1 = rand() % 6 +1;//die is 6 sided
                        die_2 = rand() % 6 +1;//die is 6 sided 

                        total_score1 = die_1 + die_2;
                        cout<<"Player  rolled: "<<die_1<<" and "<<die_2<<": "<<total_score1<<endl;
                        if(total_score1 == 7 || total_score1 == 11)//changes to the next player if player 1 rolls a 7 or an 11. 7 and 11 are not counted in the total score
                        {
                                cout<<"Your turn is now over. It is the next player's turn.\n";
                        }
                }while(total_score1 != 7 && total_score1 != 11);

        int totalsum1 = playerscore;
        overallscore1 += totalsum1;
        int totalsum2 = playerscore;
        overallscore2 += totalsum2;
        cout<<"Round score for Player 1 is: "<<totalsum1<<endl;
        cout<<"Round score for player 2 is: "<<totalsum2<<endl;

        OutputScore(overallscore1, overallscore2);
        }
        return(playerscore);
}
void EnterRounds(int& numRounds)
{
        numRounds = 25;
        cout<<"Number of rounds entered = "<<numRounds<<endl;

}
void OutputScore(int& player1score, int& player2score)
{
                cout<<"Player 1 overall score is: "<<player1score<<endl;
                cout<<"Player 2 overall score is: "<<player2score<<endl;
                cout<<"Round Over."<<endl;

}
/* Function Format:
 * Return type function name(parameters){
 *
 * }
 */                                                                                                                                                                                        
this is the current code.

and this is the original
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


int rolldice();//functions defined later
int rolldice2();

//this program will roll dice randomly for two users and determine the winner based on who has the higher score
int main()
{
        int rounds;
        srand(time(0)); //allows for random numbers of diceroll
        cout<<"Welcome to the dice game. The rules are as follows: \n"; //rules


        cout<<"1. Roll the two dice and add up the total. \n";


        cout<<"2. If you roll a 7 or an 11, your turn is over and it is now the other players turn. \n";


        cout<<"3. If the other player rolls a 7 or an 11, then the scores are tallied and the player with the highest score wins.\n";

        cout<<"Please enter the number of rounds that you and the other player want to play and press enter.\n" ;

        cin>>rounds;
        int overallscore1=0, overallscore2=0;
        for(int r = 0; r < rounds; r++)//for statement allows program to run for a # of times based on user input
        {
                int totalsum1;
                totalsum1 = rolldice();//calls the function rolldice and adds up the sum of players
                overallscore1 += totalsum1;//totals overall score for player 1
                int totalsum2;
                totalsum2 = rolldice2();//see total sum 1
                overallscore2 += totalsum2;//totals overall score for player2
                cout<<"Round score for Player 1 is: "<<totalsum1<<endl;
                cout<<"Round score for Player 2 is: "<<totalsum2<<endl;
                cout<<"Player 1 overall score is: "<<overallscore1<<endl;
                cout<<"Player 2 overall score is: "<<overallscore2<<endl;
                cout<<"Round Over."<<endl;
        }
        if(overallscore2 > overallscore1)//if statement that determines the winner based on the scores
        {
                cout<<"Player 2 is the winner.\n";
        }
        else if(overallscore2 < overallscore1)
        {
                cout<<"Player 1 is the winner.\n";
        }
        return 0;
}

int rolldice()
{
        int die_1=0, die_2=0, total_score1=0, total_score2=0, player1score=0, player2score=0; //variables for calculating player scores
        do
        {
                player1score += total_score1;
                die_1 = rand() % 6 +1;//die is 6 sided
                die_2 = rand() % 6 +1;//die is 6 sided 

                total_score1 = die_1 + die_2;
                cout<<"Player 1 rolled: "<<die_1<<" and "<<die_2<<": "<<total_score1<<endl;
        if(total_score1 == 7 || total_score1 == 11)//changes to the next player if player 1 rolls a 7 or an 11. 7 and 11 are not counted in the total score
        {
                cout<<"Your turn is now over. It is player 2's turn.\n";
        }
        }while(total_score1 != 7 && total_score1 != 11);
        return (player1score);
}
int rolldice2()
{
        int die_1=0, die_2=0, total_score1=0, total_score2=0, player1score=0, player2score=0; //variables for calculating player scores
        do
                {
                        player2score += total_score2;

                        die_1 = rand() % 6 +1;
                        die_2 = rand() % 6 +1;

                        total_score2 = die_1 + die_2;
                        cout<<"Player 2 rolled: "<<die_1<<" and "<<die_2<<": "<<total_score2<<endl;

                }while((total_score2 != 7) && (total_score2 != 11));
        return (player2score);
}
/* Function Format:
 * Return type function name(parameters){
 *
 * }
 */

She told me I had to turn roll dice into one function instead of two so thats where the other roll dice function went.
Here's an example of what it does.


Welcome to the dice game. The rules are as follows:
1. Roll the two dice and add up the total.
2. If you roll a 7 or an 11, your turn is over and it is now the other players turn.
3. If the other player rolls a 7 or an 11, then the scores are tallied and the player with the highest score wins.
Please enter the number of rounds that you and the other player want to play and press enter.
3
Number of rounds entered = 3
Player rolled: 1 and 3: 4
Player rolled: 2 and 1: 3
Player rolled: 5 and 4: 9
Player rolled: 2 and 4: 6
Player rolled: 6 and 5: 11
Your turn is now over. It is the next player's turn.
Round score for Player 1 is: 22
Round score for player 2 is: 22
Player 1 overall score is: 22
Player 2 overall score is: 22
Round Over.
Player rolled: 6 and 6: 12
Player rolled: 2 and 4: 6
Player rolled: 4 and 4: 8
Player rolled: 2 and 3: 5
Player rolled: 5 and 4: 9
Player rolled: 3 and 3: 6
Player rolled: 2 and 6: 8
Player rolled: 6 and 1: 7
Your turn is now over. It is the next player's turn.
Round score for Player 1 is: 76
Round score for player 2 is: 76
Player 1 overall score is: 98
Player 2 overall score is: 98
Round Over.
Player rolled: 3 and 4: 7
Your turn is now over. It is the next player's turn.
Round score for Player 1 is: 76
Round score for player 2 is: 76
Player 1 overall score is: 174
Player 2 overall score is: 174
Round Over.
When you get to line 67, how do you know who's turn it was?

Why do you add playerScore to both the first players total and the second player's total?

In the original code, each player rolled in one round. In the current code, one player rolls per round and it gets added to both players' scores.
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


int rolldice();//functions defined later


//this program will roll dice randomly for two users and determine the winner based on who has the higher score
int main()
{
        int rounds;
        srand(time(0)); //allows for random numbers of diceroll
        cout<<"Welcome to the dice game. The rules are as follows: \n"; //rules


        cout<<"1. Roll the two dice and add up the total. \n";


        cout<<"2. If you roll a 7 or an 11, your turn is over and it is now the other players turn. \n";


        cout<<"3. If the other player rolls a 7 or an 11, then the scores are tallied and the player with the highest score wins.\n";

        cout<<"Please enter the number of rounds that you and the other player want to play and press enter.\n" ;

        cin>>rounds;
        int overallscore1=0, overallscore2=0;
        for(int r = 0; r < rounds; r++)//for statement allows program to run for a # of times based on user input
        {
                int totalsum1;
                totalsum1 = rolldice();//calls the function rolldice and adds up the sum of players
                overallscore1 += totalsum1;//totals overall score for player 1
                int totalsum2;
                totalsum2 = rolldice();//see total sum 1
                overallscore2 += totalsum2;//totals overall score for player2
                cout<<"Round score for Player 1 is: "<<totalsum1<<endl;
                cout<<"Round score for Player 2 is: "<<totalsum2<<endl;
                cout<<"Player 1 overall score is: "<<overallscore1<<endl;
                cout<<"Player 2 overall score is: "<<overallscore2<<endl;
                cout<<"Round Over."<<endl;
        }
        if(overallscore2 > overallscore1)//if statement that determines the winner based on the scores
        {
                cout<<"Player 2 is the winner.\n";
        }
        else if(overallscore2 < overallscore1)
        {
                cout<<"Player 1 is the winner.\n";
        }
        return 0;
}

int rolldice()
{
        int die_1=0, die_2=0, total_score1=0, total_score2=0, playerscore=0; //variables for calculating player scores
        do
        {
                playerscore += total_score1;
                die_1 = rand() % 6 +1;//die is 6 sided
                die_2 = rand() % 6 +1;//die is 6 sided 

                total_score1 = die_1 + die_2;
                cout<<"Player 1 rolled: "<<die_1<<" and "<<die_2<<": "<<total_score1<<endl;
        if(total_score1 == 7 || total_score1 == 11)//changes to the next player if player 1 rolls a 7 or an 11. 7 and 11 are not counted in the total score
        {
                cout<<"Your turn is now over. It is the next player's turn.\n";
        }
        }while(total_score1 != 7 && total_score1 != 11);
        return (playerscore);
}
/* Function Format:
 * Return type function name(parameters){
 *
 * }
 */

well this is what I changed the program to when she told me I could only have one roll dice function. I figured that since roll dice returned player score and both totalsum1 and totalsum2 were set = to roll dice() that inside the function if I set them = to player score it would work the same way. The code I just posted works.
Excellent. You might make a further refinement and change rolldice to:


1
2
3
4
5
6
int rolldice( char const* player )
{
     // ...
        cout << player << " rolled: " << die_1 << " and " << die_2 << ": " << totalscore1 << endl;
     // ...
}

And call it from main like so:

1
2
3
4
5
                int totalsum1;
                totalsum1 = rolldice("Player 1");
                overallscore1 += totalsum1;
                int totalsum2;
                totalsum2 = rolldice("Player 2");


and that way you won't be saying "Player 1 rolled" every time the function is called.
Thanks for the suggestion, the problem is she required that everything be done inside of rolldice and then use outputscore to output the scores of each player. We're practicing with the difference between call by value and call by reference. and also how to call a function within a function. I think my problem lies in the scope of rolldice but I'm still trying different things to figure it out. Does it have anything to do with the fact that outputscore is a void function instead of int?
nheath12 wrote:
and also how to call a function within a function
main is a function. You call other functions from it. I'm not sure what it is you're "practicing"...
I'm supposed to call output score inside of rolldice the way it is in the first program I posted. As of right now I changed rolldice to a void function and experimenting moving things around to find out why my output for both players scores are the same.
nheath12 wrote:
'm supposed to call output score inside of rolldice the way it is in the first program I posted.
I was trying to point out that your statement was flawed because you already know how to call a function within a function.
nheath12 wrote:
I changed rolldice to a void function
What is a "void function"? "void" is the return type, not the type of the function.
1
2
void f(); //function returning void, not "void function"
int g(); //function returning int, not "int function" 
Last edited on
It doesn't matter which way I say it. That doesn't change what it is. I'm just going off of what my book says which defines a void function as a function that does not return any value nor does it need a return statement. But I think I realized what my problem is. After the first roll and change of turn, it isn't rolling again. I just have to figure out to get it to roll again in order to get the score for player 2.
nheath12 wrote:
It doesn't matter which way I say it. That doesn't change what it is.
Maybe not in your head, but as text it changes the meaning dramatically. It's also bad to think of it this way for several reasons.
nheath12 wrote:
I'm just going off of what my book says which defines a void function as a function that does not return any value nor does it need a return statement.
Could you tell me the name of the book? No book should be teaching this - it is wrong.

I'm not trying to be mean, I'm just trying to make sure you understand functions correctly.
Walter Savitch: Problem Solving with C++ 8th Edition

and yeah I understand you aren't. I'm just making myself frustrated as I try different things to fix my program. I'm almost at a conclusion though.
Topic archived. No new replies allowed.