Printing out Boolean values

I am getting 1 and 0 I need true and false.

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 6080 Views - Last Post: 16 February 2010 - 07:01 PM Rate Topic: -----

#1 GCheek  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 92
  • Joined: 31-January 10

Printing out Boolean values

Posted 16 February 2010 - 10:17 AM

Please explain how I get this to print to console True and False instead of just 1 and 0.
double userInt;
   bool charString(6);

   cout << "\n Please enter an integer: ";
   cin >> userInt;


   if (userInt != userInt /2 == 0)
   charString = false;
   else if (userInt == userInt/2 == 0)
   charString = true;

   cout << "          Odd: "<<charString<< "\n";



My teacher said cout << boolalpha will print true and false but I cant seem to get that to work. Please explain.

Is This A Good Question/Topic? 0
  • +

Replies To: Printing out Boolean values

#2 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: Printing out Boolean values

Posted 16 February 2010 - 10:21 AM

use "" around true or false. true and false are just keywords that represent the unsigned integers 1 and 0.
Was This Post Helpful? 0
  • +
  • -

#3 GCheek  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 92
  • Joined: 31-January 10

Re: Printing out Boolean values

Posted 16 February 2010 - 10:26 AM

I tried that and it gives me this error
warning C4305: 'initializing' : truncation from 'int' to 'bool'

lol nm I was getting that warning before but it still isn't giving me the true false result I am looking for lol
Was This Post Helpful? 0
  • +
  • -

#4 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: Printing out Boolean values

Posted 16 February 2010 - 10:30 AM

declare charString as a string of characters, not a string of boolean values.
Was This Post Helpful? 0
  • +
  • -

#5 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Printing out Boolean values

Posted 16 February 2010 - 10:31 AM

View PostGCheek, on 16 February 2010 - 04:17 PM, said:

Please explain how I get this to print to console True and False instead of just 1 and 0.
   double userInt;
   bool charString(6);

   cout << "\n Please enter an integer: ";
   cin >> userInt;


   if (userInt != userInt /2 == 0)
   charString = false;
   else if (userInt == userInt/2 == 0)
   charString = true;

   cout << "          Odd: "<<charString<< "\n";



My teacher said cout << boolalpha will print true and false but I cant seem to get that to work. Please explain.


A few misunderstandings here. Firstly bool charString(6) is totally incorrect. You need to define a six character array. That is large enough to contain both "true" and "false" withthe null terminating character.
To do this you would say

   char charString[6];



So [] brackets are used in arrays, not () parenthesis.

Assigning the result to charString defined as 'char charString[6];' means that you would have to use the strcpy() routine. As you are using cin and cout, then I can assume you are writing in C++. So instead of writing 'char charString[6];' you can make use of the <string> operator. This means you can write

string charString;



and then use

   if (userInt != userInt /2 == 0)
   charString = "false"; // notice the double quotes around false to let the compiler know it's a string
   else if (userInt == userInt/2 == 0)
   charString = "true"; // notice the double quotes around false to let the compiler know it's a string



The only other thing you need to do if you go for that approach is to remember to '#include <string>' at the top of the program.

Hope that helps.

This post has been edited by Martyn.Rae: 16 February 2010 - 10:32 AM

Was This Post Helpful? 0
  • +
  • -

#6 GCheek  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 92
  • Joined: 31-January 10

Re: Printing out Boolean values

Posted 16 February 2010 - 10:38 AM

I am required in this assignment to use use boolean type variables. I guess I should have mentioned that.

double userInt;
   bool result_x;

   cout << "\n Please enter an integer: ";
   cin >> userInt;


   if (userInt != userInt /2 == 0)
   result_x = false;
   else if (userInt == userInt/2 == 0)
   result_x = true;

   cout << "          Odd: "<<result_x<< "\n";



This is my updated code sorry I was playing with the string thing and forgot to change it back lol.
Was This Post Helpful? 0
  • +
  • -

#7 GCheek  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 92
  • Joined: 31-January 10

