Thread: do while loop parameters are not working

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    British Columbia, B.C.
    Posts
    30

    do while loop parameters are not working

    Hi I'm just wondering if anyone can tell me if there is something wrong with the way I am using the do while loop in my function GetIntInput().

    This is a part of my program for tic tac toe. It compiles fine and runs but the problem I have is that my condition that the user and computer cannot place thier character 'X' or 'O' in the array at a location that doens't have ' ' in it ( I pound defined ' ' to be EMPTY)

    But when the program executes the user can write over his own character and the computers. I highlighted the loop I think is the cause of the problem but I don't see why it isn't working.

    Any thoughts??

    Thank you!






    Code:
    /*******************************************************************************
    ********************************************************************************
    This function is for the users turn
    *******************************************************************************/
    void userturn(char TTTboard[])
    {
         int SquareNumber;     /*Number corresponding index array (TTTboard[])
         SquareNumber=GetIntInput(TTTboard);
         
         writetoboard(TTTboard, SquareNumber,USER); 
         DisplayTTTBoards(TTTboard);
    }
    
    /*******************************************************************************
    ********************************************************************************
     This function gets a valid integer input from the user.
     PRE: User has been prompted to enter an integer and min <= max.
     POST: an integer value, i, entered by user is returned, where min <= i <= max.    
    */
    
    int GetIntInput(char TTTboard[])
    {
        
        int i;
         
        do
        {
           printf("\n\nPlease enter number of the square you would like to place your 'O' in:");
           while(scanf("%i", &i) != 1)
       /* get rid of the characters that were entered */
           {
           clearInputStream();
           printf("\nThat is not valid. Please enter a valid location on the board: ");
     
           }
         
    
          /* clear the input stream again, in case extra characters after the integer
          were entered */
           clearInputStream();
    
         }while(ValueInRange(i,MIN,MAX)==FALSE && TTTboard[i]==COMPUTER && TTTboard[i]==USER);
            
           
           
                       return i;
              
    } 
    
    /*******************************************************************************
    ********************************************************************************
     This function determines if a value is within a range.
     PRE: min <= max and value is defined
     POST: returns true if min <= value <= max, and false otherwise.    
    *******************************************************************************/
    
    int ValueInRange(int value, int min, int max)
    {
        if(min <= value && value<= max)
       
               return TRUE;
        else
                  
               return FALSE;
       
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your value-in-range check is wrong.

    value is in range if it's >= min, and <= max.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
     TTTboard[i]==COMPUTER && TTTboard[i]==USER
    Can both of those conditions ever be true at the same time?

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    British Columbia, B.C.
    Posts
    30
    Quote Originally Posted by quzah View Post
    Your value-in-range check is wrong.

    value is in range if it's >= min, and <= max.


    Quzah.
    I'm not sure I understand how that is wrong? Doesn't it mean the same thing? Changing it to that wouldn't change the program when you compile it would it?

    min=<value<=max is what i essentially have coded

    and isn't that the same as:

    value>=min and value <=max?



    I'm still not sure why the condition that the array can only have EMPTY at a specific index to be able to write to it doesn't work.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    British Columbia, B.C.
    Posts
    30
    Quzah,

    Thanks for the reply! I'm just not sure how that would effect the function, could you clarify?

    -melodia

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    British Columbia, B.C.
    Posts
    30
    Thank you rags_to_riches

    Quote Originally Posted by rags_to_riches View Post
    Code:
     TTTboard[i]==COMPUTER && TTTboard[i]==USER
    Can both of those conditions ever be true at the same time?
    Exactly! I was wondering the same thing, they can't be the same, I thought about that but wasn't sure how I can make that work. Can I say:


    }while(ValueInRange(i,MIN,MAX)==FALSE && TTTboard[i]==COMPUTER || TTTboard[i]==USER);

    I think Itried this before but it didn't work. Should it work or would I have to change the condition completely somehow?

    I also tried

    }while(ValueInRange(i,MIN,MAX)==FALSE && TTTboard[i]!=EMPTY);


    I pound defined EMPTY to be ' ' and the array is initalized to only contain ' ' until the user or computer enters in a character

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    British Columbia, B.C.
    Posts
    30

    Unhappy

    I've tried all three ways and still the user can override any memory location in the array even if it already has an 'o' or 'x' in it. I cannot figure out why this loop control doesn't work. Help would be really appreciated

    Code:
    /*******************************************************************************
    ********************************************************************************
     This function gets a valid integer input from the user.
     User is been prompted to enter an integer and min <= max.
     It will repeat asking for input while the value is in the correct range and the memory location on board is not empty.
     POST: an integer value, i, entered by user is returned, where min <= i <= max.    
    */
    
    int GetIntInput(char TTTboard[])
    {
        
        int i;
         
        do
        {
           printf("\n\nPlease enter number of the square you would like to place your 'O' in:");
           while(scanf("%i", &i) != 1)
       /* get rid of the characters that were entered */
           {
           clearInputStream();
           printf("\nThat is not valid. Please enter a valid location on the board: ");
     
           }
         
    
          /* clear the input stream again, in case extra characters after the integer
          were entered */
           clearInputStream();
    
         }while( TTTboard[i]!=EMPTY && ValueInRange(i,MIN,MAX)==FALSE);
            
           
           
                       return i;
              
    } 
    
    
    /*******************************************************************************
    ********************************************************************************
     This function determines if a value is within a range.
     PRE: min <= max and value is defined
     POST: returns true if min <= value <= max, and false otherwise.    
    *******************************************************************************/
    
    int ValueInRange(int value, int min, int max)
    {
        if(min <= value && value<= max)
       
               return TRUE;
        else
                  
               return FALSE;
       
    }

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I'm coming into this thread late, and haven't gone through all your code, but I did want to make a few comments.

    Quote Originally Posted by melodia View Post
    Changing it to that wouldn't change the program when you compile it would it?

    min=<value<=max is what i essentially have coded

    and isn't that the same as:

    value>=min and value <=max?
    Yes, they are equivalent and your logic in ValueInRange is fine. As for the compiler changing it, it's hard to say. The resultant code will always behave the same, however the actual assembly code (and thus the ultimate binary) may be different. Depends on the compiler, but don't worry about it.

    Quote Originally Posted by melodia View Post
    }while(ValueInRange(i,MIN,MAX)==FALSE && TTTboard[i]==COMPUTER || TTTboard[i]==USER);
    Be careful of operator precedence here. Logical && has precedence over logical ||, so the code you wrote is equivalent to
    Code:
    while ((ValueInRange() == FALSE && TTTboard == COMPUTER) || TTTboard == USER)
    Again, not having gone through your code, I can't be sure, but you may have wanted
    Code:
    while (ValueInRange() == FALSE && (TTTboard == COMPUTER || TTTboard == USER))
    Notice the parentheses to force the || to evaluate first. Here is a link regarding operator precedences in C: C Operator Precedence Table.

    I just realized, I think the do-while condition that I commented on above is not what you're after. I think you're after something like "get input while value is out of range, or the position is already occupied", which might look more like
    Code:
    do {
        ...
    } while (ValueInRange() == FALSE || TTTboard == COMPUTER || TTTboard == USER);
    Or, more simply
    Code:
    do {
        ...
    } while (!ValueInRange() || TTTboard != EMPTY);

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by melodia View Post
    Quzah,

    Thanks for the reply! I'm just not sure how that would effect the function, could you clarify?

    -melodia
    I misread you. I thought you were doing:

    if( value <=min && value <= max )

    instead of:

    if( min <= value && value <= max )

    I think the other poster was on the right track though.
    Code:
    while( inrange(...) == FALSE && ( board == player || board == cpu ) )
    Or perhaps:
    Code:
    while( inrange() == false || board != empty )
    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Oct 2009
    Location
    British Columbia, B.C.
    Posts
    30
    Thank you both for replying to me! :-)

    See this is why I am confused I've tried using both || and && but neither work. I've ended up leaving the code as:

    Code:
    }while( TTTboard[i]!=EMPTY && ValueInRange(i,MIN,MAX)==FALSE)
    Sometimes I get confused by my boolean expressions though.
    Just to clarify what I want GetIntInput() to do:

    do all this

    prompt for an integer
    check if the user entered an integer

    if the user did not enter a integer
    prompt again for entry

    if the user entered a integer
    check if it is in the correct range (ValueInRange()) and check if the array TTTboard[] is empty or
    not.

    while ValueInRange() returns false and TTTboard[] is not empty


    If the condition is && both conditions need to be true for it to return the integer the user entered

    But if I use an || only one of the conditions needs to be true. This is wrong though because the entered integer could be in the correct range but the array at that index is already full.

    Am I correct about the && and ||?

    It's really confusing why this doesn't work. The program compiles perfectly fine but the condition that the memory location at the chosen index of the array must be empty to write to it is never met. Its so bizarre I feel like I'm going crazy.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Your logic sounds fine to me. Try throwing in some debug output at the bottom of the do-while loop to make sure it's behaving like you expect, like so:
    Code:
    fprintf(stderr, "You entered %d\n", i);
    fprintf(stderr, "MIN = %d, MAX = %d, ValueInRange(i, MIN, MAX) returns %s\n", MIN, MAX, ValueInRange(i, MIN, MAX) ? "TRUE" : "FALSE");
    fprintf(stderr, "The current value of the requested cell is '%c'\n", TTTboard[i]);
    fprintf(stderr, "TTTboard[i] != EMPTY evaluates to %s\n", TTTboard != EMPTY ? "TRUE" : "FALSE");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exit loop not working properly
    By melodia in forum C Programming
    Replies: 3
    Last Post: 11-21-2010, 06:04 PM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. my do while loop not working
    By rodrigorules in forum C Programming
    Replies: 12
    Last Post: 09-07-2005, 06:52 PM
  4. Replies: 6
    Last Post: 07-19-2005, 01:03 PM