Thread: if statement's not working and variables failing to increment

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    13

    if statement's not working and variables failing to increment

    I have a little program that sets up a catapult to hit a target and avoid an obstacle, for my target and obstacle I have two different if statements that update the score (which is a global int), they set up a hit box for the each one and IF the projectile goes through it they should alter the score accordingly, however the if statements never activate for whatever reason. My current line of thinking is that its something to do with the global int score, but I have no real evidence for that, I'm not getting any errors which is even more confusing. Below I'll print the function that contains the if statements and the global int.
    Code:
    //global variables
    int total_targets_down;
    
    //draw projectile (coin)
    int projectile(int user_input,float user_input_3,float user_input_4, int x_target_pos, int y_target_pos, int total_targets_down)
    {
        //set inital pos of coin
        float pos_x=user_input-60;
        float pos_y=280-10;
        //PI to a value
        int pie = 3.142;
        //move to inital
        GFX_MoveTo(pos_x, pos_y);
        //set variables
        int inital_y=pos_y;
        int inital_x=pos_x;
        float time=1;
        int gravity=9.81;
    
        double horiz = user_input_3*cos(user_input_4 *pie/180);
        double vertical = user_input_3*sin(user_input_4 *pie/180);
        //caculate trajectory
        while(pos_y<399)
        {
            time=(pos_x-inital_x)/horiz;
            pos_y=(inital_y)-(vertical*time)+(gravity*time*time)/2;
            GFX_DrawLineTo(pos_x,pos_y,3);
                pos_x+=1;
        }
        //make hit box for target, then update score if it is hit
        if (pos_x>x_target_pos && pos_x<x_target_pos+120 && pos_y>y_target_pos-10 && pos_y<y_target_pos+80)
        {
        total_targets_down+=1;
        printf("Wallet: %d\n", total_targets_down);
        GFX_DrawCircle(30,30,20,4);
        }
        //make hit box for target, then update score if it is hit
        if (pos_x> 180 && pos_x<260 && pos_y>280 && pos_y<360)
        {
        total_targets_down-=1;
        printf("Wallet: %d\n", total_targets_down);
    
        }
        GFX_UpdateDisplay();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2017
    Posts
    13
    Okay, so i figured out that both the if statements where outside the while loop so it wasnt actually using them, also the variable shouldnt be included in the function arguement because that means its seperate from the global one, therefore irrelevant after the function has ended.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So two instant lessons you learnt today:
    - don't use global variables.
    - apply decent indentation to your code.

    > int gravity=9.81;
    That just means gravity is 9.
    Make it a double if you want better precision.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pre Post increment in a single printf statement
    By sreevira in forum C Programming
    Replies: 2
    Last Post: 11-10-2013, 11:59 AM
  2. Pre Post increment in a single printf statement
    By sreevira in forum C# Programming
    Replies: 1
    Last Post: 11-10-2013, 10:44 AM
  3. Why is this if statement failing?
    By xScen3xHdstyl3x in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2011, 02:37 AM
  4. Auto Increment static variables
    By musicman in forum C++ Programming
    Replies: 6
    Last Post: 10-11-2011, 08:04 AM
  5. Increment number of variables?
    By JonathanS in forum C++ Programming
    Replies: 1
    Last Post: 10-04-2011, 06:57 PM

Tags for this Thread