Build Errors Help

Pages: 12
Moved my srand() to line 21. Removed it in 81,86. I see why I don't want it to seed each time. Thank you.

How about my goAgain function? Do you see anything wrong with it? When I want it to play a new game it doesn't return. It plays off the same game I had previous.
I see why I don't want it to seed each time

Because by seeding it with time(), time only returns the time to the current second. Therefore, if you do the following:
1
2
3
4
5
6
 
  int r1, r2;
  srand(time(NULL));
  r1 = rand(); 
  srand(time(NULL));
  r2 = rand();

r1 and r2 will be identical. i.e. the random number generator will be seeded with a value that is unique only to the second. In the example, if the two srand() calls occur in the same second (highly likely), the random number generator will be seeded with the same value. Each call to rand() will return the first number in the sequence, which will be the same number.
Guess I should have added some comas in that sentence. Haha but thank you.
I was also wondering about my goAgain function. How come when it goes down to goAgain it doesn't return to CrapsRoll? or int main ()?
Did I mess that up some how?

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
char goAgain()
        /* This function prompts the user if they would
        like to play again. They can either press
        'Y' for yes or 'N' for no.
        */

        // Pre-Condition: None
        // Post-Condition: If 'Y' || 'N' is entered,
        // it sends back to the caller and enters a loop.
{
       cout << "Would you like to play again?" << endl;
           cout << "Enter 'Y' for Yes or 'N' for No." << endl;
           char playAgain;
           cin >> playAgain;

           while (playAgain) {
                   if (playAgain != 'Y' && playAgain != 'N'){
                cout << "Invalid entry. Please select 'Y' or 'N'. (Capitals)" << endl;
           }
           if (playAgain == 'N'){
                   cout << "Thank you for playing. Good Bye" << endl;
               return playAgain;
           }
        else if (playAgain == 'Y'){
                   cout << "Would you like to play again?'" << endl;

                   return 0;
        }
         cin >> playAgain;
        }
}
I see why I don't want it to seed each time

I misread that sentence. Oh well. No harm.

I was also wondering about my goAgain function. How come when it goes down to goAgain it doesn't return to CrapsRoll? or int main ()?


Going by the code near the bottom of page 1, you still have a number of logic errors.
Line 36. You prompt the user "Are you ready to roll again?" regardless of whether then won, lost or rolled a point number. I believe I previously told you to put all of your pointroll logic after line 34 (inside the if 4,5,6,8,9,10).

You're still intermixing the second roll and playing again. Those should be completely separate. Consider the following simple loop in main:
1
2
3
4
5
 
  do 
  {  do_turn();  // Handle one complete turn
  }
  while (goAgain() == 'Y');


Then move lines 21-61 to new function do_turn().
Instead of calling goAgain() in multiple places causing logic problems, simply return out of do_turn when the turn is over either due to a win, loss or crapping out.

In the version of goAgain() immedialy above, you should be returning 'Y' at line 27, although in your current code you never check the result of goAgain.
Lets hope I can get the information you provided me right. Ill post back with results. Thank you! Learning C++ is more of a pain than Java.
So something like this?


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
// Program Assignment #2
// Programmed by
// Date:


#include <iostream>
#include <cstdlib>            // for using rand() function
#include <ctime>                // for using time() function

// namespace directives
using namespace std ;

