Build Errors Help

Pages: 12
Here is my code. Its a simple Craps Game for my programming class but I am getting some build errors that I keep getting stuck on


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
// ConsoleApplication6.cpp : Defines the entry point for the console application.
//

#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;
	int pointRoll;
	int anotherRoll;
    char repeat = 'y'; 

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

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

       
        if (roll1 == 7 || roll1 == 11)
			cout << "Your roll was: " << die1 << " + " << die2 << " = " << roll1 <<endl;
        {
           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;
        } 
		else if (roll1 == 4 || roll1 == 5 || roll1 == 6 ||
                   roll1 == 8 || roll1 == 9 || roll1 == 10) {
                                cout <<  die1 << " + " << die2 << " = " << roll1 <<endl;
                        pointRoll = roll1;
		}
		cout << "Are you ready to roll again? Press '1' when ready" << endl;
        cin >> anotherRoll;   
          
        if (anotherRoll == 1){ 
                  
           while (anotherRoll == 1)
           {                                
                         roll1 = die1 + die2; 
                         if (roll1 == pointRoll){
                           cout << "Congradulations, you Win." << endl;
                           return;
                         }
                         else if (roll1 == 7){
                           cout << "Sorry, 7 came up. You crapped out." << endl;
                           return;
                         }
                         else {
                           cout << "You're still in the game. 7 has not yet rolled." << endl;
                           cout << "Are you ready to roll again? Press '1' when ready" << endl;
                         }
                          cin >> anotherRoll; 
                 }
               
           } else {
                    cout << "Invalid entry" << endl;  
           }   
          
    }
    return(0);
}



1>------ Build started: Project: ConsoleApplication6, Configuration: Debug Win32 ------
1>  ConsoleApplication6.cpp
1>c:\users\\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(14): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(39): error C2181: illegal else without matching if
1>c:\users\\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(59): error C2561: 'main' : function must return a value
1>          c:\users\\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(12) : see declaration of 'main'
1>c:\users\\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(63): error C2561: 'main' : function must return a value
1>          c:\users\\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(12) : see declaration of 'main'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
1
2
3
4
5
6
if (roll1 == 7 || roll1 == 11)
/* Here*/		cout << "Your roll was: " << die1 << " + " << die2 << " = " << roll1 <<endl;
        {
           cout << "You win! Would you like to play again? [Y/N]:" << endl;
           cin >> repeat;
        }


You have a line of code before the block starts on your if statement, so the block executes AFTER the if whether it evaluated to true or not. Which also means your else if statement directly after that doesn't match up to your if statement since there was code in between them. Also try just:

 
return 0;

at the end of your program
line 33 move the braces on line 35 so it is after the if condition.

lines 59 & 63 return 0; main must return an int
Changed returns to repeats...
Now I am getting

1>------ Build started: Project: ConsoleApplication6, Configuration: Debug Win32 ------
1>  ConsoleApplication6.cpp
1>c:\users\sarah\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(14): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\sarah\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(39): error C2181: illegal else without matching if
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
TheIdeasMan wrote:
line 33 move the braces on line 35 so it is after the if condition.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (roll1 == 7 || roll1 == 11) { // <----- moved it to here
			cout << "Your roll was: " << die1 << " + " << die2 << " = " << roll1 <<endl;
        {
           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;
        } 
		else if (roll1 == 4 || roll1 == 5 || roll1 == 6 ||
                   roll1 == 8 || roll1 == 9 || roll1 == 10) {
                                cout <<  die1 << " + " << die2 << " = " << roll1 <<endl;
                        pointRoll = roll1;
		}


You should consider having an else clause to catch any bad input or errors.

warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data


This one is seemingly a little pedantic. Edit: Numbers default to int so, try this:

srand(time(NULL));

Or if you have C++11:

srand(time(nullptr));





Last edited on
Changed returns to repeats...

Don't know what that means.

Line 34 belongs BEFORE the if.

error C2181: illegal else without matching if

Your if is still messed up.


Last edited on
Wow thank you guys! So far it worked! Now I need to get certain things to display. I believe I can get this on my own though. I feel stupid that it was just those brackets....
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
// ConsoleApplication6.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <limits>

using namespace std;


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

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

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

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

       
        if (roll1 == 7 || roll1 == 11) {
			cout << "Your roll was: " << die1 << " + " << die2 << " = " << roll1 <<endl;
        
           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;
        } 
				else if (roll1 == 4 || roll1 == 5 || roll1 == 6 || roll1 == 8 || roll1 == 9 || roll1 == 10) 
		{
                        cout <<  die1 << " + " << die2 << " = " << roll1 <<endl;
                        pointRoll = roll1;
				}
		cout << "Are you ready to roll again? Press '1' when ready" << endl;
        cin >> anotherRoll;   
          
        if (anotherRoll == 1){ 
                  
           while (anotherRoll == 1)
           {                                
                         roll1 = die1 + die2; 
                         if (roll1 == pointRoll){
                           cout << "Congradulations, you Win." << endl;
                           cin >> repeat;
                         }
                         else if (roll1 == 7){
                           cout << "Sorry, 7 came up. You crapped out." << endl;
                          cin >> repeat;
                         }
                         else {
                           cout << "You're still in the game. 7 has not yet rolled." << endl;
                           cout << "Are you ready to roll again? Press '1' when ready" << endl;
                         }
                          cin >> anotherRoll; 
                 }
               
           } else {
                    cout << "Invalid entry" << endl;  
           }   
          
    }
    return 0;
}



