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.