Thread: newbie troubles with structures, functions, and a syntax error

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    6

    newbie troubles with structures, functions, and a syntax error

    so im trying to make a program where i have characters, and i can battle them, ive simplified it as much as possible to try and just get the most basic pieces working (eventually itll read characters from a character.file and the characters character.items lol )

    anyway i wrote this, and it wont compile, (syntax error here?: i=battle(struct monster bob, struct monster tony); ) so i tried deleting that function and writing a simple function just to see if i am not using the right function syntax, that worked fine, so i dunno what im doing wrong maybe i dont know how to use structures lol (i purposely dont have any pointers atm because i dont want to add bugs before i fix this)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {
    int i=0;
    struct monster
           {
           char name[30];
           int type;
           };
    struct monster bob = {"bob\0", 1};
    struct monster tony = {"tony\0", 2};   
    struct monster alex = {"alex\\", 3};
           
    int battle(struct monster one, struct monster two)
         {
         if(one.type>two.type)
         printf("%s wins",one.name);
         else
         printf("%s wins", two.name);
         return(0);
         }
    
    i=battle(struct monster bob, struct monster tony);
    printf("%d",i);
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. move function declaration and structure declaration out of the main
    2. in the function call provide only variables - not their types
    3. Don't put \0 in the string constant = it is added automatically
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    YES! sweet i am on my way to having a toy lol

    thank you

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    4. A proper signature for main is int main(void) which should return 0.

Popular pages Recent additions subscribe to a feed