Newbie Question: Split Programs in to multiple files.

  • Thread starter William E. Storey II
  • Start date
W

William E. Storey II

Hello All,

I am new to C programming or Procedural programming for that matter. I
got my start in the programming world with .NET. I have decided to move
to a real programming language that is not so bloated.

My problem is that I am trying to break up my program in to seperate
files for maintainability purposes. However I cant seem to get it to
work. The project is a simple console blackjack game. I have the
following files,

- main.c
- player.c
- dealer.c
- deck.c
- play.c

I did not see the need for a library since I did not really plan on
reusing the code ( except for maybee the shuffle function ). So what I
was trying to do was just to break the modules up in to seperate files
that I could just use the includes pre processor directive in order to
combine them.

Here is where my problem is though. In the Player, Dealer, and Play
modules. I need to use the deck types however since the compiler needs
things to be in a top to bottom order I was not able to include the
files in a way that would appease the compiler. So I tried to switch to
the library Idea.

So I made deck.o, player.o, play.o and dealer.o. Howerver when I mse
the player.o, play.o and dealer.o I tried to incldue the deck.h header
file so that they could be used in the library. But I got a conflicting
types error. So I removed the deck header file and then I just got a
variable not declared error

I am at a loss and was wondering if you guys and any advice on how to
solve this problem. I am not sure if I need to restructure the project
or not. It just seems to me unneccesary to use librarys, but since i
can not do that in a way that will compile right I dont see any other
method. I would love to hear your input and ideas on this topic.

Also when designing, a library what goes in the header file and what
goes in the c file? For instance if I made a library called deck.
would I declare the deck structure in the header, c file, or both?

If you need anymore information in order to help me or would like to see
the source code that I have please feel free to email me at
(e-mail address removed).

Thanks Again for any Help Offered.

William E. Storey II
 
M

Michael Mair

William said:
Hello All,

I am new to C programming or Procedural programming for that matter. I
got my start in the programming world with .NET. I have decided to move
to a real programming language that is not so bloated.

My problem is that I am trying to break up my program in to seperate
files for maintainability purposes. However I cant seem to get it to
work. The project is a simple console blackjack game. I have the
following files,

- main.c
- player.c
- dealer.c
- deck.c
- play.c

I did not see the need for a library since I did not really plan on
reusing the code ( except for maybee the shuffle function ). So what I
was trying to do was just to break the modules up in to seperate files
that I could just use the includes pre processor directive in order to
combine them.

Here is where my problem is though. In the Player, Dealer, and Play
modules. I need to use the deck types however since the compiler needs
things to be in a top to bottom order I was not able to include the
files in a way that would appease the compiler. So I tried to switch to
the library Idea.

So I made deck.o, player.o, play.o and dealer.o. Howerver when I mse
the player.o, play.o and dealer.o I tried to incldue the deck.h header
file so that they could be used in the library. But I got a conflicting
types error. So I removed the deck header file and then I just got a
variable not declared error

I am at a loss and was wondering if you guys and any advice on how to
solve this problem. I am not sure if I need to restructure the project
or not. It just seems to me unneccesary to use librarys, but since i
can not do that in a way that will compile right I dont see any other
method. I would love to hear your input and ideas on this topic.

Also when designing, a library what goes in the header file and what
goes in the c file? For instance if I made a library called deck.
would I declare the deck structure in the header, c file, or both?

If you need anymore information in order to help me or would like to see
the source code that I have please feel free to email me at
(e-mail address removed).

I am not exactly sure where your conceptual problem is; it would help,
though, if you would give us a short overview over the data structures
and interfaces you are using.

e.g.
--- deck.h ---
#ifndef DECK_H
#define DECK_H

typedef struct {
/* describe card */
} card;

typedef struct {
/* Here goes your representation of the deck */
} deck;

int shuffle (deck *my_Deck);
card draw (deck *my_Deck);
.....

#endif
--- deck.c ---
.....
#include <stdlib.h>
.....
#include "deck.h"
.....
/* return 0 on success */
int shuffle (deck *my_Deck)
{
.....
}
.....
--- dealer.h ---
#ifndef DEALER_H
#define DEALER_H

#include <stddef.h>
#include "deck.h"
#include "player.h"

typedef player dealer;

int deal (deck *my_Deck, dealer *me, player **playerlist,
size_t numplayers);

#endif
--- main.c ---

#include "deck.h"
#include "player.h"
#include "dealer.h"
#include "play.h"

int main (int argc, char **argv)
{
/* retrieve number of players from command line or stdin */
....
/* init players */
....
/* init shuffle rng */
....
/* Play until the user does not want to anymore */
....
play(....);
....
/* Cleanup */
}
-----

Maybe the inclusion guards (#ifndef...) are what you were looking
for.


Cheers
Michael
 
C

CBFalconer

William E. Storey II said:
I am new to C programming or Procedural programming for that matter.
I got my start in the programming world with .NET. I have decided to
move to a real programming language that is not so bloated.

My problem is that I am trying to break up my program in to seperate
files for maintainability purposes. However I cant seem to get it
to work. The project is a simple console blackjack game. I have the
following files,

- main.c
- player.c
- dealer.c
- deck.c
- play.c
.... snip ...

For each of those files, decide what file level entities within the
file are to be visible outside. Then mark all others as static.
Then create a filename.h file, which specifies the prototypes for
the externally visible functions, and declares the externally
visible data as "extern". #include "filename.h" in filename.c, so
the compiler can check agreement with the actual situation.

Whenever otherfile.c needs access to something in filename.c,
#include "filename.h" in otherfile.c

If a type is needed, such as a struct definition, or an enum, or a
common #define, put that in the appropriate header file. But never
declare data items in headers.

The more things you mark static, the better organized your program
actually is.

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top