www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Reading bool as the string "true" or "false"

I am trying to learn to read bool as a string and have it converted to bool.
Too much to ask? :)

This is what I know in C++:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    cin >> boolalpha;
    cout << boolalpha;

    bool b = false;
    cin >> b;
    cout << b << endl;
}

cin and cout use "false" and "true" for bool in that program.

I know that 'bool alpha' is the default for D for the output, but how about the
input? These attempts failed for me with dmd v2.029:

import std.cstream;

void main()
{
    bool b;

    // Desired:
    din.readf(&b);
    // Segmentation fault when tried with false, 0, etc.

    // According to the std.format spec; but apparently for
    // output only:
    din.readf("%s", &b);
    // Segmentation fault when tried with false, 0, etc.

    // Again from the std.format spec because bool is perhaps
    // 'integral'?
    din.readf("%d", &b);
    // Segmentation fault when tried with false, 0, etc
}

Should I be using a different module? What am I doing wrong?

Thank you,
Ali
Jul 29 2009