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 ==========
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:
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 ==========
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....
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 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>------ 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 ==========
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?
// #include preprocessor directives
#include <iostream>
#include <cstdlib> // for using rand() function
#include <ctime> // for using time() function
// namespace directives
usingnamespace 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();
}
elseif (myRoll == 2 || myRoll == 3 || myRoll == 12){
cout << "Sorry you lose." << endl;
goAgain();
}
elseif (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();
}
elseif (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;
}
elseif (playAgain == 'Y'){
cout << "Would you like to play again?'" << endl;
return playAgain;
}
cin >> playAgain;
}
}
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.
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).