Thread: Struct dilemma

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    119

    Struct dilemma

    I don't have a clue why this little test program I made for a text based rpg doesn't work.

    According to the tutorial I read on structures, it should work fine.

    Code:
    #include <stdio.h>
    
    void info(int enemy_id);
    
    void fight(int hp, int dam);
    
    int main() {
    
        int enemy_number;
    
        enemy_number = 1;
    
        info(enemy_number);
    
    return 0;
    
    }
    
    void info(int enemy_id) {
    
    struct enemystats {
    
           int hp;
           int dam;
    
           }
    
           struct enemystats bear; <---read below what I write #1
           struct enemystats dog;
    
    if (enemy_id == 1) {
    
       bear.hp = 150;
       bear.dam = 75;
       }
    
       if (enemy_id == 2) {
    
       dog.hp = 75;
       dog.dam = 45;
    
       }
    
       else exit(0);
    
       fight(hp,dam); <---read below (#2)
    }
    
    
    void fight() {
    
                 printf("The monster's hp is %d",hp);
                 getchar();
                 return;
    
                 }
    DevC is my compiler (for what it's worth)

    #1 - The compiler says, "Two or more data types in declaration of bear".

    Well, according to the tutorials I've read on structures, what I typed there should make a new member in struct enemystats, which can then be used like "bear.hp" and "bear.dam" - what gives?

    #2 - The compiler says, "'hp' undeclard (first use in this function)"
    Actually, I see what that means. Which means that I'm not going to be able to accomplish this using structures to load the stats...I guess I'll try and do something with an array. Basically I wanted it to be where when you walk into a certain room in this game i'm gonna make, if you see a certain type of monster, the program goes to get the info on that particular monster, then passes on the relevant stats (hp for hitpoints, dam for damage) to a fight function.

    But I'd still like to know the awnser to question number 1, that doesn't make sense to me.

    Thanks in advance,

    Ash

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    struct declarations have a semi-colon after the ending brace }

    And as for your hp undeclared, hp is part of a struct, so it only makes sense to refer to it as part of an instance of that struct type, such as bear.hp.

    Also, you need stdlib.h for exit(0);

    Also, your fight function definition doesn't match your prototype.

    Edit: and please make an effort to ensure your code is indented sensibly. Parts of the "info" function are flushed to the left, but they should have an indent. It makes things easier for people helping.
    Last edited by cwr; 01-06-2006 at 05:19 AM.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    lol, i see what you mean, I need to go to sleep, i'm too tired to be trying this right now, when I'm making mistakes like that! Thanks for the refresher on the ; after the ending brace, totally slipped my mind!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM