Thread: can someone tell me what's wrong with this?

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    34

    can someone tell me what's wrong with this?

    This is from a tic tac toe game I'm making, it's the function to check if there is a win or not. It's simple code so you should be able to see what I'm doing with no problems. I have it set up to check in my array if there is a win, if so it returns a 1 if no it returns a 0 .. but it never returns a 1. Here's the code.

    Code:
    int checkwin(void)
    { 
     if ((board[1][1] == 'X') && (board[2][0] == 'X') && (board[0][2] == 'X'))
      return ('1');
     
     else
      return('0');
    }
    As you can see I only have one win situation set up, just for testing. The way this works is I have a do while loop that takes the user info, puts it in the array. It's set up as a do while win = '0' loop. Then I do win = checkwin(); before the end of the loop and it doesn't do anything. Any suggestions?
    -gunder

    if (problem)
    postcount++;

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Without seeing the entire code, your function expects an integer to be returned but you're returning the char value of '1'

    How does your function know about board[][]? Since you haven't passed anything in as an argument, my guess is the poor thing doesn't know what to do. A global array?
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    34
    The reason I have it set up using the '''s is because in my do while loop if I have do while win = 0 it just runs the loop once and quits even though I never changed win. If I do a do while win = '0' then it works fine. And yes my board array is global.

    I'll attatch the full source to this post. Note to anyone who looks at it, it's not playable yet but it's readable. Look at the checkwin and twoplayer functions to see what's not working. I wrote this using dev-c++, anyone should be able to understand the code, I comment quite a bit.
    -gunder

    if (problem)
    postcount++;

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >I have do while win = 0

    Don't assign a value in the loop condtion (=), do a comparison (==).
    Code:
    do { /*...*/ }
    while (win == '0');
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    34
    Ugh, well I feel like an idiot. Thanks a lot I knew it had to be something simple like that. I can't beleive I didn't spot that, oh well thanks again.
    -gunder

    if (problem)
    postcount++;

  6. #6
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Check your PM (user cp link) for a halfhearted attempt at a tic tac toe game
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM