Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Newbie Question: Split Programs in to multiple files.

Reply
Thread Tools

Newbie Question: Split Programs in to multiple files.

 
 
William E. Storey II
Guest
Posts: n/a
 
      05-22-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed).

Thanks Again for any Help Offered.

William E. Storey II
 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      05-22-2005
William E. Storey II wrote:
> 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 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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      05-23-2005
"William E. Storey II" wrote:
>
> 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:
news:news.announce.newusers
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a split is not a split Dumbell Computer Support 3 03-09-2009 10:45 PM
How can I split database results with ExecuteReader and Split? needin4mation@gmail.com ASP .Net 2 05-05-2006 10:36 PM
split on '' (and another for split -1) trans. (T. Onoma) Ruby 10 12-28-2004 06:36 AM
Small inconsistency between string.split and "".split Carlos Ribeiro Python 11 09-17-2004 05:57 PM
Why does split operate over multiple lines in the absence of "ms" ? And why doesn't $_ work with split? Sara Perl Misc 6 04-12-2004 09:07 AM



Advertisments