Edited code. Got a new error when I run the program and I lose instantly.
It ask me to press Y then Press 1. Then the error comes
Run-Time Check Failure #3 - The variable 'pointRoll' is being used without being initialized.



Oh and when I run it and I hit one of the pointroll numbers it always wins instead of generating another random #
Last edited on
You can initialise variables to something at declaration - it may give a wrong answer but at least it won't crash.

Have you used a debugger? IF you have an IDE it should be easy - set up a watchlist of variables, see how the values change as you step through the code 1 line at a time. See where they go wrong & deduce the problem.

You can also put cout statements everywhere in you code to do the same thing. Start with a few to narrow down where the problem is.

Line 44 could just be an else rather than else if, because that would cover the remaining possibilities.

Are lines 52 to 74 necessary? You already have a while loop to play multiple times.
I had removed the lines like you suggested but I still need to have a secondary set of rolls when I roll either a 4 5 6 8 9 or 10 on roll 1.
Also I am using Visual Studio 2012 but I barely know how to use it.
I had removed the lines like you suggested but I still need to have a secondary set of rolls when I roll either a 4 5 6 8 9 or 10 on roll 1.


You can still code that logic into your loop with a RollCount variable.

Also I am using Visual Studio 2012 but I barely know how to use it.


Is that about the debugger? I have never used VS : Is there a debug option on the run Menu? Or use cout like I mentioned above.

If all else fails read the manual.
I use the debugger. The program essentially works. It just doesn't do what I need it to. Sucks being new at this. I can also step through my program line by line.
Well, then you should be able work out where it goes wrong, by looking at the values of the variables & seeing how the execution moves through the loops & if statements etc.
Assuming your last post of your code is still current, I see I few logic problems.

1) You should put your dice roll logic into a function. Add after line 10:
1
2
3
4
5
6
7
8
9
10
int roll_dice ()
{  int die1, die2; 
    int roll;

    die1 = rand() % 6 + 1;
    die2 = rand() % 6 + 1; 
    roll = die1 + die2;
    cout << "Your roll was: " << die1 << " + " << die2 << " = " << roll <<endl;
    return roll;
}

Delete line16 and 46.
Replace lines 28-30 with:
 
 roll1 = roll_dice();


2) regarding
The variable 'pointRoll' is being used without being initialized.

The only place I see that you've set pointroll is at line 47. You then check it at line 57. You can reach line 57 without going through line 47 resulting in an uninitialized variable.

3) Line 56. You supposedly roll the dice again, but you haven't changed the value of die1 or die2. Change line 56 to:
1
2
 
  roll1 = roll_dice();


4) Your whole roll again logic from lines 49-74 should be inside your else if at line 44. i.e. move 49-74 to after lne 47. You're prompting the user for a second roll even if they won or crapped out.

