7 Replies - 735 Views - Last Post: 07 March 2009 - 01:55 AM Rate Topic: -----

#1 soroush  Icon User is offline

  • D.I.C Head

Reputation: -8
  • View blog
  • Posts: 67
  • Joined: 03-February 09

boolean

Posted 06 March 2009 - 02:38 PM

Write a Boolean expression that is true when the bool variable moon has the value "blue" or the value "Blue"?
doesn't work well
#include<iostream>
using namespace std;
int main()
{
	bool check =true;
	
	string moon;
	moon="Blue";
	string ::size_type pos;
	pos=moon.find('B');
	
	if(true==pos)
		cout<<"found";
	else
		cout<<"not found";


system("pause");
	return 0;
}


*** MOD EDIT: Fixed code tags. ***

This post has been edited by JackOfAllTrades: 06 March 2009 - 02:43 PM


Is This A Good Question/Topic? 0
  • +

Replies To: boolean

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: boolean

Posted 06 March 2009 - 02:47 PM

A bool variable cannot have a value other than true or false. If that's your actual assignment, your teacher is an idiot.

string::find returns a number, the index to the character if it's found. It's equal to npos if the character is not found.
Was This Post Helpful? 0
  • +
  • -

#3 soroush  Icon User is offline

  • D.I.C Head

Reputation: -8
  • View blog
  • Posts: 67
  • Joined: 03-February 09

Re: boolean

Posted 06 March 2009 - 02:53 PM

View PostJackOfAllTrades, on 6 Mar, 2009 - 01:47 PM, said:

A bool variable cannot have a value other than true or false. If that's your actual assignment, your teacher is an idiot.

string::find returns a number, the index to the character if it's found. It's equal to npos if the character is not found.

I'm learng C++ from a book!and it's a question in the text book!how can I make it work in any way by using boolean?please help
Was This Post Helpful? 0
  • +
  • -

#4 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: boolean

Posted 06 March 2009 - 02:55 PM

I think you read it wrong because it is impossible for a bool variable to contain a string value.

It can only contain a boolean value which is:
0
1
false
true


Was This Post Helpful? 0
  • +
  • -

#5 soroush  Icon User is offline

  • D.I.C Head

Reputation: -8
  • View blog
  • Posts: 67
  • Joined: 03-February 09

Re: boolean

Posted 06 March 2009 - 02:59 PM

View Poste_barroga, on 6 Mar, 2009 - 01:55 PM, said:

I think you read it wrong because it is impossible for a bool variable to contain a string value.

It can only contain a boolean value which is:
0
1
false
true


I didn't read it wrong!
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: boolean

Posted 06 March 2009 - 03:00 PM

Maybe you all are reading too much into it?

int main( )  
{
   string moon = "blue";
   if(!(strcmp(moon.c_str(), "blue")) || !(strcmp(moon.c_str(), "Blue")))
	   cout << "string is blue!\n";
   else
	   cout << "string is not blue!\n";

	return 0;
}





To add the boolean:

int main( )  
{
   string moon = "blue";
   boolean isBlue;
   if(!(strcmp(moon.c_str(), "blue")) || !(strcmp(moon.c_str(), "Blue")))
	   isBlue = true;
   else
	  isBlue = false;

//do something with isBlue down here?

	return 0;
}


This post has been edited by KYA: 06 March 2009 - 03:02 PM

Was This Post Helpful? 0
  • +
  • -

#7 soroush  Icon User is offline

  • D.I.C Head

Reputation: -8
  • View blog
  • Posts: 67
  • Joined: 03-February 09

Re: boolean

Posted 06 March 2009 - 03:04 PM

View Postsoroush, on 6 Mar, 2009 - 01:59 PM, said:

View Poste_barroga, on 6 Mar, 2009 - 01:55 PM, said:

I think you read it wrong because it is impossible for a bool variable to contain a string value.

It can only contain a boolean value which is:
0
1
false
true


I didn't read it wrong!

YES I DID!!!!!I LOVE C++
#include<iostream>
using namespace std;
int main()
{
	bool check;
	string moon;
	moon="Blue";
	string ::size_type pos;
	pos=moon.find('b');
	
	if(pos!=4294967295)
		check=true;
	else 
		check=false;

		if(check)
		   cout<<"found";
		else
		   cout<<"not found";
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#8 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: boolean

Posted 07 March 2009 - 01:55 AM

You can assign the result of a boolean expression directly to a bool object; which means you can do this instead
#include <string>
#include <iostream>

int main()
{
    using namespace std;

    string moon = "Blue";
    bool check = moon.find('B') != string::npos;

    if( check )
        cout << "Found";
    else
        cout << "not found";
} 
the result of moon.find('B') != string::npos as a true/false value can be directly assigned to check; Any expression which can exist as part of an if condition can be assigned to a bool.



View PostKYA, on 6 Mar, 2009 - 10:00 PM, said:

   if(!(strcmp(moon.c_str(), "blue")) || !(strcmp(moon.c_str(), "Blue")))

There's no need to use strcmp at all - the whole point of having overloaded operators in the string type is to allow expressions like if( moon == "Blue" || moon == "blue" )

This post has been edited by Bench: 07 March 2009 - 02:03 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1