Off Topic: The Flood
This topic has moved here: Subject: Can anyone help me with the "Bool" variable in C++
  • Subject: Can anyone help me with the "Bool" variable in C++
Subject: Can anyone help me with the "Bool" variable in C++
  • gamertag: [none]
  • user homepage:

"You are, all of you, vermin. Cowering in the dirt, thinking... what? That you might escape the coming fire? Your world will burn until its surface is but glass! "

-The Prophet of truth

So could anyone demonstrate it in an if or if else statement?

Common usages?

  • 01.07.2013 12:05 PM PDT
  • gamertag: [none]
  • user homepage:

You can ask John Cena

  • 01.07.2013 12:07 PM PDT


Posted by: My Porch Died

Stop.
Just stop

  • 01.07.2013 12:08 PM PDT
  •  | 
  • Exalted Mythic Member
  • gamertag: [none]
  • user homepage:

Booleans are always true (1) or false (0). They're mostly used in a yes-or-no situation. So the conditional part of an if statement for a boolean named yesorno would be "if(yesorno == true)" or "if(yesorno == false)". Is there something specific you're trying to accomplish with them?

[Edited on 01.07.2013 12:11 PM PST]

  • 01.07.2013 12:11 PM PDT

Allons-y!

I'm not using C++, but it's similar. Anywho, boolean values are either true or false. If/Else is a way of expressing that. So say:

If xxx==true
Do: xxx

Else
Do:
xxx



Best representation I can give.

  • 01.07.2013 12:22 PM PDT

bool theBoolean = true;

if(theBoolean)
{
//This statement will be executed.
cout << "The If Statement was True!";
}
else
{
cout << "The If Statement was False!";
}

theBoolean = false;

if(theBoolean)
{
cout << "The If Statement was True!";
}
else
{
//This statement will be executed.
cout << "The If Statement was False!";
}

  • 01.07.2013 12:24 PM PDT

Per Audacia Ad Astra

bool bValue = true;
if (bValue)
cout << "bValue was true" << endl;
else
cout << "bValue was false" << endl;

  • 01.07.2013 12:25 PM PDT

Halo: CE Anniversary Achievement Idea
C-C-C-CANNON BREAKER!
Let Sgt. Johnson die on Halo.

Posted by: HaloBeforeHoes
So could anyone demonstrate it in an if or if else statement?

Common usages?


A bool simply denotes to true or false. It's great because it helps you track what is going on in your program. Let's say you have a simple program that adds two numbers for you. However, you want the program to keep running until you tell it to stop. Here's a very cut and dry example of how bools can do just that.
------------------
bool programRepeat = true;

while (programRepeat == true)
{
// Code goes here
// Ask user if program should continue
// Set programRepeat to false if the user is done
// Program ends once the while loop finishes
}

[Edited on 01.07.2013 12:29 PM PST]

  • 01.07.2013 12:28 PM PDT
  • gamertag: [none]
  • user homepage:

"You are, all of you, vermin. Cowering in the dirt, thinking... what? That you might escape the coming fire? Your world will burn until its surface is but glass! "

-The Prophet of truth


Posted by: L00
Is there something specific you're trying to accomplish with them?


Yes, I am trying to use them in a program that tells me a password.
In the program a question is asked and the password is granted if you have the right response. Would the user be able to enter "true" or "false" as a response to a question if I use bool as you showed?

  • 01.07.2013 12:29 PM PDT

Per Audacia Ad Astra

Common uses of a bool variable?
I'd use them instead of integers for whether certain code should be processed.

E.g. (in pseudo-code format)
if (enemy attack == true && player shielding == false)
{
playerdamage(enemy weapon damage, player health);
flinch();
}

  • 01.07.2013 12:32 PM PDT
  •  | 
  • Fabled Legendary Member


Posted by: HaloBeforeHoes

Posted by: L00
Is there something specific you're trying to accomplish with them?


Yes, I am trying to use them in a program that tells me a password.
In the program a question is asked and the password is granted if you have the right response. Would the user be able to enter "true" or "false" as a response to a question if I use bool as you showed?

They could enter "true" but it wouldn't work (unless the answer was also "true") because they're entering a string, not a boolean. Something like:

if (response = answer)
return password
else
return "wrong"

[Edited on 01.07.2013 12:33 PM PST]

  • 01.07.2013 12:33 PM PDT

Halo: CE Anniversary Achievement Idea
C-C-C-CANNON BREAKER!
Let Sgt. Johnson die on Halo.

Posted by: HaloBeforeHoes

