Help with my Code

Hey guys, I am having trouble with my class assignment. This is what I am supposed to do:

To get full credit on this assignment you must use loops and decisions. To get random numbers for your dice in your program you will need to use the rand() and srand() library functions. (See the appendix of the book for examples in the book on how to use rand() and srand().)

A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5 and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on the first roll, the player wins. If the sum is 2, 3 or 12 on the first roll (called “craps”), the player loses (i.e. the “house” wins). If the sum is 4, 5, 6, 8, 9 or 10 on the first roll, then that sum becomes the player’s “point.” To win, you must continue rolling the dice until you “make your points”. The player loses by rolling a 7 before making the point.

It would be very helpful in this program to use boolean variables to keep track of things such as if the user has won the game or not, and if the game is over or not.


So far the program is working. I am stuck on keeping track of when the player rolls more than once.

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
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <limits>
using namespace std;

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

    int die1, die2 = 0; 
    int roll1, roll2 = 0;
    char repeat = 'y';

    cout << "Welcome. The game is about to begin." << endl;

    while (repeat == 'y' || repeat == 'Y') 
    {
        cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
                                                                            
        die1 = rand() % 6 + 1;
        die2 = rand() % 6 + 1; 
        roll1 = die1 + die2;

        cout << "Your roll was: " << die1 << " + " << die2 << " = " << roll1 << endl;
        if (roll1 == 7 || roll1 == 11)
        {
            cout << "You win! Would you like to play again? [Y/N]:" << endl;
            cin >> repeat;
        }
        else if (roll1 == 2 || roll1 == 3 || roll1 == 12)
        {
            cout << "Sorry, you lose! Would you like to play again? {Y/N]:" << endl;
            cin >> repeat;
        } 
    }

    return(0);
}
closed account (3CXz8vqX)
uhm...you mean how many times it has looped before quitting? Or do you mean whether they've chosen to roll more than one dice that round?

If the former, then you'll need an extra variable called 'loop_count' or something along those lines then just before the while loop finishes, increment loop_count.

I am trying to get it to calculate for the "points". Lets say I roll a 10. I need to get it to keep track of the 10 when I continue to roll. I either need to roll another 10 or lose by rolling a 7.
Right now its only calculating for my first roll. I can only win if I roll a 7 or 11. If I don't roll those it keeps looping back to my first roll tell I roll a losing number. It won't keep track of my "point rolls"
Last edited on
closed account (3CXz8vqX)
So lemme get this straight...

If you roll a 10...then a 7...then a 8.... you want something to add up to 25? (The total points so far).
No, If I roll a 10 it is called a "point" I need to roll another 10 inorder to win the game. If I roll anything else it continues to loop until I roll another 10 or a 7, 7 being I lost the game.
Try this for your while loop:

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
#include <locale> // to use std::toupper function

using namespace std;
//put std:: before each STL thing
//or do this:

/*
using std::cout;
using std::cin;
using std::endl;

//similar for anything used a lot

*/

bool Quit = false;

while (!Quit) {

....

std::cout << "Would you like to play again" << std::endl ;
std::cin >> repeat;

repeat = std::toupper(repeat);

     if(repeat != 'Y') {
         Quit = true;
     }

}

//execution continues here 


http://www.cplusplus.com/reference/locale/toupper/
http://www.cplusplus.com/reference/cctype/toupper/


Hope all goes well :)
closed account (3CXz8vqX)
o.O interesting. Why not just store roll1 inside roll2, then reuse roll1?

Then compare whether roll1 == roll2 in order to ascertain victory.

So...
roll2;
if (roll1 == A_WINNING_ROLL ) roll2 = roll1;
roll again...
if roll2 == roll1 then WIN.
Last edited on
How would that look in code? To store another roll inside my first roll statement? Sorry I am new to this. I feel like I almost have it just missing something.
Last edited on
closed account (3CXz8vqX)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
        if (roll1 == 7 || roll1 == 11)
        {
            if (roll2 == 0) // A check to see if it has already been filled. This is not a good solution...
            { 
                roll2 = roll1;
            }
            else if ( roll 1 == roll 2 )
           {
                //Win
           }
            cout << "You win! Would you like to play again? [Y/N]:" << endl;
            cin >> repeat;
        }
Last edited on
Ok sweet!
How do I tell it to go on to roll2 if the number is 4, 5, 6, 8, 9 or 10 on roll1?
Because
1
2
3
4
5
6
cout << "Your roll was: " << die1 << " + " << die2 << " = " << roll1 << endl;
        if (roll1 == 7 || roll1 == 11)
        {
            cout << "You win! Would you like to play again? [Y/N]:" << endl;
            cin >> repeat;
        }


but if roll1 equals 4,5,6,8,9, or 10 it would do another roll to match that roll
I don't wan't to give the whole anser you won't learn that way. However, i think the code below will point you in the right direction

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
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <limits>
using namespace std;

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

    int die1, die2 = 0; 
    int roll1, roll2 = 0;
    char repeat = 'y';
   
    bool secondRoll = true;    // <-- this is new


    cout << "Welcome. The game is about to begin." << endl;

    while (repeat == 'y' || repeat == 'Y') 
    {
        cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
          
        secondRoll = true;    // <-- this is new
                                                                
        die1 = rand() % 6 + 1;
        die2 = rand() % 6 + 1; 
        roll1 = die1 + die2;

        cout << "Your roll was: " << die1 << " + " << die2 << " = " << roll1 << endl;
        if (roll1 == 7 || roll1 == 11)
        {
            cout << "You win! Would you like to play again? [Y/N]:" << endl;
            cin >> repeat;
            
            secindRoll = false    // < -- this is new
        }
        else if (roll1 == 2 || roll1 == 3 || roll1 == 12)
        {
            cout << "Sorry, you lose! Would you like to play again? {Y/N]:" << endl;
            cin >> repeat;
            
            secondRoll = false;   // <-- this is new
        } 

        //new while statement
        while(secondRoll)
        {
         
            //statements to calculate secondRoll
        }

   }

    return(0);
}

I hope this is what you were looking for and good luck with your programming
Last edited on
Topic archived. No new replies allowed.