A little problem with my game

This is a discussion on A little problem with my game within the Game Programming forums, part of the General Programming Boards category; ok so I am making a little game everything goes fine until I get to this: Code: printf("You are facing ...

  1. #1
    Registered User lalorobot's Avatar
    Join Date
    Jun 2012
    Posts
    6

    A little problem with my game

    ok so I am making a little game everything goes fine until I get to this:
    Code:
    printf("You are facing 1 goblins goblin 1 has %d hp and deal 10 dammage by hit\n",goblin1Health);
    printf("(A)Attack Deals 25 Damage.\n");
    printf("(H)Heal Heals 25 health.\n");
    scanf("%c", battleCommands);
    it says that the program has stopped working..

    here is the complete source code:
    Code:
    #include <stdio.h>
    char KnightName[100];
    int goblin1Health = 50;
    int playerHealth = 100;
    char GoToBattle[10];
    char battleCommands;
    
    
    int main()
    {
        putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');
        putchar('\n');
        putchar('[');
        putchar('\n');
        putchar('[');
        putchar('\n');                                                                                                                                
        putchar('[');
        putchar('\n');    printf("[   BattleKnight\n");
        putchar('[');
        putchar('\n');
        putchar('[');
        putchar('\n');
        putchar('[');
        putchar('\n');
        putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');
        putchar('\n');
        printf("What is your name brave warrior?\n");
        scanf("%s", &KnightName);
        
        menu();
      
      return 0;
    }
    void menu()
    {
        printf("Welcome to BattleKnight %s\n", KnightName);
        printf("What do you want to do sir?\n");
        printf("Tip: Say the keywords of the options you are gived in here example: go to battle = battle\n", KnightName);
        printf("1- To Battle\n");
        scanf("%s", &GoToBattle);
            if(GoToBattle == 98, 97, 116, 116, 108, 101)
            {
                printf("You are facing 1 goblins goblin 1 has %d hp and deal 10 dammage by hit\n",goblin1Health);
                printf("(A)Attack Deals 25 Damage.\n");
                printf("(H)Heal Heals 25 health.\n");
                scanf("%c", battleCommands);
                if(battleCommands = 65)
                {
                    printf("You attack goblin 1");
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    goblin1Health--;
                    printf("You are facing 1 goblins goblin 1 has %d hp and deal 10 dammage by hit\n",goblin1Health);
                }
            }
        return 0;
    }
    note: I am using mingw compiler and I am new to programming also the language is C not c++

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    211
    You if statement says

    if(battleCommands = 65)

    it should be

    if(battleCommands == 65)

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,053
    Code:
    goblin1Health--;
    goblin1Health--;
    goblin1Health--;
    ...
    goblin1Health--;
    It would be easier to implement this as:

    Code:
    goblin1Health = goblin1Health - 25;
    Or, alternatively:

    Code:
    goblin1Health -= 25;

  4. #4
    Registered User lalorobot's Avatar
    Join Date
    Jun 2012
    Posts
    6
    ok thanks for correcting me there but for some reason it still says that it has stopped working.... here is a screenshot for you to see:
    Attachment 11733
    Edit:
    Also I get these warnings from the compiler:
    Name:  console warnings.png
Views: 136
Size:  27.7 KB

    note: I am on windows 8
    Last edited by lalorobot; 06-11-2012 at 11:49 AM.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,053
    You can define your functions before or after "main()" - if you define your functions after main ("function definition"), you have to declare them first before "main()" ("function declaration").

    Code:
    #include <stdio.h>
    
    void function1(void);  // note semi-colon after "function declaration"
    
    int main(void)
    {
        function1();
        return 0;
    }
    
    void function1(void)   // note NO semi-colon after "function definition" line
    {
        // code here
    }
    Also, you have your "menu()" function returning a value when the function is declared as not having a return type.

    Code:
    void menu()
    {
        // your code
    
        return 0;  // <-- THIS DOES NOT BELONG if the return type of the function is "void"
    }
    This line also needs to be fixed:

    Code:
    if(GoToBattle == 98, 97, 116, 116, 108, 101)
    You are trying to compare a user input string to integers. There are several ways to achieve a proper comparison correctly; one is with a string compare function.

    There's probably more that needs fixing, but that's what I saw after a brief look-through.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,602
    scanf takes the address of a variable, remember that and reexamine line #46
    Devoted my life to programming...

  7. #7
    Registered User lalorobot's Avatar
    Join Date
    Jun 2012
    Posts
    6
    Oh thanks! after some googling I finally understand string compare! and it works!
    I only have this two things the compiler sent me:
    Code:
    BattleKnight.c: In function 'menu':BattleKnight.c:51:4: warning: passing argument 2 of 'strcmp' makes pointer from
    integer without a cast [enabled by default
    here is the new source code:
    Code:
    #include <stdio.h>#include <string.h>
    char KnightName[50];
    int goblin1Health = 50;
    int playerHealth = 100;
    char GoToBattle[7] = "battle";
    char GoToBattleCommand[7];
    char attack [4] = "Att";
    char heal [4] = "Hea";
    char battleCommands;
    
    
    void menu();
    int main()
    {
        putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');
        putchar('\n');
        putchar('[');
        putchar('\n');
        putchar('[');
        putchar('\n');                                                                                                                                
        putchar('[');
        putchar('\n');    printf("[   BattleKnight\n");
        putchar('[');
        putchar('\n');
        putchar('[');
        putchar('\n');
        putchar('[');
        putchar('\n');
        putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');putchar('_');
        putchar('\n');
        printf("What is your name brave warrior?\n");
        scanf("%s", &KnightName);
        
        menu();
      
      return 0;
    }
    void menu()
    {
        printf("Welcome to BattleKnight %s\n", KnightName);
        printf("What do you want to do sir?\n");
        printf("Tip: Say the keywords of the options you are gived in here example: go to battle = battle\n", KnightName);
        printf("1- To Battle\n");
        scanf("%s", &GoToBattleCommand);
            if(strcmp(GoToBattle,  GoToBattleCommand) == 0 )
            {
                printf("You are facing 1 goblins goblin 1 has %d hp and deal 10 dammage by hit\n",goblin1Health);
                printf("(Att)Attack Deals 25 Damage.\n");
                printf("(Hea)Heal Heals 25 health.\n");
                scanf("%s", &battleCommands);
                if(strcmp(attack,  battleCommands) == 0 )
                {
                    printf("You attack goblin 1");
                    goblin1Health -= 25;
                    printf("You are facing 1 goblins goblin 1 has %d hp and deal 10 dammage by hit\n",goblin1Health);
                }
            }
    }
    Edit:
    to GReaper oh thanks!!!
    Last edited by lalorobot; 06-11-2012 at 01:02 PM.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    char KnightName[50];
    ....
    printf("What is your name brave warrior?\n");
    scanf("%s", &KnightName);
    You declare "KnightName" as an array of 50 chars therefore you don't need the & in front of the variable name (I assume you don't know about pointers yet, do you?)

    Code:
    printf("Tip: Say the keywords of the options you are gived in here example: go to battle = battle\n", KnightName);
    You don't use the value of "KnightName" in the format string.

    Code:
    char GoToBattleCommand[7];
    ....
    scanf("%s", &GoToBattleCommand);
    Same as above.

    Code:
    char battleCommands;
    ....
    scanf("%s", &battleCommands);
    if (strcmp(attack,  battleCommands) == 0 )
    You declare "battleCommands" as a char variable (an integer type), but later use it as a string.

  9. #9
    Registered User lalorobot's Avatar
    Join Date
    Jun 2012
    Posts
    6
    ok thanks gotta make that changes

    Edit: Oh and I know about do pointers and you all are going to be in the Credits!
    Last edited by lalorobot; 06-11-2012 at 02:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game problem
    By brookemay in forum Game Programming
    Replies: 6
    Last Post: 05-29-2011, 10:36 AM
  2. problem with my 2. game constructor problem I think
    By military genius in forum Game Programming
    Replies: 6
    Last Post: 10-10-2009, 11:56 AM
  3. Problem with my game
    By porkandbeans in forum C Programming
    Replies: 3
    Last Post: 05-25-2008, 09:13 AM
  4. 24 game problem
    By Blizzarddog in forum C++ Programming
    Replies: 10
    Last Post: 11-29-2003, 08:02 AM
  5. Problem with a game im making
    By TheGr8one in forum Game Programming
    Replies: 2
    Last Post: 10-19-2001, 07:38 PM

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