You're trying to combine additional rolls with playing the game again. This is what is confusing your logic. That should be separate logic.
Thanks AbstractionAnon, I did what you had said. However when II added the diceroll logic above int main() it still tells me that die1 and die2 are undefined. Why is that?


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
// ConsoleApplication6.cpp : Defines the entry point for the console application.
//


#include <iostream>
#include <ctime>
#include <limits>

using namespace std;

int roll_dice ()
{   int die1, die2; 
    int roll;

    die1 = rand() % 6 + 1;
    die2 = rand() % 6 + 1; 
    roll = die1 + die2;
    cout << "Your roll was: " << die1 << " + " << die2 << " = " << roll <<endl;
    return roll;
}
int main()
{
    srand(time(NULL)); 

  
    int roll1, roll2 = 0;
	int pointRoll;
	int anotherRoll;
    char repeat = 'y'; 

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

    while (repeat == 'y' || repeat == 'Y') 
    {
        cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
                                                                            
			roll1 = roll_dice();

       
        if (roll1 == 7 || roll1 == 11) {
			cout << "Your roll was: " << die1 << " + " << die2 << " = " << roll1 <<endl;
        
           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;
        } 
				else if (roll1 == 4 || roll1 == 5 || roll1 == 6 || roll1 == 8 || roll1 == 9 || roll1 == 10) 
		{
 
                        pointRoll = roll1;
								cout << "Are you ready to roll again? Press '1' when ready" << endl;
        cin >> anotherRoll;   
          
        if (anotherRoll == 1){ 
                  
           while (anotherRoll == 1)
           {                                
                         roll1 = roll_dice();
                         if (roll1 == pointRoll){
                           cout << "Congradulations, you Win." << endl;
                           cin >> repeat;
                         }
                         else if (roll1 == 7){
                           cout << "Sorry, 7 came up. You crapped out." << endl;
                          cin >> repeat;
                         }
                         else {
                           cout << "You're still in the game. 7 has not yet rolled." << endl;
                           cout << "Are you ready to roll again? Press '1' when ready" << endl;
                         }
                          cin >> anotherRoll; 
                 }
               
           } else {
                    cout << "Invalid entry" << endl;  
          
    }
    return 0;
}


1>------ Build started: Project: Project2, Configuration: Debug Win32 ------
1>  Source.cpp
1>c:\users\sarah\documents\visual studio 2012\projects\project2\project2\source.cpp(23): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\sarah\documents\visual studio 2012\projects\project2\project2\source.cpp(84): fatal error C1075: end of file found before the left brace '{' at 'c:\users\sarah\documents\visual studio 2012\projects\project2\project2\source.cpp(35)' was matched
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
Ok guys last thing I need help with
I need help with when I go to play again it jumps town to char goAgain. I can't seem to get this function to work properly. Any tips?


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
// #include preprocessor directives
#include <iostream>
#include <cstdlib>            // for using rand() function
#include <ctime>                // for using time() function

// namespace directives
using namespace std ;

void CrapsIntro() ;               // brief description of Craps game
int  CrapsRoll() ;                  // outcome of two six-sided dice rolls      // Function for if chooses passline
char goAgain();         // option if user wants to play again


 int main()
{

           int myRoll;       
           int pointRoll;   // If user hits a "point", it will be stored here
           int anotherRoll;
		   char repeat = 'y'; //when declaring a char with a letter you have to put ' ' around it
           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.
           }   
          
    }

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
        srand(int(time(0))) ;            // seeding random number generator
        randomNumber = rand() ;   // generate random number
        dieOne = (randomNumber % 6) + 1 ;       // a number between 1 and 6
       
        // die two
        srand(int(time(0))) ;            // seeding random number generator
        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 playAgain;
        }
         cin >> playAgain;
        }
}
I said to delete what is now line 41. There is no reason to display the dice roll there since you now display what was rolled in roll_dice().

At line 81, you're missing two }. One to match the if at line 58 and one to match the while at line 33.
I changed it a bit, using the information you provided me. It helped a lot! I changed some of the int names and stuff. You helped me get on the right track. Much appreciated.

I posted my new code above you. I am having a new problem though. I think it might be one of my cin >> or I am not directing the goAgain function properly.
Last edited on
You should have only one call to srand() in your program. It should be at the start of main (after line 20). You don't want to seed the random number generator each time you roll the dice (lines 81, 86).
Pages: 12