I'm trying to write a program that takes user inputs for coordinates and manipulates an array, but I have to check for invalid input and a sentinel value to quit, (-1). For some reason, what I've written so far isn't working.
Here's some sample output ...
"
Please enter coordinates : -1 3 //should quit asking for input here, but doesn't.
Please enter coordinates: -5 6 //this part works fine.
You have entered invalid coordinates.
Please enter coordinates: -2 5 //should notify for invalid input, but quits the program.
Thank you!
"
What am I doing wrong?
Here's the source code ...
#include <stdio.h>
int main()
{
int x = 0, y = 0, ;
int old_gen[10][15] ;
printf( "We are going to play the game of life. " ) ;
printf( "You will enter the coordinates of all\n" ) ;
printf( "living cells in the first generation of " ) ;
printf( "a 10x15 grid, and I will show you\n" ) ;
printf( "which cells survive, which cells die, " ) ;
printf( "and which cells are born in the next\n" ) ;
printf( "three generations.\n\n" ) ;
printf( "Enter the x coordinate of the living cell " ) ;
printf( "followed by the y coordinate. When you\n" ) ;
printf( "are finished and would like to begin the " ) ;
printf( "demonstration, enter (-1).\n\n" ) ;
printf( "Please enter the coordinates of a living cell: " ) ;
scanf( "%d %d", &x, &y ) ;
if ( 1 <= x <= 10 )
{
if ( 1 <= y <= 15 )
old_gen[x-1][y-1] = 1 ;
printf( "\nPlease enter the coordinates of a living cell: " ) ;
scanf( "%d %d", &x, &y ) ;
}
if ( x == -1 )
{
printf ( "\n\nThank You!\n\n" ) ;
}
if ( y == -1 )
{
printf ( "\n\nThank You!\n\n" ) ;
}
if ( x < 1 )
{
printf( "\nYou have entered invalid coordinates." ) ;
printf( "\nPlease enter the coordinates of a living cell: " ) ;
scanf( "%d %d", &x, &y ) ;
}
if ( x > 10 )
{
printf( "\nYou have entered invalid coordinates." ) ;
printf( "\nPlease enter the coordinates of a living cell: " ) ;
scanf( "%d %d", &x, &y ) ;
}
if ( y < 1 )
{
printf( "\nYou have entered invalid coordinates." ) ;
printf( "\nPlease enter the coordinates of a living cell: " ) ;
scanf( "%d %d", &x, &y ) ;
}
if ( y > 15 )
{
printf( "\nYou have entered invalid coordinates." ) ;
printf( "\nPlease enter the coordinates of a living cell: " ) ;
scanf( "%d %d", &x, &y ) ;
}
else
printf( "\n\nThank you!\n\n" ) ;
return 0 ;
}
Thanks for any help!
Eddie