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;
}