Thread: Not sure why the if statement is ignored

  1. #1
    Registered User
    Join Date
    Mar 2021
    Posts
    1

    Question Not sure why the if statement is ignored

    Code:
    #include <stdio.h>
    #include <graphics_lib.h>
    
    
    char user_input;
    
    
    int main(void)
    {
    
    
            char c;
            int i = 0;
    
    
            printf("Please enter r, b or g:  \n");
            scanf(" %c", &c);
    
    
            /* Open a graphics window */
            /* Make it 640 pixels wide by 480 pixels high */
            initwindow(1200, 680);
    
    
            /* choose red pen colour */
    
    
            switch (c)
            {
                case 'r':
                case 'R':
                    setcolor(RED);
                    break;
                case 'b':
                case 'B':
                    setcolor(BLUE);
                    break;
                case 'g':
                case 'G':
                    setcolor(GREEN);
                    break;
                default:
                    printf("Please run the program again and enter a valid character");
                    return 0;
            }
    
    
            /* draw a circle on the screen buffer
             at x_position, y_position
             with radius 10 and line thickness 2 */
            line(40+i, 600, 70+i, 520, 2);
            line(70+i, 520, 100+i, 600, 2);
            line(70+i, 520, 70+i, 440, 2);
            line(70+i, 460, 100+i, 500, 2);
            line(70+i, 460, 40+i, 500, 2);
            circle(70+i, 410, 30+i, 2);
            circle(55+i, 400, 5+i, 2);
            circle(85+i, 400, 5+i, 2);
            arc(70+i, 395, 30, 45, 90, 2);
            line(0, 600, 1200, 600, 2);
    
    
            update_display();
    
    
    
    
    
    
            float pos_x, pos_y, init_pos_x, init_pos_y, vel_x, vel_y;
            float time, gravity;
    
    
            init_pos_x = 100;
            init_pos_y = 500;
    
    
            gravity = 9.81;
    
    
            printf("What do you want to do next? (t for throw projectile, r for move right and l for move left) \n");
            scanf("%c", &user_input);
            getch();
    
    
            if (user_input == 't'){
    
    
                printf("What is the Velocity in the X direction? \n");
                scanf("%f", &vel_x);
                getch();
    
    
                printf("What is the Velocity in the Y direction? \n");
                scanf("%f", &vel_y);
                getch();
    
    
                    moveto((int)init_pos_x, (int)init_pos_y);
    
    
                        for (pos_x = init_pos_x; pos_y <= 600; pos_x++)
                        {
                            /* Calculate new height */
                            time = (pos_x - init_pos_x) / vel_x;
                            pos_y = init_pos_y - (vel_y * time) + (gravity * time * time)/2;
    
    
                            /* Draw a line */
                            lineto((int)pos_x, (int)pos_y, 1);
                            update_display();
                        }
                }
            else if (user_input == 'r'){
    
    
                i = 150;
                cleardevice();
                line(40+i, 600, 70+i, 520, 2);
                line(70+i, 520, 100+i, 600, 2);
                line(70+i, 520, 70+i, 440, 2);
                line(70+i, 460, 100+i, 500, 2);
                line(70+i, 460, 40+i, 500, 2);
                circle(70+i, 410, 30+i, 2);
                circle(55+i, 400, 5+i, 2);
                circle(85+i, 400, 5+i, 2);
                arc(70+i, 395, 30, 45, 90, 2);
                line(0, 600, 1200, 600, 2);
                update_display();
    
    
            }
            else if (user_input == 'l') {
    
    
            i = -150;
                cleardevice();
                line(40+i, 600, 70+i, 520, 2);
                line(70+i, 520, 100+i, 600, 2);
                line(70+i, 520, 70+i, 440, 2);
                line(70+i, 460, 100+i, 500, 2);
                line(70+i, 460, 40+i, 500, 2);
                circle(70+i, 410, 30+i, 2);
                circle(55+i, 400, 5+i, 2);
                circle(85+i, 400, 5+i, 2);
                arc(70+i, 395, 30, 45, 90, 2);
                line(0, 600, 1200, 600, 2);
                update_display();
            }
    
    
            /* Wait for a key press */
            getch();
    
    
    
    
            /* remove the display */
            closegraph();
    
    
    
    
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > scanf("%c", &user_input);
    Maybe you want
    scanf(" %c", &user_input);
    The leading space means you skip newlines etc and read the next visible printable character.

    It's only 'ignoring' because it doesn't contain what you expect.

    Some simple debug like
    Code:
    scanf("%c", &user_input);
    printf("Got char=%d\n", user_input);
    would have revealed all in an instant.
    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. Replies: 2
    Last Post: 04-23-2020, 12:03 PM
  2. 'If' Statement inside the Switch statement being ignored.
    By howardbc14 in forum C Programming
    Replies: 4
    Last Post: 04-11-2015, 11:45 AM
  3. EOF Statement in C++
    By Vespasian in forum C++ Programming
    Replies: 5
    Last Post: 09-24-2013, 02:14 PM
  4. what does this statement mean??
    By juice in forum C++ Programming
    Replies: 7
    Last Post: 02-27-2012, 08:56 AM
  5. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM

Tags for this Thread