newbie troubles with structures, functions, and a syntax error

This is a discussion on newbie troubles with structures, functions, and a syntax error within the C Programming forums, part of the General Programming Boards category; so im trying to make a program where i have characters, and i can battle them, ive simplified it as ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    4. A proper signature for main is int main(void) which should return 0.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21