Click to See Complete Forum and Search --> : one program, many files


Hyena
October 20th, 1999, 07:14 AM
okay, heres the deal- im trying to organize my main program, blackjack.ccp, by breaking it up into multiple programs. i've been okay with header files, but someone suggested the other day that i use another .ccp file. heres what i tried so far-

*************************************************
********blackjack.ccp****************************
*************************************************
#include <iostream.h>
#include "deck.h"
#include "bool.h"
#include "rand.h"
#include "function.h"


//-----------------------function prototypes-----------------------------
void noRepeat(int x);
void cHand(int& cTotal, int& cardCounter);
//-----------------------------------------------------------------------



//-----------------------global variables-------------------------------
int Deck[52];
deck d;
BOOL haveAce = FALSE;
//----------------------------------------------------------------------





//======================================================================
//-------------------------------MAIN-----------------------------------
//----------------------------------------------------------------------
void main()
{

char ans;


do {

d.makeDeck();
randomize();

int cardCounter = 0;


Deck[0] = rand() % 52;

for(int x = 1; x < 52; x++)
{
Deck[x] = rand() % 52;
noRepeat(x);
}


int pTotal, cTotal, hitOrStay;


pTotal = cTotal = hitOrStay = 0;



cout << "You draw ";
hand(pTotal, cardCounter, haveAce);
hand(pTotal, cardCounter, haveAce);




if(pTotal == 21)
{
cout << "Blackjack. You win\n";
}





while((pTotal < 22) && (ans != 's') && (ans != 'S'))
{
cout << "You have " << pTotal << endl;
cout << "\n<H>it or <S>tay? ";
cin >> ans;

if((ans == 'h') || (ans == 'H'))
{
hand(pTotal, cardCounter, haveAce);


if((haveAce == TRUE) && (pTotal > 21))
{
pTotal -= 9;
haveAce = FALSE;
}

}

}


cout << endl;




if(pTotal > 21)
{
cout << "You loose\n";
pTotal = 0;
}


cout << "\nComputer draws ";
cHand(cTotal, cardCounter);
cHand(cTotal, cardCounter);


while(cTotal < pTotal)
{
cHand(cTotal, cardCounter);
}



if(((cTotal < 22) && (pTotal < cTotal)) || (cTotal == 0))
{
cout << "The computer wins. Try again.\n";
}
else
{
cout << "You win, way to go\n";
}



cout << "\n\nPlay again? ";
cin >> ans;



} while((ans == 'y') || (ans == 'Y'));
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//=======================================================================



//------------------------------computer's hand----------------------------
void cHand(int& cTotal, int& cardCounter)
{

int temp = 0;

temp = Deck[cardCounter];

if(temp > 35)
{
cout << d.card[temp].face;
}
else cout << d.card[temp].value;



cout << " of " << d.card[temp].suit << "\n";

if(d.card[temp].face == "Ace")
{
haveAce = TRUE;

if((cTotal + 11) < 22)
{ d.card[temp].value = 11; }
else { d.card[temp].value = 1; }
}


cardCounter++;
cTotal += d.card[temp].value;
}
//-------------------------------------------------------------------------



//-------------------------no repeat---------------------------------------
void noRepeat(int x)
//shuffles deck (randomizes 0 - 51 with no repitions)
{
Deck[x] = rand() % 52;



for(int y = 0; y < x; y++)
{
if(Deck[x] == Deck[y])
noRepeat(x);
}


}
//-----------------------------------------------------------------------
**************************************************

**************************************************
*************function.h***************************
**************************************************
#ifndef FUNCTIONS_H
#define FUNCTIONS_H

void hand(int& pTotal, int& cardCounter, BOOL& haveAce);


#endif
*************************************************

**************************************************
**********function.ccp****************************
**************************************************
#include "deck.h"
#include <iostream.h>
#include "bool.h"

extern int Deck[52];
deck dd;


//------------------------------player's hand-----------------------------
void hand(int& pTotal, int& cardCounter, BOOL& haveAce)
{

int temp = 0;

temp = Deck[cardCounter];

if(temp > 35)
{
cout << dd.card[temp].face;
}
else cout << dd.card[temp].value;


cout << " of " << dd.card[temp].suit << "\n";

if(dd.card[temp].face == "Ace")
{
haveAce = TRUE;

if((pTotal + 11) < 22)
{ dd.card[temp].value = 11; }
else
{ dd.card[temp].value = 1; }
}


cardCounter++;
pTotal += dd.card[temp].value;

}
//-------------------------------------------------------------------------


this is what i have and i still keep getting 'unresolved external errors.' this is my first time working with multiple ccp files, so i dont know how to fix what im doing so please help.

ssrinivasa_rao
October 20th, 1999, 07:27 AM
Hi,


You can instruct compiler to compile individual files or compile all the files and link them at once.


If you want compile them at once
Microsoft compiler command line would be.
cl file1.cpp file2.cpp
The above command compiles, links and generates the executable.

Another method is:

cl -c file1.cpp
Above command generates the file1.obj
cl -c file2.cpp
Above command generates the file2.obj


link file1+file2
Links the obj files and generates exe binary.

This individual compilation is useful to avoid frequent compilation of entire project, when you are in development stage.

Another method is using make files. Try to read the documentation of the your specific make tool.

Hope this works.

Have fun.

Hyena
October 20th, 1999, 07:54 AM
hold on hold on, you're going way over my head- which do i want to do, compile these indivdually or all at once or what? also, could you show me some code for this, and which of the programs to put it in?

Matt
October 20th, 1999, 09:17 AM
What variables are causing the external errors?

Hyena
October 20th, 1999, 09:39 AM
i dont know what variables are causing the problems. how do i find out?

ssrinivasa_rao
October 21st, 1999, 11:10 AM
Hi,

Did you tried compiling as a single .cpp file.

If you succeed in that either of the technique which I said in prev. message should work.

Still if you are getting errors send the details of the tool or compiler you are using.