This is for a homework assignment and it is done, but I have this little bug which I just can't figure out. I'm guessing it has to deal with the input buffer. So here goes:
My program generates an 8x8 map containing random values from 0 - 9 and also finding the shortest path from one point to another. It's supposed to take user input to either generate a map with new values, specify new points to find the shortest path for, and quitting the program. It continually loops through until the user enters the command to quit.
r - to generate new map
s followed by 4 single spaced numbers - to specify 2 points
q - to quit the program
Here is an example of the problem:
Please enter a command: r
Generating new height map
Please enter a command: error
Please enter a command:
Here is the code with the problem isolated.
CODE
#include <stdio.h>
int main() {
char command;
int x0, y0, x1, y1;
/*
'q' to quit
'r' to generate a new random height map
's x0 y0 x1 y1' to find the cheapest path from (x0,y0) to (x1,y1)
*/
while(true){
printf("Please enter a command: ");
scanf("%c", &command);
if((command != 'q') && (command != 'r') && (command != 's')){
printf("error\n");
}
else if(command == 'q'){
printf("quiting...\n");
return 0;
}
else if(command == 'r'){
printf("generating random height map\n");
}
else if(command == 's'){
scanf("%d %d %d %d", &x0, &y0, &x1, &y1);
if(((int) x0 > 7) || ((int) y0 > 7) || ((int) x1 > 7) || ((int) y1 > 7) ||
((int) x0 < 0) || ((int) y0 < 0) || ((int) x1 < 0) || ((int) y1 < 0)){
printf("error, values not within range\n");
return 0;
}
printf("x0 = %d\n", (int) x0);
printf("y0 = %d\n", (int) y0);
printf("x1 = %d\n", (int) x1);
printf("y1 = %d\n", (int) y1);
}
}
}