noob question

This is a discussion on noob question within the C Programming forums, part of the General Programming Boards category; you does anyone know what is wrong ith this code Code: #include <stdio.h> #include <stdlib.h> int fight ( int player_a_p ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    26

    Unhappy noob question

    you does anyone know what is wrong ith this code


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    int fight ( int player_a_p , int enemy_h_p );// a_p means attack points and h_p means health
    int main()
    
    
    {
        int player_a_p;
        int attack_q; // attack_q means attack_question
    
        
        printf ("You are walking through the woods and a goblin jumps out \n");
        printf ("will you attack [1] or try to escape [2]? \n ");
        scanf ( "%d", &attack_q );
        
        if (attack_q = 1)
        {fight;}
        else
        {printf ("that was not an option!");}
        
    }
    
    int fight (int player_a_p , int enemy_h_p , int q_offence );
    
    {
        int player_a_p
        int q_offence
        int enemy_hp;
        enemy_hp = 100;
        
       do   
        
        printf ("enemy hit points : \n " , enemy_h_p);
        printf ("will you use an offensive move [1] \n ");
        scanf ( "d%", &q_offence);
       
        if (q_offence = 1)
       
        {
       
                      player_a_p = randn(10) + 20
                      printf ("you hit for " , player_a_p)
                      printf ("the enemy's hit points are " , enemy_h_p)
                      getchar();
                      getchar();
                      getchar();
                      getchar();
                      getchar();
                      getchar();
        }
        
        while (1==1)
        
        
        
    }


    im using dev-c++ and this is in c

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by gillypie View Post
    if (attack_q = 1)
    This will *always* be true, since you are *assigning* not *comparing* 1 to attack_q. You want:
    Code:
    if (1 == attack_q)
    For this precise reason, you should always put the constant first, then the compiler will complain if you try to use = instead of ==

    QuantumPete
    edit: You're also missing braces for the do ... while loop!
    edit:
    Code:
    printf ("enemy hit points : \n " , enemy_h_p);
    You need a format specifier, if you actually want to print out enemy_h_p:
    Code:
    printf ("enemy hit point: &#37;d\n", enemy_h_p);
    edit:
    Code:
    {fight;}
    What the heck?? You need to pass arguments to this function!
    Code:
    fight (player_a_p, enemy_h_p, q_offence);
    Also your declaration of fight doesn't match the definition!
    Alright that was fun, but give us the specific compiler error next time as well!
    Last edited by QuantumPete; 09-04-2007 at 06:57 AM.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Code:
    -You're not calling fight, you're just referencing it to nothing (has no effect)
    -if (q_offence = 1) should be if (q_offence == 1)
    -if (attack_q = 1) should be if (attack_q == 1)
    -The do-while loop has no {}.
    -You havnt added ";" on all your lines.
    Some reason i needed to use the code thingies..

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    how do you "call" fight.

    i know i sound stupid but im really new to this.
    Last edited by gillypie; 09-04-2007 at 07:09 AM. Reason: hadnt seen 39ster's comment

  5. #5
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Post your new code.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    yes, but with what? Telepathy is not my strong point...

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    sorry lol

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    int fight ( int player_a_p , int enemy_h_p );// a_p means attack points and h_p means health
    int main()
    
    
    {
        int player_a_p;
        int attack_q; // attack_q means attack_question
    
        
        printf ("You are walking through the woods and a goblin jumps out \n");
        printf ("will you attack [1] or try to escape [2]? \n ");
        scanf ( "%d", &attack_q );
        
        if (1 == attack_q)
        {fight;}
        else
        {printf ("that was not an option!");}
        
    }
    
    int fight (int player_a_p , int enemy_h_p , int q_offence );
    
    {
        int player_a_p;
        int q_offence;
        int enemy_hp;
        enemy_hp = 100;
        
       do   
        {
        printf ("enemy hit points : \n " , enemy_h_p);
        printf ("will you use an offensive move [1] \n ");
        scanf ( "d%", &q_offence);
       
        if (1 == q_offence)
       
        {
       
                      player_a_p = randn(10) + 20;
                      printf ("you hit for " , player_a_p);
                      printf ("the enemy's hit points are " , enemy_h_p);
                      getchar();
                      getchar();
                      getchar();
                      getchar();
                      getchar();
                      getchar();
        }
        while (1==1)
        
    
        
    }
        
        
        
        
        ive colon'd up everything exept the stuff im not supposed to (unless im supposed to colon every line)

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I wrote this in the other thread - seems like you didn't follow the advice:
    Quote Originally Posted by matsp
    One tip I have is: When writing conditional code, write it "constant first", because then you don't get into trouble with mixing single equal signs (assign value) with double equal signs (test for equality). E.g.
    Code:
      ...
      if ( 7 == magic_number ) ...
      ...
    --
    Mats

  9. #9
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    try this:

    Code:
    int fight ();
    int main()
    
    {
        int player_a_p = 10;
        int attack_q; // attack_q means attack_question
    
    
        printf ("You are walking through the woods and a goblin jumps out \n");
        printf ("will you attack [1] or try to escape [2]? \n ");
        scanf ( "&#37;d", &attack_q );
    
        if (attack_q == 1)
        	fight();
        else
        	printf ("that was not an option!");
    
    }
    
    int fight ()
    
    {
       int player_a_p;
       int enemy_h_p = 100;
       int q_offence;
    
       do
       {
        printf ("enemy hit points : %i\n " , enemy_h_p);
        printf ("will you use an offensive move [1] \n ");
        scanf ( "%d", &q_offence);
    
        if (q_offence == 1)
    
        {
    
                      player_a_p = 20 + rand() %10;
                      printf ("you hit for %i\n" , player_a_p);
                      printf ("the enemy's hit points are %i\n" , enemy_h_p);
                      getchar();
                      getchar();
                      getchar();
                      getchar();
                      getchar();
                      getchar();
        }
    
        }while (1==1);
    
    
    }
    Last edited by 39ster; 09-04-2007 at 07:26 AM.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    im sorry matsp i am really a noob i dont understand alot of c jargon yet. but thanks for your help and no-offence - its good advice.

    the code you psted does not work on my machine 39ster does it work on yours? just awnted to know if the problems my computer or your code - could be a breakthrough.
    Last edited by gillypie; 09-04-2007 at 07:22 AM.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
        if (1 == attack_q)
        {fight;}
    This is not calling a function. To call a function, it needs to have parenthesis after the function name. But fight also takes arguments.

    Now if we look at this:
    Code:
    int fight ( int player_a_p , int enemy_h_p );// a_p means attack points and h_p means health
    ...
    int fight (int player_a_p , int enemy_h_p , int q_offence );
    ...
    The first place were fight is declared is called a prototype (it shows what the function looks like, but doesn't have the definition that tells what it actually does).

    Next comes teh definition - but it's got a semicolon on the end, so the compiler will think it's a prototype - it will probably complain that you are changing the prototype. After that follows a bunch of code that is the actual fight-code.

    So, you need to decide if fight takes two or three arguments, and then pass them, and make the definition of the function work.

    I would also advice that you want to put your braces "{" and "}" on separate lines from the code within the braces - making it look nice and tidy is a good goal [and perhaps more important: Constistant - each place of doing something similar should look very similar, so you don't have to think "why does it look different here from the other bit of code that does similar things"].

    --
    Mats

  12. #12
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by 39ster View Post
    -You havnt added ";" on all your lines
    Sage advice. Try adding a semicolon after all lines that are not followed by an opening brace...

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  13. #13
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Quote Originally Posted by gillypie View Post
    im sorry matsp i am really a noob i dont understand alot of c jargon yet. but thanks for your help and no-offence - its good advice.

    the code you psted does not work on my machine 39ster does it work on yours? just awnted to know if the problems my computer or your code - could be a breakthrough.
    Try it again. I just changed
    Code:
    int fight ();
    
    {
    to
    Code:
    int fight ()
    
    {
    AND PLEASE TELL US THE ERROR =D

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    while (1==1);
    is the same as
    Code:
    while(1);
    --
    Mats

  15. #15
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by gillypie View Post
    just awnted to know if the problems my computer or your code - could be a breakthrough.
    With code this simple and no machine-dependent operations, it's almost *always* the code

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  2. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 12:28 PM
  3. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 07:08 PM
  4. Very noob question :(.
    By GamerProduction in forum Tech Board
    Replies: 4
    Last Post: 04-14-2007, 05:40 AM
  5. Noob question ( little dos program )
    By Demon1s in forum C++ Programming
    Replies: 13
    Last Post: 04-04-2003, 08:28 PM

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