Can you store / convert int values ​​in bool variables?

I want to read a file consisting of ones and zeros on each line. How can I store false

in any variable bool

after reading 0

from a file.

+2


a source to share


2 answers


Just read the boolean variable:

bool b;
file >> b;

      

where file is your input file stream. 1 will be converted to true, from 0 to false.



If you've read the line, the obvious way to do the conversion is to look at the first character:

string s = .... // populate
bool b = s[0] != '0';

      

but I don't understand why you were reading the line if what you really want is a bool.

+6


a source


bool b = (variableFromFile != 0);

      



+2


a source







All Articles