Thread: Help with BlackJack

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    17

    Help with BlackJack

    I just started programming in c++. I wanted to make a BlackJack game this morning. I used the random number generator shown in the faq, yet I still get some errors that I dont know how to fix. I am using visual c++ 6.

    Code:
    //BlackJack by Jontay
    //Started 6-25-03
    //Special Thanks to www.Cprogramming.com for their Random Number Generator
    
    #include "iostream.h"
    #include "ctime.h"
    
    int CardValue,Total,Money=500,Timer,Bet,Ac,DTotal;
    
    int AceChoose();
    {
    	cout<<"Would you like your Ace to be 1 or 11?"<<endl;
    	cin>>Ac;
    	
    	if (Ac = 1 || 11);
    		return Ac;
    
    	return 1;
    
    }
    
    int GetRand(int min,int max)
    {
      static int Init = 0;
      int rc;
      
      if (Init == 0)
      {
        /*
         *  As Init is static, it will remember it's value between
         *  function calls.  We only want srand() run once, so this
         *  is a simple way to ensure that happens.
         */
        srand(time(NULL));
        Init = 1;
      }
    
      /*
       * Formula:  
       *    rand() % N   <- To get a number between 0 - N-1
       *    Then add the result to min, giving you 
       *    a random number between min - max.
       */  
      rc = (rand() % (max - min + 1) + min);
      
      return (rc);
    }
    
    int GetCard()
    {
    	int temp = GetRand(1,13)
    
    	switch (temp)
    	{
    	
    		case 1:CardValue = AceChoose();
    		        return CardValue;
    		
    		case 2:CardValue = 2;
    			    return CardValue;
    		
    		case 3:CardValue = 3;
    				return CardValue;
    		
    		case 4:CardValue = 4;
    				return CardValue;
    	
    		case 5:CardValue = 5;
    				return CardValue;
    		
    		case 6:CardValue = 6;
    				return CardValue;
    		
    		case 7:CardValue = 7;
    				return CardValue;
    
    		case 8:CardValue = 8;
    				return CardValue;
    
    		case 9:CardValue = 9;
    				return CardValue;
    
    		case 10:CardValue = 10;
    				return CardValue;
    
    		case 11:CardValue = 10;
    				return CardValue;
    
    		case 12:CardValue = 10;
    				return CardValue;
    
    		case 13:CardValue = 10;
    				return CardValue;
    	}
    
    	return 1;
    
    }
    
    int Hit();
    {
    	Total = Total + GetCard();
    	
    	if (Total > 21)
    	{
    		cout<<"You busted! :-("<<endl;
    		cout<<"Press "1" to continue"<<endl;
    		cin<<Ac
    		
    			if (Ac = 1); 
    				main();
    
    			else 
    				break;
    	}
    	
    	return 0;
    }
    
    int Stay();
    {
    	DTotal = GetCard();
    	
    	while (DTotal <= 16)
    	{
    		DTotal = DTotal + GetCard();
    		if (DTotal > 21)
    		{
    			cout<<"Dealer Busted, You win"
    			Money = Money + (Bet * 2)
    			cout<<"Press "1" to continue"<<endl;
    			cin<<Ac
    		
    			if (Ac = 1); 
    				main();
    
    			else 
    				break;
    		}
    	}
    
    	if (Total > DTotal)
    	{
    		cout<<"You Win"<<endl;
    		Money = Money + (Bet * 2)
    		cout<<"Press "1" to continue"<<endl;
    		cin<<Ac
    		
    		if (Ac = 1); 
    				main();
    
    		else 
    			break;
    	}
    
    	else
    	{
    		cout<<"Sorry, You lose"
    		cout<<"Press "1" to continue"<<endl;
    		cin<<Ac
    		
    		if (Ac = 1); 
    				main();
    
    		else 
    			break;
    	}
    
    	return 1;
    }
    		
    
    
    int main();
    {
    	cout<<"Money:"<<Money<<endl;
    	Total = GetCard()
    	cout<<"Your total is "<<Total<<endl;
    	cout<<"Please place your bet"<<endl;
    	cin<<Bet;
    
    	Money = Money - Bet;
    
    	cout<<"Here's your second card"<<endl;
    	cout<<"Your total is "<<Total<<endl;
    	
    	if (total = 21);
    	{
    		cout<<"BLACKJACK!";
    		Money = Money + (Bet * 4);
    		cout<<"Press "1" to continue"<<endl;
    		cin<<Ac
    		
    			if (Ac = 1); 
    				main();
    
    			else 
    				break;
    	}
    
    	
    	for (Timer = 0;Timer = 10 ; Timer++);
    	{
    		cout<<"Press 1 to Hit or 2 to stay"<<endl;
    		cin>>Ac;
    
    		if (Ac = 1);
    			Hit();
    		else 
    			Stay();
    	}
    
    	return 1;
    
    }
    Also I know I could probably do better with the structure, but right now I'm interested in why it won't compile. Here are the errors.

    BlackJackbyJontay.cpp
    D:\VC98\Source\BlackJackbyJontay.cpp(11) : error C2447: missing function header (old-style formal list?)
    D:\VC98\Source\BlackJackbyJontay.cpp(34) : error C2065: 'srand' : undeclared identifier
    D:\VC98\Source\BlackJackbyJontay.cpp(34) : error C2065: 'time' : undeclared identifier
    D:\VC98\Source\BlackJackbyJontay.cpp(44) : error C2065: 'rand' : undeclared identifier
    D:\VC98\Source\BlackJackbyJontay.cpp(53) : error C2143: syntax error : missing ';' before 'switch'
    D:\VC98\Source\BlackJackbyJontay.cpp(101) : error C2447: missing function header (old-style formal list?)
    D:\VC98\Source\BlackJackbyJontay.cpp(121) : error C2447: missing function header (old-style formal list?)
    D:\VC98\Source\BlackJackbyJontay.cpp(175) : error C2447: missing function header (old-style formal list?)
    Error executing cl.exe.

    Please Help. Thanks.
    Last edited by Jontay; 06-25-2003 at 08:53 AM.

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Only briefly looked through your code. To solve most of your problems, make sure you include the line
    Code:
    using namespace std;
    somewhere near the top.

    The errors saying missing function header is due to you putting semicolons after you declare the function name. So change
    Code:
    int AceChoose();
    to
    Code:
    int AceChoose()
    and there are several other times you did this as well so make sure you change those too.

    And last, your compiler won't catch this, but you're program won't run correctly because of this syntax error: make sure that you use == instead of just = when comparing variables. Change
    Code:
    if (Ac = 1 || 11);
    to
    Code:
    if (Ac == 1 || Ac==11)
    Notice you also had a semicolon after the if statement which shouldn't have been there. You don't put semicolons after if or loop statements unless you want them to run without doing any other commands. You did that in a few places and you'll find that your program won't run correctly because of it.

    Also, I'm sure some people here will get on your case about repeatedly calling main() inside your program. See if you can find a way to loop instead of recursively calling your program over and over again.
    Last edited by PJYelton; 06-25-2003 at 09:05 AM.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    17
    Thanks for all that. I'll correct everything you pointed out and see if it runs. About calling main() again, I didn't know if was considered bad coding practice or not, but I thought it would be better than using goto's.

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    You don't need to use gotos or recall main. Just use a loop. Something like this:
    Code:
    do
    {
        // print money etc
        // if user doesn't have a blackjack, allow hit or stand until bust or done
        // else congratulate for blackjack
        // ask if user wants to play again
    }  while (/* user says yes to playing again*/);

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    17
    I got it working now. Thanks for the help. Now I'll just make the code easier to understand from here on out. I really appreciate it.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Couple things about your includes:
    - enclose include files that the compiler knows about in < > not " "
    - iostream.h is an old deprecated header - iostream (with no .h) is standard
    - ctime.h doesn't (or shouldn't) exist - time.h is from the Standard C library (no namespace), and ctime is the same thing in the std namespace provided by the Standard C++ library
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    17
    I took it straight from this in the FAQ. I assume it was a typo.

    The changes needed to make the previous example into a C++ program are minimal, only the headers and main need to change, as is shown here:


    Code:
    #include <iostream> 
    #include <ctime> 
    
    int main(void)
    {
      int i, r;
    
      for (i = 0; i < 20; i++)
      {
        r = GetRand(10, 12);
        std::cout <<"Your number is " <<r <<std::endl;
      }
    
      return(0);
    }

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Notice that there is no '.h' on either of those though.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Exclamation HINT

    Don't try to write the whole program and compile. It's very hard to debug when you have multiple errors.

    Experienced programmers test-compile and test-run often during program "development". (It IS good practice to plan-out your program completely using a flow chart or psudo code.)
    Most programmers will test-compile (and test-run if possible) every function they write before moving-on to the next function. Of course, if it's a complex function they will test before the function is complete.

    Beginners should compile as soon as there is enough to compile... I mean you have to have "complete" functions with brackets and the return value if not void, etc. The function does not have to do anything for you to test-compile. Then, you should re-compile every line or two.

    You should also test each function as soon as there is enough code to do something. Sometimes it's helpful to add debug cout statements to let you know what's going on (i.e. cout << "In Hit() function".)

    It takes some practice to learn how to sequence your programming so that you can test-comipile and test-run, but programming is a lot more fun if you only have to work on one or two errors at a time!

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    15
    Well, actually, that's only SOMEwhat true. Compiling often is the quickest way to get a project done, imo, but at the same time, it doesn't allow you to catch your own errors and thus become a better programmer.

    (I should say that this is more useful for a somewhat experienced programmer than a just starting programmer)

    For example, as of late (the last year) I have been writing my programs to completion, before compiling. once or twice a day, I look over everything to check my semi-colons (the bane of my existence) and syntax and logic. then I continue programming.

    This does a few things for you - you review your code often, meaning you're more likely to remember where that one offending line is that's screwing it all up.
    you're reading code and quickly assembling in your mind what it does (granted, you DID write it). Like they say, practice makes perfect.

    Not to mention, as you are going over your program in your mind during the day/night, you might up and realize - "Hey! I screwed up!" and when you do, you can fix it yourself before you compile. Yet another usefull side effect of this technique.

    This is also the technique my college teaches. I find it rather usefull, and I know it has helped me with my programming.

    All that being said, there is a line at which this excercise becomes useless. HUGE programs should NOT be compiled in this way.

    well, that's my ten point five cents

    - SB

    Oh, by the way, there is a name for the programming process that this technique is taken from, if I can remember, I'll post it.

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    15

    Lightbulb Ah, I got it

    Here we go. PSP -> Personal Software Process

    Check it out, it's useful and our graduates who know it say their employers were impressed by it.

    Watts Humphrey, A Discipline for Software Engineering, Addison-Wesley, 1995, ISBN 0-201-54610-8.
    An introduction to Humphrey's Personal Software Process (PSP), which applies principles of the Software Engineering Institute's Capability Maturity Model (CMM) to the individual software developer.

    -SB
    /*When all else fails, Immortality can be achieved through Massive Failure*/

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    17
    Thanks for all the input. I really appreciate it. I learned a lot of things from all this. It may seem weird, but I like to work out the errors in the code sometimes. I thought that this program was a little bit small to test every function, but I will most definitely consider it in larger, future programs.

  13. #13
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Perhaps not every function, but on very large projects, it is a very good idea to at least try every component (usually a single source file or source file/header file pair), and ensure its working properly, and that the bugs (as many as you can find) are out of it before moving on.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  2. Help with Blackjack program
    By sugie in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2005, 12:30 AM
  3. Blackjack!
    By Dr. Bebop in forum Game Programming
    Replies: 1
    Last Post: 10-03-2002, 08:58 PM
  4. Blackjack
    By the_head in forum C Programming
    Replies: 1
    Last Post: 08-03-2002, 08:57 AM
  5. BlackJack Program...
    By 67stangman in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2002, 10:44 PM