Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

can i use words in bool ?

+6 votes
asked Jun 12, 2022 by Adam Mohamed (180 points)
#include<iostream>

using namespace std;

int main()

{

bool y="Yes";

cout<<"can any one answer me"<<y<<endl;

}

1 Answer

+1 vote
answered Jun 14, 2022 by Peter Minarik (84,720 points)

No, you can not. A bool takes the values true or false. The literal "Yes" is a string, stored in std::string or a const char * or char * for a more C-like type.

That being said, your code compiles, because any number can be automatically cast to a boolean type. The string literal "Yes" is really just a memory address, where this string is found. And a memory address is a number. If it is zero (null), it is false, if it is non-zero, the boolean value of it is true.

That's why your code compiles and runs, but y does not actually hold the string literal "Yes".

commented Jun 15, 2022 by NITIN PAL (110 points)
error the code bool is used only true and false statement .
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...