Connecting Tech Pros Worldwide Forums | Help | Site Map

Using functions or modules help

Newbie
 
Join Date: Oct 2008
Posts: 12
#1: Oct 19 '08
I have to write a program that lets the user play the game of Craps. As of right now it is incomplete and I have run into some errors. It's my first time using functions instead of the program all being in the int main().

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
  1. // #include preprocessor directives
  2. #include <iostream>
  3. #include <cstdlib>        // for using rand() function
  4. #include <ctime>            // for using time() function
  5. #include <string>
  6.  
  7. // namespace directives
  8. using namespace std ;
  9.  
  10. void CrapsIntro() ;            // brief description of Craps game 
  11. int  CrapsRoll() ;            // outcome of two six-sided dice rolls
  12. void myTimeDelay(int) ;            // time delay between rolls
  13. void playLineFunction(int);
  14. void dontPlayLineFunction();
  15. // functionA prototype
  16. // functionB prototype
  17. // other function prototypes
  18. // . . .
  19.  
  20. const int DELAY = INT_MAX / 2 ;    // manual delay between rolls
  21.  
  22.  
  23. int main ()
  24. {
  25.     // variable declaration
  26.     int myRoll;
  27.     char bettingMode; //used for choosing a betting mode
  28.     char playAgain; //Y or N to play again
  29.     int playLoop; //used for making the loop to play the game again
  30.     // variable initializations
  31.  
  32.     // Display game instructions
  33.     CrapsIntro() ;
  34.     playLoop = 1;
  35.  
  36. while (playLoop > 0)
  37. {
  38.     cout << "Which betting mode would you like to play, Pass Line, or Don't Pass Line?" << endl;
  39.     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
  40.     cin >> bettingMode;
  41.     if (bettingMode == 80)// ASCII code for P
  42.     {
  43.         cout << "You chose to play Pass Line" << endl;
  44.         myRoll = CrapsRoll();
  45.         playLineFunction(myRoll);
  46.     }
  47.     if (bettingMode == 68)// ASCII code for D
  48.     {    
  49.         cout << "You chose to play Don't Pass Line" << endl;
  50.         myRoll = CrapsRoll();
  51.     }
  52.     if (bettingMode != 80 && bettingMode != 68) //If the user inputs a character other than P or D 
  53.     {                                           //it ends the program for an invalid play mode
  54.         cout << "You have chosen an invalid game mode" << endl;
  55.         return 1;
  56.     }
  57.     cout << "Do you wish to play again? Type Y or N" << endl; // Ask user if they want to play another game
  58.     cin >> playAgain;
  59.     if (playAgain == 78) //ASCII code for Y
  60.     {    
  61.         cout << "Thank you for playing" << endl;
  62.         playLoop--; // Decrement playLoop so that it exits the loop
  63.     }
  64.  
  65. }
  66.     return 0;
  67. }
  68.  
  69. // Play Line Function
  70. // This function tests the conditions and rules
  71. // in order to play the Play Line mode of Craps
  72. void playLineFunction(int myRoll)
  73. {
  74.     int POINT;
  75.  
  76.     if (myRoll == 7 || myRoll == 11)
  77.     {
  78.         cout << "You win!" << endl;
  79.     }
  80.     if (myRoll == 2 || myRoll == 3 || myRoll == 12)
  81.     {
  82.         cout << "You lose! Crapped out" << endl;
  83.     }
  84.     else 
  85.     {
  86.         myRoll = POINT;
  87.         cout << "You've established a point of " << POINT << endl;
  88.     }
  89.     while (myRoll != 7)
  90.     {
  91.         myRoll = CrapsRoll();
  92.         if (myRoll = POINT)
  93.         {
  94.             cout << "You win!" << endl;
  95.  
  96.         }
  97.     }
  98.     cout << "You Lose! Seven out" << endl;
  99. }
  100.  
  101.  
  102. // functionB definition
  103. // Provide function description comment block, preconditions, and 
  104. // postconditions
  105.  
  106. // . . .
  107.  
  108. // The purpose of this function is to give an intorduction to the user 
  109. // about the program
  110. void CrapsIntro()
  111. {
  112.     cout << "This is my Craps game. You will need to choose a playing mode of Craps, " << endl ;
  113.     cout << "and then proceed following the basic rules of Craps to win or lose." << endl << endl;
  114. }
  115.  
  116. // Provide function description comment block, preconditions, and 
  117. // postconditions
  118. void myTimeDelay(int delay)
  119. {
  120.     int count = 0 ;
  121.     while (delay > count)
  122.     {
  123.         // do nothing delay period
  124.         count++;
  125.     }
  126. }
  127.  
  128. // Provide function description comment block, preconditions, and 
  129. // postconditions
  130. int CrapsRoll()
  131. {
  132.     int randomNumber ;            // a random number
  133.     int dieOne ;                // a six-sided die roll value
  134.     int dieTwo ;                // a six-sided die roll value
  135.  
  136.     myTimeDelay(DELAY) ;
  137.  
  138.     cout << "Rolling my two dice ..." << endl ;
  139.  
  140.     // die one
  141.     srand(int(time(0))) ;                // seeding random number generator 
  142.     randomNumber = rand() ;            // generate random number
  143.     dieOne = (randomNumber % 6) + 1 ;    // a number between 1 and 6
  144.  
  145.     myTimeDelay(DELAY) ;
  146.  
  147.     // die two
  148.     srand(int(time(0))) ;                // seeding random number generator 
  149.     randomNumber = rand() ;            // generate random number
  150.     dieTwo = (randomNumber % 6) + 1 ;    // a number between 1 and 6
  151.  
  152.     cout << "You rolled a " << dieOne + dieTwo << endl ;
  153.  
  154.     return dieOne + dieTwo ;
  155. }
  156.  
The program compiles correctly with no errors, but when it is run, I get the error that POINT is an undeclared variable. POINT is declared inside the void function playLineFunction (line 74) that I initialized in the main function.

I don't understand why I am getting this error. Any assistance is appreciated.

boxfish's Avatar
Expert
 
Join Date: Mar 2008
Location: California
Posts: 478
#2: Oct 19 '08

re: Using functions or modules help


Maybe it says POINT is uninitialized, not undeclared?
Expand|Select|Wrap|Line Numbers
  1. myRoll = POINT;
  2. cout << "You've established a point of " << POINT << endl;
You have never given POINT any value, so it will be completely random. For me it was 2088810195.
I didn't take the time to understand the logic of your code, but it would make more sense to me to write
Expand|Select|Wrap|Line Numbers
  1. POINT = myRoll;
  2. cout << "You've established a point of " << POINT << endl;
but that's just my wild guess.
Hope this helps anyway.
Newbie
 
Join Date: Oct 2008
Posts: 12
#3: Oct 19 '08

re: Using functions or modules help


Sorry you were correct the error I get was an initializing error and not a declaration error.

I set it now to:
int POINT = 0;
And I had the other 2 reversed like you said. Now POINT = myRoll

I no longer get the error but I've run into trouble with the nested If statement within the while statement. Here are the changes I made (I only made changes to the playLineFunction)
Expand|Select|Wrap|Line Numbers
  1. void playLineFunction(int myRoll)
  2. {
  3.     int POINT = 0;
  4.  
  5.     if (myRoll == 7 || myRoll == 11)
  6.     {
  7.         cout << "You win!" << endl;
  8.     }
  9.     if (myRoll == 2 || myRoll == 3 || myRoll == 12)
  10.     {
  11.         cout << "You lose! Crapped out" << endl;
  12.     }
  13.     else 
  14.     {
  15.         POINT = myRoll;
  16.         cout << "You've established a point of " << POINT << endl;
  17.     }
  18.     while (myRoll != 7)
  19.     {
  20.         myRoll = CrapsRoll();
  21.         if (myRoll = POINT)
  22.         {
  23.             cout << "You win!" << endl;
  24.  
  25.         }
  26.     }
  27.     cout << "You Lose! Seven out" << endl;
  28. }
  29.  
Ideally when the program runs it sets POINT equal to myRoll. While myRoll does not equal to 7 (7 means you've lost after you've established a point) myRoll calls the CrapsRoll function and gets a new value. Then it goes to the nested if statement saying you win if myRoll is equal to POINT.
The problem is that the program is saying "You win!" even when myRoll doesn't equal POINT also, it does not exit the while loop when myRoll is 7. So as of now it's in an infinite loop. I think this has to do with line 20 where it calls the CrapsRoll function.
I know I've set POINT = myRoll which would cause an infinteloop of "You win!" but it should be getting a new value for myRoll from the CrapsRoll function which would cause it loop until it rolls a 7 or it rolls the value of POINT.

I think that explains my new problem. Thanks for the help before boxfish
boxfish's Avatar
Expert
 
Join Date: Mar 2008
Location: California
Posts: 478
#4: Oct 20 '08

re: Using functions or modules help


Quote:

Originally Posted by TamaThps

also, it does not exit the while loop when myRoll is 7. So as of now it's in an infinite loop.

You wrote
if (myRoll = POINT)
You assign POINT to myRoll instead of comparing them. Use
if (myRoll == POINT)
Hope this helps.
Reply