Re: Printing out Boolean values

Posted 16 February 2010 - 10:59 AM

Can anyone help me with this?
Was This Post Helpful? 0
  • +
  • -

#8 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: Printing out Boolean values

Posted 16 February 2010 - 11:06 AM

add an extra if and else statement after setting result_x and print a line corresponding to the value of that result.

if(result_x) cout << "True" << endl;
else cout << "False" << endl;



Something along those lines.
Was This Post Helpful? 0
  • +
  • -

#9 FrozenSnake  Icon User is offline

  • En man från Sverige!

Reputation: 122
  • View blog
  • Posts: 995
  • Joined: 30-July 08

Re: Printing out Boolean values

Posted 16 February 2010 - 11:10 AM

Then use use bool and still print out true or false.

bool result_x;
// other code ...
if(result_x == true)
	cout << "True";
else
	cout << "False";


http://www.cpprefere.../keywords/start <-- a list of keywords in c++

* edit *
Oh Bodom658 was faster!

This post has been edited by FrozenSnake: 16 February 2010 - 11:11 AM

Was This Post Helpful? 0
  • +
  • -

#10 GCheek  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 92
  • Joined: 31-January 10

Re: Printing out Boolean values

Posted 16 February 2010 - 11:18 AM

I understand how to manually input text into a cout statement. The point of this excercise is to automatically output the words True or False based on the outcome of the statement and its boolean result this is what I need help with.. How would I integrate the Boolalpha statement into this?
Was This Post Helpful? 0
  • +
  • -

#11 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: Printing out Boolean values

Posted 16 February 2010 - 11:21 AM

cout << boolalpha << result_x << endl;

This post has been edited by bodom658: 16 February 2010 - 11:22 AM

Was This Post Helpful? 1
  • +
  • -

#12 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Printing out Boolean values

Posted 16 February 2010 - 11:27 AM

Everyone who's replied to this thread so far is overcomplicating this - C++ will do it automatically with a stream manipulator called boolalpha
#include <iostream>
#include <iomanip>

int main()
{
    bool b = true;
    std::cout << std::boolalpha << b << std::endl;
    b = false;
    std::cout << std::boolalpha << b << std::endl;
} 




edit - nevermind - I didn't see bodom658's reply til after posting

This post has been edited by Bench: 16 February 2010 - 11:31 AM

Was This Post Helpful? 1
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,591
  • Joined: 16-October 07

Re: Printing out Boolean values

Posted 16 February 2010 - 11:34 AM

This works:
bool foo = false;
cout << "foo = " << (foo?"True":"False") << endl;



Edit: Oops, missed the invocation of "boolalpha". At the same time, the alpha is all lower case on my box.

This post has been edited by baavgai: 16 February 2010 - 11:36 AM

Was This Post Helpful? 1
  • +
  • -

#14 GCheek  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 92
  • Joined: 31-January 10

Re: Printing out Boolean values

Posted 16 February 2010 - 05:49 PM

this is my code so far. For some reason its not actually doing the math and no matter what its returning a true value. What am I doing wrong?
 bool result_x;
   double userInt;
   
   cout << "\n Please enter an integer: ";
   cin >> userInt;


   if (userInt != userInt / 2 == 0)
	result_x = false;
   else if (userInt == userInt / 2 == 0)
	result_x = true;

   cout <<"          Odd: "<< boolalpha << result_x << "\n";


Was This Post Helpful? 0
  • +
  • -

#15 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,591
  • Joined: 16-October 07

Re: Printing out Boolean values

Posted 16 February 2010 - 06:10 PM

You are saying strange things here: (userInt == userInt / 2 == 0)

You are asserting that all three of those values are equal. Clearly, this is never true "userInt == userInt / 2".

To find oddness, you can simply do this:
int userInt;
cout << "\n Please enter an integer: ";
cin >> userInt;
	
bool isOdd = (userInt % 2)!=0;



After that, print it out using any of the offered options.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2