This is a blackjack program using array. It works in Devc++ but doesn't work in visual C++.I think I am missing some kind of header files could anyone help me out?
my email is ....
thanks

#include <iostream>
#include <ctime>
#include<iomanip>
#include <string>
using namespace std;
void deal(int,int&,int& ,int[][2],int&,int[][2] );
void deal(int,int&,int&,int&,int[][2],int&,int[][2] );
int getbet(int);
void filldeck(int[][2],int);
void shuffle(int[][2],int);
void printhand(int,int[][2]);
int main()
{string suit[4]={"hearts","diamonds","spades","clubs"};
string card[13]={"ace","2","3","4","5","6","7","8","9","10","jack","queen","king"};
int totdealer,totplayer,upto=0;
int money,bet,aces,decks=2,j;
int deck[52*decks][2];
int dealhand[20][2],playhand[20][2],playcards,dealcards;
char yesno='Y';
bool again;
srand(time(0));
filldeck(deck,decks);
 /*  cout<<"deck-defore shuffled:\n";
       for(j=0;j<52*decks;j++)
           cout<<card[deck[j][1]]<<"\t"<<suit[deck[j][0]]<<endl;
       cout<<endl;
 */
shuffle(deck,decks);
   /*cout<<"deck-after shuffled:\n";
       for(j=0;j<52*decks;j++)
           cout<<card[deck[j][1]]<<"\t"<<suit[deck[j][0]]<<endl;
       cout<<endl;
       */
cout<<"How much money do you have? ";
cin>>money;
while(money>0&&toupper(yesno)=='Y')
{bet=getbet(money);
totdealer=0;
playcards=0;
dealcards=0;
cout<<"\nDealer cards\n";
deal(2,totdealer,upto,deck,dealcards,dealhand);
printhand(dealcards,dealhand);
aces=0;
totplayer=0;
cout<<"\nPlayer cards\n";
deal(2,aces,totplayer,upto,deck,playcards,playhand);
printhand(playcards,playhand);
cout<<"\nYour total is "<<totplayer<<endl;
cout<<"You have "<<aces<<" aces"<<endl;
again=false;
while(totdealer<21&&totplayer<21&&!again)
    {
    cout<<"\ndo you want another card? ";
     cin>>yesno;   
     if(toupper(yesno)=='Y')
          {cout<<"\nPlayer cards\n";
          deal(1,aces,totplayer,upto,deck,playcards,playhand);
          printhand(playcards,playhand);
           cout<<"\nYour total is "<<totplayer<<endl;
           cout<<"You have "<<aces<<" aces"<<endl;
           }
     else
         again=true;
     }
cout<<"Dealer total: "<<totdealer<<endl;
cout<<"Your total: "<<totplayer<<endl;
if(totdealer>totplayer||totplayer>21)
      money-=bet;
else if(totdealer<totplayer&&totplayer<=21)
      money+=bet;
cout<<"\nGame over\nYou have $"<<money<<" left\n";
if(money>0)
     {cout<<"\nPlay again (Y/N)?";
     cin>>yesno;
     }
}
system("pause");
return 0;
}
void printhand(int m,int deck[][2])
{int i;
string suit[4]={"hearts","diamonds","spades","clubs"};
string card[13]={"ace","2","3","4","5","6","7","8","9","10","jack","queen","king"};
for(i=0;i<m;i++)
   cout<<card[deck[i][1]]<<"\t"<<suit[deck[i][0]]<<endl;

}
void deal(int num,int& tot,int& upto,int deck[][2],int& m,int p[][2])
{int i,n;

 for(i=0;i<num;i++)
     {n=deck[upto][1];
     n++;
     if(n>10)
         n=10;
       p[m][1]=deck[upto][1];
     p[m][0]=deck[upto][0];
     m++;   
         
      if(n==1)
          tot+=11;
      else if(n>=10)
          tot+=10;
      else
          tot+=n;
      upto++;
       }

}
void deal(int num,int& aces,int &tot,int& upto,int deck[][2],int& m,int p[][2])
{int i,n;
 for(i=0;i<num;i++)
     {n=deck[upto][1];
     n++;
     if(n>10)
         n=10;
     p[m][1]=deck[upto][1];
     p[m][0]=deck[upto][0];
     m++;
      if(n==1)
         {aces++;
          tot+=11;
          }
      else if(n>=10)
          tot+=10;
      else
          tot+=n;
      upto++;
          }
if(tot>21)
    if(aces==0)
         return;
    else
        {aces--;
        tot-=10;
        }
}
int getbet(int money)
{int bet;
cout<<"You have $"<<money<<" enter your bet: ";
cin>>bet;
while(bet>money)
    {cout<<"You cannot bet more then you have!!!\n";
     cout<<"You have $"<<money<<" enter your bet: ";
     cin>>bet;
     }
return bet;
}
void shuffle(int deck[][2],int decks)                  
{
int i,num1,type1,num2,type2,temp;
for(i=0;i<100*decks;i++)
   {num1=rand()%(13*decks);
    num2=rand()%(13*decks);
    type1=rand()%4;
    type2=rand()%4;
    temp=deck[type1][0];
    deck[type1][0]=deck[type2][0];
    deck[type2][0]=temp;
    temp=deck[num1][1];
    deck[num1][1]=deck[num2][1];
    deck[num2][1]=temp;
    }
      
}

      
void filldeck(int deck[][2],int decks)                  
{
	bool cards[4][13][decks];
int i,j,k,num,type,d;
for(i=0;i<4;i++)
   for(j=0;j<13;j++)
         for(k=0;k<decks;k++)
             cards[i][j][k]=false;
             
   for(j=0;j<52*decks;j++)
        {do
           {
            num=rand()%13;
            type=rand()%4;
            d=rand()%decks;
            }while(cards[type][num][d]);
         deck[j][0]=type;
         deck[j][1]=num;
         cards[type][num][d]=true; 
         }


}

Edited 6 Years Ago by ravi1986: Email Snipped. Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

What does "it doesn't work" mean?

Or you could try this: if it compiles in VC++, then run it through the debugger in VC++ or manually debug it by adding ouput statements after each action within the program to see where it fails.

What does "it doesn't work" mean?

Or you could try this: if it compiles in VC++, then run it through the debugger in VC++ or manually debug it by adding ouput statements after each action within the program to see where it fails.

No it doesn't work in VC++ at all looks like it needs to be deleted arrays for temporay. just run in VC++ and you will know what I mean I can send you the error if you want.

Well, first off, that formatting is horrible. You're going to want to start indenting properly if you want much help at DW.

Second, you can't just post and say "It doesn't work" then tell people they need to figure out what's wrong with it. Post your errors or what you're having trouble with so we don't have to compile it ourselves for something that can be figured out by looking at your error and then your code.

Third, your array declarations are incorrect. You can't declare an array using a non-const integer unless you're using new. There are a few ways to solve this.

Declare a const int globally and take the int decks out of your filldecks function and the int decks creation in main:

const int decks = 2;

Use a #define statement instead of a global variable:

#define DECKS 2

Create your arrays using new and pointers:

int **deck = new int*[52 * decks];
for(int i = 0; i < 52 * decks; i++)
    deck[i] = new int[2];

Well, first off, that formatting is horrible. You're going to want to start indenting properly if you want much help at DW.

Second, you can't just post and say "It doesn't work" then tell people they need to figure out what's wrong with it. Post your errors or what you're having trouble with so we don't have to compile it ourselves for something that can be figured out by looking at your error and then your code.

Third, your array declarations are incorrect. You can't declare an array using a non-const integer unless you're using new. There are a few ways to solve this.

Declare a const int globally and take the int decks out of your filldecks function and the int decks creation in main:

const int decks = 2;

Use a #define statement instead of a global variable:

#define DECKS 2

Create your arrays using new and pointers:

int **deck = new int*[52 * decks];
for(int i = 0; i < 52 * decks; i++)
    deck[i] = new int[2];

First I want to apologies not being clear myself may be because I'm new and its my first post. This program is written in DEVC++ and works perfect with no issues but I need this program running in visual C++ and here is the error during compile:-
Compiling...
blackjack_2.cpp
.\blackjack_2.cpp(18) : error C2057: expected constant expression
.\blackjack_2.cpp(18) : error C2466: cannot allocate an array of constant size 0
.\blackjack_2.cpp(18) : error C2133: 'deck' : unknown size
.\blackjack_2.cpp(171) : error C2057: expected constant expression
.\blackjack_2.cpp(171) : error C2466: cannot allocate an array of constant size 0
.\blackjack_2.cpp(171) : error C2087: 'cards' : missing subscript
Build log was saved at "file://d:\Documents and Settings\RGUPTA\Desktop\c++\c++2005\c++2005\Debug\BuildLog.htm"
c++2005 - 6 error(s), 0 warning(s)

I think its the memory allocation I ran out and don't know how to delete[] or other method to fix it. If you need any additional information just reply here and I will get back to ASAP. I can't even edit my post so sorry!

Fix first error, then recompile. Frist error appears to refer to this line:

int deck[52*decks][2];

decks is an int variable meaning it can have whatever value you want in any given program. Yes, you gave it the value of 2 by default when you declared it, but that doesn't qualify it to be a constant int, which it needs to be to declare deck using static memory like you try to do above. See Sodabread's post for options on how to fix this error.

The same problem exists in this line:

bool cards[4][13][decks];

which is why errors 4 and 5 are essentially the same as 1 and 2.

This article has been dead for over six months. Start a new discussion instead.