Posted by: L00
Is there something specific you're trying to accomplish with them?


Yes, I am trying to use them in a program that tells me a password.
In the program a question is asked and the password is granted if you have the right response. Would the user be able to enter "true" or "false" as a response to a question if I use bool as you showed?


Are you using character arrays or Strings to process the question's response?

  • 01.07.2013 12:36 PM PDT

Per Audacia Ad Astra

if (input == password)
{
login == true;
}

Although I'd save the hassle of making a bool and simply;
if (input == password)
{
login();
}

  • 01.07.2013 12:36 PM PDT
  • gamertag: [none]
  • user homepage:

"You are, all of you, vermin. Cowering in the dirt, thinking... what? That you might escape the coming fire? Your world will burn until its surface is but glass! "

-The Prophet of truth


Posted by: DTA MoonDawg
Posted by: HaloBeforeHoes

Posted by: L00
Is there something specific you're trying to accomplish with them?


Yes, I am trying to use them in a program that tells me a password.
In the program a question is asked and the password is granted if you have the right response. Would the user be able to enter "true" or "false" as a response to a question if I use bool as you showed?


Are you using character arrays or Strings to process the question's response?


Wow lol I just figured out what those two are. I plan to use character arrays.

  • 01.07.2013 12:52 PM PDT

Halo: CE Anniversary Achievement Idea
C-C-C-CANNON BREAKER!
Let Sgt. Johnson die on Halo.

Posted by: HaloBeforeHoes

Posted by: DTA MoonDawg
Posted by: HaloBeforeHoes

Posted by: L00
Is there something specific you're trying to accomplish with them?


Yes, I am trying to use them in a program that tells me a password.
In the program a question is asked and the password is granted if you have the right response. Would the user be able to enter "true" or "false" as a response to a question if I use bool as you showed?


Are you using character arrays or Strings to process the question's response?


Wow lol I just figured out what those two are. I plan to use character arrays.


Personally, I would prefer Strings over character arrays for what you are trying to do. I've been working with Java this past semester, so I could be wrong, but I distinctly remember being able to compare two strings in C++ with the "==" operator. You don't have to mess with any string compare functions to see if the response is correct. If you use character arrays, you may have to iterate through the entire array to see if each character matches.

ALSO, character arrays can only be set to a specified length. If you add any more characters than that, your program will crash. Strings have no limit.

  • 01.07.2013 12:57 PM PDT

Dear Floodians:

A Girl thread a day keeps the ladies at bay and gets us banned, the lonely single way.

Protip, never do this:

if(bool==true){...}

Only newbies do that.

Always do it like this:
if(bool){...}

  • 01.07.2013 1:06 PM PDT

Currently studying Computer Science & Software engineering. Hope to work on mobile devices of the future! When a certain game's credits roll, look out for my name! ;)

You will -blam!- bricks when you see what game it is! =)


Posted by: Thunderjam0
Protip, never do this:

if(bool==true){...}

Only newbies do that.

Always do it like this:
if(bool){...}


Ive heard that some languages work on negative logic, so just watch out for those. As long as youre in C++ this is fine.

  • 01.07.2013 1:10 PM PDT
  •  | 
  • Exalted Legendary Member
  • gamertag: Btcc22
  • user homepage:


Posted by: Thunderjam0
Protip, never do this:

if(bool==true){...}

Only newbies do that.

Always do it like this:
if(bool){...}


There are some languages and scenarios in which you need to be explicit about this. :)

[Edited on 01.07.2013 1:12 PM PST]

  • 01.07.2013 1:12 PM PDT
  • gamertag: [none]
  • user homepage:

"You are, all of you, vermin. Cowering in the dirt, thinking... what? That you might escape the coming fire? Your world will burn until its surface is but glass! "

-The Prophet of truth

Oh guys another question.

Could user Cin the bool value

Example:
bool AQ
Question
cin>>AQ;
If (AQ == True)
Do this
Else
Do that



Would that work?

  • 01.07.2013 2:29 PM PDT
  •  | 
  • Exalted Legendary Member
  • gamertag: Btcc22
  • user homepage:

The easiest way to find out the answers to these questions is to try it. :)

  • 01.07.2013 5:00 PM PDT
  • gamertag: [none]
  • user homepage:

Posted by: x Foman123 x
Posted by: jaythenerdkid
I definitely believe every wildly inflated claim ITT.

Weow kid, do you even lift?


Skype: au-simon

Posted by: Btcc22
The easiest way to find out the answers to these questions is to try it. :)

  • 01.07.2013 5:03 PM PDT