Thread: Boolean in a character array

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    10

    Boolean in a character array

    Hi
    I am trying to get the code below displayed as X values where true, and O where false... so for the character array below, im trying to get it to output
    X X X
    X X O

    I have tried a few things such as a for loop (which i think is used?) but it ends up outputting something stupid



    Code:
    char Board[3][3] =	{ {true,true,true},  
    		  {true,true,false},};

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I have tried a few things...
    Let's see em and we'll go from there.

    gg

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Think about the data type of 'true' and 'false'.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    10
    Code:
    for (int q = 0; q < 2; q++)
    		{
    
    			for (int p = 0; p < 3; p++)
    			{
    				if (Board[q][p] = true)
    				{
    				Board[q][p] = 'X';
    				cout << Board[q][p];
    				}
    		
    				if (Board[q][p] = false)
    				{
    					Board[q][p] = 'O';
    					cout << Board[q][p];
    				}
    			}
    			 cout << endl;
    		}
    Thats the closest ive got but i dont think its close to being correct as it just displays:
    XXX
    XXX
    when im trying to get
    XXX
    XXO (As the bottom right is false)

  5. #5
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    The char type cannot hold true and false values, learn more about data types.
    Do not make direct eye contact with me.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    First problem:

    if (Board[q][p] = true)

    should be:

    if (Board[q][p] == true)

    >> for (int q = 0; q < 2; q++)

    should be:


    >> for (int q = 0; q < 3; q++)

    Finally, you could save yourself all of those steps by simply doing:

    Code:
    #define SET 'X'
    #define UNSET 'O'
    
    
     char Board[3][3] =
    {
      {SET, SET, SET},
      {SET, SET, SET},
      {SET, SET, UNSET}
    };
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    For good programming practice, that really ought to be:
    const char SET = 'X';
    const char UNSET = 'O';
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    10
    thanks alot
    sorted

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Character Array - almost? works
    By voltson4 in forum C Programming
    Replies: 3
    Last Post: 03-04-2003, 06:03 PM
  5. Array of Character Arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 06:07 PM