Dice program Beginner in need of help

I'm having a problem adding up the values of the dice rolls in a while loop. The program will only store the last value so how would I get it to store multiples values? Here is my program thus far. I would try an array but we haven't gotten to that yet.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
        int die_1=0, die_2=0, total_score1, total_score2, player1score;
        char enter;

        cout<<"Welcome to the dice game. The rules are as follows: \n";
        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<<"Enter the letter s and press enter to roll.\n";
        cin>>enter;
        srand(time(0));
        if((enter = 's') || (enter = 'S'))
        {
                do
                {
                        die_1 = (rand() % 6) +1;
                        die_2 = (rand() % 6) +1;
                        total_score1 = die_1 + die_2;
                        player1score = total_score1;
                        cout<<total_score1<<endl;
                }while((total_score1 != 7) && (total_score1 != 11));
                cout<<"Player score is: "<<player1score<<endl;
        }

        return 0;
}


Anyone know what operators or equation I should use to accomplish this?
Last edited on
I never got an answer plus it was late at night when I posted it. The question would not have been seen so I just posted it again during the day.
You got two responses. One of them was a possible solution, however you didn't respond to it to say either "How would I do that?" or "I don't understand." Instead, you ignored it and reposted your question.

We can see that over the course of the time you've been asking this question, your code has not changed at all, suggesting that you aren't actually working on the problem. Just hoping for an answer.

Some people search for answers prior to posting (and I don't know whether you did or not, it's beside the point) and when they do so, it is helpful if they don't come up with answers strewn across three threads, so please stick with your original thread in the future. There isn't any stigma associated with bumping a post here, unless it's done excessively.

Perhaps you can work with the following.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    std::srand(std::time(0)) ;

    unsigned playerScore = 0 ;

    unsigned diceTotal = 0 ;
    while ( diceTotal != 7 && diceTotal != 11 )
    {
        playerScore += diceTotal ;

        unsigned die1 = rand() % 6 + 1 ;
        unsigned die2 = rand() % 6 + 1 ;

        diceTotal = die1+die2 ;

        std::cout << "Rolled " << die1 << " & " << die2 << ": " << diceTotal << '\n' ;  
    }
    std::cout << "Your game is done!\nScore: " << playerScore << '\n' ;
}
I didn't reply to the answers given which was a mistake and I thank you for pointing that out but I have worked on my code a little. quick question?
What effect does "std" have on the code and when I tried the += function on my program I kept getting a strange value. I can post what it gave me. I tried that before I posted the question so I just went back to what I had.

This was the output when I used the += function.

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.
Enter the letter s and press enter to roll.
s
4
10
6
7
Player score is: 6471695
[nheath@localhost lab5]$

Is there a way to fix that?
Iinitialize the variable to a reasonable value. For instance, it might be reasonable to start with a score of 0. See line 9 in the snippet above.
Oh ok thank you. That fixed my problem. Setting it to 0 before everything else made the program work. Thank you and sorry if I was rude for reposting and ignoring the other answers I got. I'll remember what and what not to do.
Topic archived. No new replies allowed.