Thread: Negating Boolean Values

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105

    Negating Boolean Values

    Hello guys, I made some research on how to negate a boolean value but none seemed to work.

    Ok, so I have the following code:

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    {
    	bool Turn = true;
    
    
    	for (int i = 1; i < 11; i++)
    	{
    		if (Turn = true)
    		{
    			cout<<"X";
    		}
    		else if (Turn = false)
    		{
    			cout<<"O";
    		}
    
    
    	}
    
    
    	cin.get();
    }
    As of now the code prints "XXXXXXXXXX". And I want it to print "XOXOXOXOXO", so I tried 2 different things, which did not work:

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    {
    	bool Turn = true;
    
    
    	for (int i = 1; i < 11; i++)
    	{
    		if (Turn = true)
    		{
    			cout<<"X";
    		}
    		else if (Turn = false)
    		{
    			cout<<"O";
    		}
    
    
    		Turn = !Turn;
    	}
    
    
    	cin.get();
    }
    AND:

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    {
    	bool Turn = true;
    
    
    	for (int i = 1; i < 11; i++)
    	{
    		if (Turn = true)
    		{
    			cout<<"X";
    		}
    		else if (Turn = false)
    		{
    			cout<<"O";
    		}
    
    
    		if (Turn = true)
    		{
    			Turn = false;
    		}
    		else if (Turn = false)
    		{
    			Turn = true;
    		}
    	}
    
    
    	cin.get();
    }

    Ok so I made those changes so that the boolean 'Turn' would change everytime the loop executed. Guess it doesn't work. Can anyone give me a hand?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First you need to learn the difference between the assignment operator= and the comparison operator==. Look at the following snippet:
    Code:
    	for (int i = 1; i < 11; i++)
    	{
    		if (Turn = true)
    		{
    			cout<<"X";
    		}
    		else if (Turn = false)
    		{
    			cout<<"O";
    		}
    In this snippet you are using the assignment operator= so every time the loop runs it assigns true to Turn.

    Also note you don't need to use the else if(), a simple if is sufficient and you don't even need to use the comparison operator in this if statement.

    Code:
    if(Turn)
    {
       // do things.
    }
    else
    {
       // do other things.
    }
    Jim

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Thanks for the explanation, I tend to mix up "==" and "=" but now I see the difference clearly.
    Fixed it now. Double thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. write() and read() int values from socket returning wrong values.
    By Premjith P S in forum Linux Programming
    Replies: 8
    Last Post: 11-29-2012, 02:59 PM
  2. passing int values and *double values through message queues
    By Hyp3rTension in forum C Programming
    Replies: 11
    Last Post: 05-11-2012, 05:04 AM
  3. Negating a bitmap image
    By o.fithcheallaig in forum C Programming
    Replies: 14
    Last Post: 01-22-2012, 08:50 PM
  4. Boolean Values
    By guitarman32 in forum C++ Programming
    Replies: 17
    Last Post: 04-06-2006, 12:54 PM
  5. Replies: 4
    Last Post: 04-02-2004, 07:30 PM