473,388 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Using functions or modules help

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.
Oct 19 '08 #1
3 2873
boxfish
469 Expert 256MB
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.
Oct 19 '08 #2
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
Oct 19 '08 #3
boxfish
469 Expert 256MB
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.
Oct 20 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
4
by: Leon | last post by:
Hi! Is it possibile to get names and types of functions, subs and variables included in modules and forms VB code? The reason - I must to create documentation of application. I would to...
7
by: Tim ffitch | last post by:
Hi I have created a VB dll file that contains common functions I use across various projects in VB, Access and Excel. Rather than have to code the functions in each I decided to use the dll...
13
by: Bijoy Naick | last post by:
My project contains multiple aspx pages. Many of these pages have code-behind that use several helper functions. Instead of copying each helper function into each aspx page, I am thinking of...
2
by: Martin v. Löwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version:...
9
by: sck10 | last post by:
Hello, I am trying to find the equivalent of LEN in C#: if (Len(this.ddlAudioDate.SelectedItem.Text) != 0) Any help would be appreciated. Thanks, sck10
4
by: wanwan | last post by:
Hi, I have a project with multiple c files. Some of these modules have functions that have the same name and their operations are exactly the same. My rusty memory tells me there is a way...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.