I am using visual studio on windows XP.
In my code I've defined a local variable in a function but when I run the program and it gets to that function it says I have an undeclared variable.
The code for the program is as follows:
Expand|Select|Wrap|Line Numbers
- // #include preprocessor directives
- #include <iostream>
- #include <cstdlib> // for using rand() function
- #include <ctime> // for using time() function
- #include <string>
- // namespace directives
- using namespace std ;
- void CrapsIntro() ; // brief description of Craps game
- int CrapsRoll() ; // outcome of two six-sided dice rolls
- void myTimeDelay(int) ; // time delay between rolls
- void playLineFunction(int);
- void dontPlayLineFunction();
- // functionA prototype
- // functionB prototype
- // other function prototypes
- // . . .
- const int DELAY = INT_MAX / 2 ; // manual delay between rolls
- int main ()
- {
- // variable declaration
- int myRoll;
- char bettingMode; //used for choosing a betting mode
- char playAgain; //Y or N to play again
- int playLoop; //used for making the loop to play the game again
- // variable initializations
- // Display game instructions
- CrapsIntro() ;
- playLoop = 1;
- while (playLoop > 0)
- {
- cout << "Which betting mode would you like to play, Pass Line, or Don't Pass Line?" << endl;
- cout << "Type P for Pass Line or D for Don't Pass Line" << endl; // Ask user to play Pass Line OR Don't Pass Line
- cin >> bettingMode;
- if (bettingMode == 80)// ASCII code for P
- {
- cout << "You chose to play Pass Line" << endl;
- myRoll = CrapsRoll();
- playLineFunction(myRoll);
- }
- if (bettingMode == 68)// ASCII code for D
- {
- cout << "You chose to play Don't Pass Line" << endl;
- myRoll = CrapsRoll();
- }
- if (bettingMode != 80 && bettingMode != 68) //If the user inputs a character other than P or D
- { //it ends the program for an invalid play mode
- cout << "You have chosen an invalid game mode" << endl;
- return 1;
- }
- cout << "Do you wish to play again? Type Y or N" << endl; // Ask user if they want to play another game
- cin >> playAgain;
- if (playAgain == 78) //ASCII code for Y
- {
- cout << "Thank you for playing" << endl;
- playLoop--; // Decrement playLoop so that it exits the loop
- }
- }
- return 0;
- }
- // Play Line Function
- // This function tests the conditions and rules
- // in order to play the Play Line mode of Craps
- void playLineFunction(int myRoll)
- {
- int POINT;
- if (myRoll == 7 || myRoll == 11)
- {
- cout << "You win!" << endl;
- }
- if (myRoll == 2 || myRoll == 3 || myRoll == 12)
- {
- cout << "You lose! Crapped out" << endl;
- }
- else
- {
- myRoll = POINT;
- cout << "You've established a point of " << POINT << endl;
- }
- while (myRoll != 7)
- {
- myRoll = CrapsRoll();
- if (myRoll = POINT)
- {
- cout << "You win!" << endl;
- }
- }
- cout << "You Lose! Seven out" << endl;
- }
- // functionB definition
- // Provide function description comment block, preconditions, and
- // postconditions
- // . . .
- // The purpose of this function is to give an intorduction to the user
- // about the program
- void CrapsIntro()
- {
- cout << "This is my Craps game. You will need to choose a playing mode of Craps, " << endl ;
- cout << "and then proceed following the basic rules of Craps to win or lose." << endl << endl;
- }
- // Provide function description comment block, preconditions, and
- // postconditions
- void myTimeDelay(int delay)
- {
- int count = 0 ;
- while (delay > count)
- {
- // do nothing delay period
- count++;
- }
- }
- // Provide function description comment block, preconditions, and
- // postconditions
- int CrapsRoll()
- {
- int randomNumber ; // a random number
- int dieOne ; // a six-sided die roll value
- int dieTwo ; // a six-sided die roll value
- myTimeDelay(DELAY) ;
- cout << "Rolling my two dice ..." << endl ;
- // 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
- myTimeDelay(DELAY) ;
- // 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 ;
- }
I don't understand why I am getting this error. Any assistance is appreciated.