int  CrapsRoll() ;                  // outcome of two six-sided dice rolls
char goAgain();         // option if user wants to play again


 int main()
{
	       void CrapsIntro();
           int myRoll;       
           int pointRoll;   // If user hits a "point", it will be stored here
           int anotherRoll;
		   char repeat = 'Y'; // the repeat function to correspond with the 'Y' key
		   srand(int(time(0))) ; // seeding random number generator

		   do
		   {  do_turn();

		   CrapsIntro();

		   myRoll = CrapsRoll(); // Uses function CrapsRoll() to genereate a random number

           if (myRoll == 7 || myRoll == 11){
                   cout << "You win." << endl;
               goAgain();
           }
           else if (myRoll == 2 || myRoll == 3 || myRoll == 12){
                   cout << "Sorry you lose." << endl;
					goAgain();
           }
           else if (myRoll == 4 || myRoll == 5 || myRoll == 6 ||
                   myRoll == 8 || myRoll == 9 || myRoll == 10) {
                        cout << "The number you rolled is a point number." << endl;
                        pointRoll = myRoll;

						cout << "Are you ready to roll again? Press '1' when ready" << endl;
						cin >> anotherRoll;   // Roll again after point is hit
          
           if (anotherRoll == 1){  // If it is true (User entered 1)
                  
             while (anotherRoll == 1)         // If it doesn't hit point or 7,
             {                                // loop is used until it does.
                         myRoll = CrapsRoll();  // dice roll
                         if (myRoll == pointRoll){
                           cout << "Congradulations, you Win." << endl;
                            goAgain();
                         }
                         else if (myRoll == 7){
                           cout << "Sorry, 7 came up. You crapped out." << endl;
                            goAgain();
                         }
                         else {
                           cout << "You're still in the game." << endl;
                           cout << "Are you ready to roll again? Press '1' when ready" << endl;
                         }
                          cin >> anotherRoll;  //go back to the loop to roll again
                 }
               
           } else {
                    cout << "Invalid entry" << endl;  // User did not enter 1.
           }
		   while (goAgain() == 'Y');     
    }

int CrapsRoll()
    /* The purpose of this function is to create
    a random roll for two die and return to the
    caller. It has another function inside for
    time delay.
    */

        // Pre-Condition: None
        // Post-Condition: Send back to the caller
        // the sum of two die's being randomly rolled
{
        int randomNumber ;                  // a random number
        int dieOne ;                // a six-sided die roll value
        int dieTwo ;                // a six-sided die roll value
       
        // die one
        randomNumber = rand() ;   // generate random number
        dieOne = (randomNumber % 6) + 1 ;       // a number between 1 and 6
       
        // die two
        randomNumber = rand() ;   // generate random number
        dieTwo = (randomNumber % 6) + 1 ;       // a number between 1 and 6

        cout << "You rolled a " << dieOne + dieTwo << endl ;
       
        return dieOne + dieTwo ;
}

char goAgain()
        /* This function prompts the user if they would
        like to play again. They can either press
        'Y' for yes or 'N' for no.
        */

        // Pre-Condition: None
        // Post-Condition: If 'Y' || 'N' is entered,
        // it sends back to the caller and enters a loop.
{
       cout << "Would you like to play again?" << endl;
           cout << "Enter 'Y' for Yes or 'N' for No." << endl;
           char playAgain;
           cin >> playAgain;

           while (playAgain) {
                   if (playAgain != 'Y' && playAgain != 'N'){
                cout << "Invalid entry. Please select 'Y' or 'N'. (Capitals)" << endl;
           }
           if (playAgain == 'N'){
                   cout << "Thank you for playing. Good Bye" << endl;
               return playAgain;
           }
        else if (playAgain == 'Y'){
                   cout << "Would you like to play again?'" << endl;

                   return 0;
        }
         cin >> playAgain;
        }
}

void CrapsIntro()


{
	 cout<< "          Welcome to the Dice game Craps!" << endl;
	 cout <<  "A player rolls two dice. Each die has six faces. These faces contain" << endl;
     cout <<  "1, 2, 3, 4, 5 and 6 spots. After the dice have come to rest, the sum" << endl;
     cout <<  "of the spots on the two upward faces is calculated. If the sum is 7" << endl;
     cout <<  "or 11 on the first roll, the player wins. If sum is 2, 3 or 12 on the" << endl;
     cout <<  "first roll (called Craps), the player loses (i.e. the house" << endl;
     cout <<  "wins). If the sum is 4, 5, 6, 8, 9 or 10 on the first roll, then that" << endl;
     cout <<  "sum becomes the player's point. To win, you must continue rolling" << endl;
     cout <<  "the dice until you make your points. The player loses by rolling" << endl;
     cout <<  "a 7 before making the point." << endl;
	 cout << "                              " << endl;
}
I see the call to do_turn at line 27, but where is do_turn?

The call to CrapsIntro() (line 29) belongs before the do while loop. You don't want to repeat the intro on every turn.

You're still returning 0 at line 129 instead of 'Y'.

You still have calls to goAgain scattered everywhere causing problems.

So replace the calls for goAgain to do_turn?
Sorry I am uneducated with this. Its only my second program.
Also what do you mean by "where is do_turn?" I think I miss understood you when you told me to do this loop instead.
So replace the calls for goAgain to do_turn?

No. I said to put the logic from lines 31-72 (most recent post) into the new function.

Also what do you mean by "where is do_turn?"

A said to add a new function. You added a loop to call the new function, but you never implemented the new function.

This is what I had in mind:
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
int main()
{   srand(int(time(0))) ; // seeding random number generator
    
    CrapsIntro();
    do 
    {  CrapsTurn();
    } while (goAgain());
    return 0;
}

void CrapsTurn ()
{   int myRoll;       
    int pointRoll;   // If user hits a "point", it will be stored here
    int anotherRoll;
	   
    myRoll = CrapsRoll();   //  First roll
    if (myRoll == 7 || myRoll == 11)
    {   cout << "You win." << endl;
        return;     //  Done with turn
    }
    if (myRoll == 2 || myRoll == 3 || myRoll == 12)
    {   cout << "Sorry you lose." << endl;
        return;     //  Done with turn
    }
    //  Can only be 4,5,6,8,9,10
    cout << "The number you rolled is a point number." << endl;
    pointRoll = myRoll;
    	         
    while (1)       //  keep rolling until a win or a crap out                       
    {   cout << "Are you ready to roll again? Press '1' when ready" << endl;
	    cin >> anotherRoll;         // Roll again after point is hit
        if (anotherRoll != 1) 
        {   cout << "Invalid entry" << endl;
            continue;               //  Next iteration of the loop
        }
        myRoll = CrapsRoll();  // dice roll
        if (myRoll == pointRoll)
        {   cout << "Congradulations, you Win." << endl;
            return;    // Done with turn
        }
        if (myRoll == 7)
        {   cout << "Sorry, 7 came up. You crapped out." << endl;
            return;     // done with turn
        }
        cout << "You're still in the game." << endl;
    }
}

bool goAgain()
    /* This function prompts the user if they would
       like to play again. They can either press
       'Y' for yes or 'N' for no.
    */

    // Pre-Condition: None
    // Post-Condition: If 'Y' || 'N' is entered,
    // it sends back to the caller and enters a loop.
{   char playAgain;

    while (1) 
    {   cout << "Would you like to play again?" << endl;
        cout << "Enter 'Y' for Yes or 'N' for No." << endl;
        cin >> playAgain;
        if (playAgain == 'N')
        {   cout << "Thank you for playing. Good Bye" << endl;
            return false;
        }
        if (playAgain == 'Y')
            return true;
        cout << "Invalid entry." << endl;
    }
}

I've omitted CrapsIntro() and CrapsRoll() as they are fine.
Last edited on
Topic archived. No new replies allowed.
Pages: 12