Thread: Mini Game

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    20

    Mini Game

    Hi guys I'm developing a mini game as a project, and I would gladly appreciate if someone could tell me if I'm heading in the right direction.

    The aim is to develop a bee-keeping game. First of all I need to have a text file called Inventory which contains the following data:

    Total Amount of Bees: 15
    Total Amount of Infected Bees: 5
    Total Amount of Money: 1500
    Total Amount of Honey Jars: 5
    Total Amount of Beeswax: 0

    Once that is finished I need to write a menu which holds the following choices:
    New Game (Set to Default Amounts)
    Buy New Bees
    Bees Death Process
    End Program

    Below is the code I have written so far

    Code:
    #include <stdio.h>
    
    
    struct Inventory {
    int NoOfBees, NoOfInfBees, Money, Honey, Beeswax;
    };
    
    
        int enterChoice(void);
        void textFile(FILE *readPtr);
        void BuyBees(FILE *fPtr);
        void DeadBees(FILE *fPtr);
    
    
    int main(void)
    {
        FILE *cfPtr;
        int choice;
    
    
        if ((cfPtr = fopen("Inventory1.txt", "rb+"))==NULL){
        printf("File could not be opened.\n");
        }
        else {
            while((choice=enterChoice())!=4){
            switch(choice){
    
    
            case 1:
            textFile(cfPtr);
            break;
    
    
            case 2:
            BuyBees(cfPtr);
            break;
    
    
            case 3:
            DeadBees(cfPtr);
            break;
    
    
            default:
            printf("Invalid choice.\n");
            break;
            }
        }
        fclose(cfPtr);
        }
        return 0;
    }
        /*New Game (Default Amounts, Choice 1)*/
        void textFile(FILE*readPtr)
        {
        FILE *writePtr;
        struct clientData client = {0,"","",0.0};
        if ((writePtr = fopen("Inventory1.txt","w"))==NULL){
        printf("File could not be opened.\n");
        }
        else {
        rewind(readPtr);
        fprintf(writePtr,"Total Number of Bees: 15\n", "Total Number of Infected Bees: 5\n", "Total Amount of Money: 1500\n", "Total Amount of Honey Jars: 5\n", "Total Amount of Beeswax: 0\n");
    
    
        while (!feof(readPtr)) {
        fread(&client, sizeof(struct clientData),1,readPtr);
        }
    }
        fclose(writePtr);
        }
    }
    
    
        /*Menu*/
        int enterChoice( void )
        {
        int menuChoice;
    
    
        printf( "\nEnter your choice\n"
        "1: New Game (Set to Default Amounts)\n"
        "2: Buy Bees.\n"
        "3: Bees Death Process.\n"
        "4: End Program.\n");
    
    
        scanf("%d", &menuChoice);
        return menuChoice;
    }
    Any help/tips/critique would be greatly appreciated.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    What happened with your last thread about this, bonett09? Qv.

    Opening, Reading and Updating a text file.

    Scroll to the bottom, I just posted there.

    It looks as if you are trying to implement CommonTater's suggestion, which is good, but
    you are going about it in a strange way, and this code OBVIOUSLY WON'T COMPILE for a number of reasons.

    Any help/tips/critique would be greatly appreciated.
    Yes. Do not write code in such a way that you cannot test it every 10 minutes or 10-15 lines, whichever comes first. As I said, this code obviously won't compile and perhaps never did, so another suggestion would be that rather than asking vaguely for a critique, you be more specific and say: "This code won't compile and I get a number of errors such as _____. What does that mean? What have I done wrong?"
    Last edited by MK27; 12-24-2011 at 06:36 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    20
    I thought it went dead so I just erased everything and started from scratch. As you may have realized I'm pretty desperate as I have a deadline and I'm confused at about every corner, but anyway.

    Well at the moment I have these errors, but as you said I purposely started 'unflowingly' as I only know how to do certain parts.

    Random2.c: In function 'void textFile(FILE*)':
    Random2.c:49:20: error: variable 'textFile(FILE*)::clientData client' has initializer but incomplete type
    Random2.c:58:41: error: invalid application of 'sizeof' to incomplete type 'textFile(FILE*)::clientData'
    Random2.c: At global scope:
    Random2.c:63:1: error: expected declaration before '}' token

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bonett09 View Post
    As you may have realized I'm pretty desperate as I have a deadline and I'm confused at about every corner, but anyway.
    Phew! We are on the same page then (not as in, I am confused, but as in: we both know you are). So...

    Well at the moment I have these errors, but as you said I purposely started 'unflowingly' as I only know how to do certain parts.
    Like I said, it is important to always compile and test as you go, even if that means sometimes shimming in some temporary code so that everything will make enough sense together. That may seem like an unnecessary waste of time, but not as much as of an unnecessary waste of time as what you may have to deal with later if you don't. Eg, very common: making the same mistake umpteen times because you didn't check to see if something worked, you just kept on typing. Now you have to correct all umpteen versions of the mistake. Etc.

    Code:
    Random2.c: In function 'void textFile(FILE*)':
    Random2.c:49:20: error: variable 'textFile(FILE*)::clientData client' has initializer but incomplete type
    Random2.c:58:41: error: invalid application of 'sizeof' to incomplete type 'textFile(FILE*)::clientData'
    Because you haven't defined type "struct clientData" yet, but you tried to instantiate it. Errors like this show up at compile time, meaning you can't refer to a non-existent type somewhere even if it is in a section of code that can't happen at runtime (such as this, since the function is never called).

    How exactly are you compiling the code? This is important, because it looks to me like that is output from a C++ compiler, and compiling C code as C++ can complicate things.

    Code:
    Random2.c: At global scope:
    Random2.c:63:1: error: expected declaration before '}' token
    Well, this block of code at the end:
    Code:
    }
     
     
        /*Menu*/
        int enterChoice( void )
        {
        int menuChoice;
     
     
        printf( "\nEnter your choice\n"
        "1: New Game (Set to Default Amounts)\n"
        "2: Buy Bees.\n"
        "3: Bees Death Process.\n"
        "4: End Program.\n");
     
     
        scanf("%d", &menuChoice);
        return menuChoice;
    }
    Is not inside any function. Why did you do that? Again: you should not write code willy-nilly with the idea that you'll put it inside a function later, or whatever. Always write, like I said, so that you can compile and test. If you can't compile, stop what you are doing, figure out why, and fix it before you continue.

    What you have to do right now is put that code somewhere more appropriate and define struct clientData. That should allow you to compile without errors, even if the resulting program doesn't do anything. If not, post the corrected code and your new errors.
    Last edited by MK27; 12-24-2011 at 07:48 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    20
    Okay, I adjusted the code and I have some new errors now, what do these mean?

    Random2.c: In function 'void textFile(FILE*)':
    Random2.c:49:37: error: invalid conversion from 'const char*' to 'int' [-fpermissive]
    Random2.c:49:37: error: invalid conversion from 'const char*' to 'int' [-fpermissive]


    Tool completed with exit code 1

    By the way, your hunch was right in the other thread, I use mingw as my compiler.

    Code:
    #include <stdio.h>
    
    
    struct Inventory {
    int NoOfBees, NoOfInfBees, Money, Honey, Beeswax;
    };
    
    
        int enterChoice(void);
        void textFile(FILE *readPtr);
        void BuyBees(FILE *fPtr);
        void DeadBees(FILE *fPtr);
    
    
    int main(void)
    {
        FILE *cfPtr;
        int choice;
    
    
        if ((cfPtr = fopen("Inventory1.txt", "rb+"))==NULL){
        printf("File could not be opened.\n");
        }
        else {
            while((choice=enterChoice())!=4){
            switch(choice){
    
    
            case 1:
            textFile(cfPtr);
            break;
    
    
            case 2:
            BuyBees(cfPtr);
            break;
    
    
            case 3:
            DeadBees(cfPtr);
            break;
    
    
            default:
            printf("Invalid choice.\n");
            break;
            }
        }
        fclose(cfPtr);
        }
        return 0;
    }
        /*New Game (Default Amounts, Choice 1)*/
        void textFile(FILE*readPtr)
        {
        FILE *writePtr;
        struct Inventory bee = {0,"","",0.0};
        if ((writePtr = fopen("Inventory1.txt","w"))==NULL){
        printf("File could not be opened.\n");
        }
        else {
        rewind(readPtr);
        fprintf(writePtr,"Total Number of Bees: 15\n", "Total Number of Infected Bees: 5\n", "Total Amount of Money: 1500\n", "Total Amount of Honey Jars: 5\n", "Total Amount of Beeswax: 0\n");
    
    
        while (!feof(readPtr)) {
        fread(&bee, sizeof(struct Inventory),1,readPtr);
        }
    }
        fclose(writePtr);
    }

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bonett09 View Post
    Random2.c: In function 'void textFile(FILE*)':
    Random2.c:49:37: error: invalid conversion from 'const char*' to 'int' [-fpermissive]
    Random2.c:49:37: error: invalid conversion from 'const char*' to 'int' [-fpermissive]
    Because "" is an (empty) string literal, but all fields in struct Inventory are ints:

    Code:
    struct Inventory {
    int NoOfBees, NoOfInfBees, Money, Honey, Beeswax;
    };
    
    struct Inventory bee = {0,"","",0.0};
    There's a couple of other problems there too: your forth arg is a double, probably the compiler will convert that, and you don't have a fifth one. That might not matter, but if you just want to zero the whole struct, you can use:

    Code:
    struct Inventory bee = { 0 }
    Because when you partially initialize a compound or array type (structs are compound), the rest of it is set to zero.

    By the way, your hunch was right in the other thread, I use mingw as my compiler.
    But how are you using it? Directly on the command line, or via some button somewhere?

    BTW, did you think about what I said in the other thread, vis, at least switching to an editor with syntax highlighting? It makes life much easier. Notepad++ is free, shouldn't take much to install, (I presume) is pretty simple to use, and intended for C/C++ coding.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    20
    I'm downloading Notepad++ at the very moment as at first I was hesitant due to becoming accustomed to Textpad.

    I didn't completely understand your question regarding how I'm using it via a button or command line, however I can give you a link which our lecturer gave us in regards how we should set it up. It is at the very bottom.

    Introduction to Programming in C

    In regards to the code, I have done as you suggested and I have come up with:

    Random2.c:(.text+0x69): undefined reference to `BuyBees(_iobuf*)'
    Random2.c:(.text+0x77): undefined reference to `DeadBees(_iobuf*)'
    Random2.c:(.text+0x8b): undefined reference to `enterChoice()'
    collect2: ld returned 1 exit status


    Tool completed with exit code 1

    That is because I haven't continued my code right? Or am I wrong?

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bonett09 View Post
    I didn't completely understand your question regarding how I'm using it via a button or command line, however I can give you a link which our lecturer gave us in regards how we should set it up. It is at the very bottom.
    Evidently your prof wants you to use g++ and not gcc to compile, altho it is a C course. Sort of strange but not a big deal; C++ compilers are just a little stricter than C compilers about some things. Right now it probably does not make any difference at all.

    Code:
    Random2.c:(.text+0x69): undefined reference to `BuyBees(_iobuf*)'
    Random2.c:(.text+0x77): undefined reference to `DeadBees(_iobuf*)'
    Random2.c:(.text+0x8b): undefined reference to `enterChoice()'
    Because altho you have declared those functions (with a prototype), you have not defined them yet. Just comment out the references to them in your switch(), and, one at a time, write the functions, compile, test, when everything works, move on to the next one.

    Anyway, I'm off into the wild blue yonder for the day. Good luck!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    20
    Thanks! Will repost here either tomorrow or the day after.

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    20
    Hi again,

    I was thinking of tackling the first choice (BuyBees) by first writing it in pseudocode. Do you think I would be going the right way if I followed this diagram?

    http://imageshack.us/photo/my-images/52/buybees.jpg/

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your diagram doesn't include the diamond shaped figures to show the if() statements - it just "jumps" to the next rectangular figure. Yes, that drawing has some of the logic that you could use, but it doesn't have it all.

  12. #12
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Why lawd! WHY!?! Why did he have to post a link to such an awful flowchart?!

    I'll be back in a while... I have a flowchart to fix. 8-)

  13. #13
    Registered User
    Join Date
    Dec 2011
    Posts
    20
    Its only meant as a brainstorm, not an actual pseudocode. A bit of a misunderstanding in my context.

  14. #14
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    I wasn't insulting you, just the flowchart. I have to redo it or I'll be thinking about it all day. It's nothing personal and if it sounded like an insult I apologize.

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I don't see anything wrong with the diagram. I would not call it pseudo-code, and it does not use formal flow-control symbols, but who cares? It makes sense. If the assignment were to "create a formal flow control chart for the program", there might be something to work on there andrew89, but since it isn't, you're just engaged in distraction and irrelevancies. No insult intended
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Critique my mini-AES
    By Hughesz93 in forum C Programming
    Replies: 7
    Last Post: 05-04-2011, 03:49 PM
  2. need some help to upgrade a mini game in C
    By Exutus in forum C Programming
    Replies: 2
    Last Post: 11-23-2009, 12:10 AM
  3. Mini If
    By Milhas in forum C Programming
    Replies: 4
    Last Post: 03-27-2008, 04:04 PM
  4. Mini-screens
    By pianorain in forum Tech Board
    Replies: 2
    Last Post: 07-25-2004, 11:21 PM
  5. mini-itx
    By whistlenm1 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-18-2003, 03:58 PM