I was asked to do a simple 20x20 turtle graphics program but for some reason I'm having an issue that's probably related from line 42 to 150 (Only they were originally on the post but I edited it since someone asked me to on the comments):
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
int floor[20][20], step, p1 = 0, p2 = 0;
char compass[6] = "westm", com[1], pen[3] = "up";
int main()
{
com[0] = 'x';
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
floor[i][j] = 0;
}
}
while (com[0] != 'e' && com[0] != 'E')
{
com[0] = 'x';
printf("Enter a command \n");
scanf("%c", &com[0]);
getchar();
if (com[0] == 'e' || com[0] == 'E')
{
printf("End \n");
}
else
{
if (com[0] == 'u' || com[0] == 'U')
{
strncpy(pen, "up", 3);
printf("The pen was turned up \n");
}
if (com[0] == 'd' || com[0] == 'D')
{
strncpy(pen, "do", 3);
floor[p1][p2] = 1;
printf("The pen was turned down \n");
}
if (com[0] == 'r' || com[0] == 'R')
{
if (!strcmp(compass, "westm"))
{
strncpy(compass, "south", 6);
}
if (!strcmp(compass, "south"))
{
strncpy(compass, "eastm", 6);
}
if (!strcmp(compass, "eastm"))
{
strncpy(compass, "north", 6);
}
if (!strcmp(compass, "north"))
{
strncpy(compass, "westm", 6);
}
printf("The turtle turned right \n");
}
if (com[0] == 'l' || com[0] == 'L')
{
if (!strcmp(compass, "westm"))
{
strncpy(compass, "north", 6);
}
if (!strcmp(compass, "south"))
{
strncpy(compass, "westm", 6);
}
if (!strcmp(compass, "eastm"))
{
strncpy(compass, "south", 6);
}
if (!strcmp(compass, "north"))
{
strncpy(compass, "eastm", 6);
}
printf("The turtle turned left \n");
}
if (com[0] == 'w' || com[0] == 'W')
{
step = 2147483647;
if (!strcmp(compass, "westm"))
{
while (step + p2 > 19)
{
printf("Type a valid number of steps \n");
scanf("%d", &step);
getchar();
}
if (!strcmp(pen, "do"))
{
for (int i = 0; i <= p2 + step; i++)
{
floor[p1][p2 + i] = 1;
}
}
p2 = floor + p2;
}
if (!strcmp(compass, "north"))
{
while (p1 - step < 0)
{
scanf("%d", &step);
getchar();
}
if (!strcmp(pen, "do"))
{
for (int i = 0; i <= p1 - step; i++)
{
floor[p1 - i][p2] = 1;
}
}
p1 = p1 - step;
}
if (!strcmp(compass, "eastm"))
{
while (p2 - step < 0)
{
scanf("%d", &step);
getchar();
}
if (!strcmp(pen, "do"))
{
for (int i = 0; i <= p2 - step; i++)
{
floor[p1][p2 - i] = 1;
}
}
p2 = p2 - step;
}
if (!strcmp(compass, "south"))
{
while (step + p2 > 19)
{
scanf("%d", &step);
getchar();
}
if (!strcmp(pen, "do"))
{
for (int i = 0; i <= p1 + step; i++)
{
floor[p1 + i][p2] = 1;
}
}
p1 = p1 + step;
}
}
if (com[0] == 'p' || com[0] == 'P')
{
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
if (floor[i][j] == 0)
{
printf(". ");
}
else
{
printf("* ");
}
}
printf("\n");
}
}
}
}
}
Basically, on the beginning of the code there is an "while" waiting for user inputs until the're different from both 'e' and 'E', and it seems to be working fine on most cases
The problem is that when I walk AFTER turning right or left (by inputting 'w' after either the characters 'r' or 'l') or try to use the 'w' input for multiple times the program keeps asking for inputs (for the variable com, not step) without reading 'w' for some reason
Any other inputs like 'p', 'e' or even 'l' and 'r' again are working fine, but 'w' specifically doesn't work, and if I use 'p' ('e' doesn't count because it'll stop the repetition) and then 'w' the input will be recognized too
The algorythm with all those ifs kinda sucks but it's the most simple and easy to explain I could think of by myself