Thread: Problem with variable control

  1. #1
    Unregistered
    Guest

    Problem with variable control

    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

  2. #2
    Unregistered
    Guest
    try using 'unsigned int' type for the 'int' type. and this piece of code could be:

    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" ) ;
    }
    _______________should work~____________________

    if ( x <= 10 || y <= 15 ){
    old_gen[x-1][y-1] = 1 ;
    printf( "\nPlease enter the coordinates of a living cell: " ) ;
    scanf( "%d %d", &x, &y ) ;
    } else{
    printf( "\nYou have entered invalid coordinates." ) ;
    printf( "\nPlease enter the coordinates of a living cell: " ) ;
    scanf( "%d %d", &x, &y ) ;
    if ( x <= 10 || 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 || y == -1 )
    {
    printf ( "\n\nThank You!\n\n" ) ;
    }

  3. #3
    Unregistered
    Guest
    That actually didn't work either, but it got me thinking, and I managed to accomplish what I needed to by completely rewriting the code from scratch.

    So, even though you didn't solve the problem directly, thanks for your help! I appreciate it.

    Eddie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in char variable
    By hitesh_best in forum C Programming
    Replies: 2
    Last Post: 08-11-2007, 12:01 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Edit Control problem
    By Malek in forum Windows Programming
    Replies: 3
    Last Post: 06-16-2002, 01:12 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Peculiar Problem with char variable in C Language
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-31-2001, 04:06 PM