www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use "read_bool"?

reply "Zeh" <zecacu yahoo.com.br> writes:
Hi, i am just a newbie trying learn D. But, i get having some 
trouble with "read_bool". More specifically on program of this 
lesson:

import std.stdio;
import std.conv;
import std.string;

void main()
{
     write("How many are we? ");
     int personCount;
     readf(" %s", &personCount);

     write("How many bicycles are there? ");
     int bicycleCount;
     readf(" %s", &bicycleCount);

     write("What is the distance to the beach? ");
     int distance;
     readf(" %s", &distance);

     bool existsCar = read_bool("Is there a car? ");
     bool existsLicense =
         read_bool("Is there a driver license? ");

When i try compile this, i get the following errors:

hello.d|43|Error: undefined identifier read_bool|
hello.d|44|Error: undefined identifier read_bool|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 0 seconds) 
===|

So, someone know what's happening? What is the correct way to use 
the Read_bool?

Thanks for everyone!
Aug 02 2012
next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 08/03/2012 06:17 AM, Zeh wrote:
 Hi, i am just a newbie trying learn D. But, i get having some trouble
 with "read_bool". More specifically on program of this lesson:

 import std.stdio;
 import std.conv;
 import std.string;

 void main()
 {
 write("How many are we? ");
 int personCount;
 readf(" %s", &personCount);

 write("How many bicycles are there? ");
 int bicycleCount;
 readf(" %s", &bicycleCount);

 write("What is the distance to the beach? ");
 int distance;
 readf(" %s", &distance);

 bool existsCar = read_bool("Is there a car? ");
 bool existsLicense =
 read_bool("Is there a driver license? ");

 When i try compile this, i get the following errors:

 hello.d|43|Error: undefined identifier read_bool|
 hello.d|44|Error: undefined identifier read_bool|
 ||=== Build finished: 2 errors, 0 warnings (0 minutes, 0 seconds) ===|

 So, someone know what's happening? What is the correct way to use the
 Read_bool?

 Thanks for everyone!
presumably read_bool is part of the tutorial. Otherwise you may use this: bool read_bool(){ auto str = readln()[0..$-1]; switch(str){ case "true", "y", "ye", "yes": return true; case "false", "n", "no": return false; default: throw new Exception("please answer yes or no"); } }
Aug 02 2012
prev sibling next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 08/02/2012 09:17 PM, Zeh wrote:
 Hi, i am just a newbie trying learn D. But, i get having some trouble
 with "read_bool". More specifically on program of this lesson:
As Timon said, read_bool() is a separate function on the same page, a little after main(): bool read_bool(string message) { // Print the message writef(message ~ "(false or true) "); // Read the line as a string string input; while (input.length == 0) { input = chomp(readln()); } // Produce a 'bool' value from that string bool result = to!bool(input); // Return the result to the caller return result; } Unlike Timon's function, you must enter either "false" or "true" with the one above. Ali
Aug 02 2012
parent reply simendsjo <simendsjo gmail.com> writes:
On Fri, 03 Aug 2012 08:05:46 +0200, Ali =C3=87ehreli <acehreli yahoo.com=
 wrote:
 On 08/02/2012 09:17 PM, Zeh wrote:
  > Hi, i am just a newbie trying learn D. But, i get having some troub=
le
  > with "read_bool". More specifically on program of this lesson:

 As Timon said, read_bool() is a separate function on the same page, a =
=
 little after main():

 bool read_bool(string message)
 {
      // Print the message
      writef(message ~ "(false or true) ");

      // Read the line as a string
      string input;
      while (input.length =3D=3D 0) {
          input =3D chomp(readln());
      }

      // Produce a 'bool' value from that string
      bool result =3D to!bool(input);

      // Return the result to the caller
      return result;
 }

 Unlike Timon's function, you must enter either "false" or "true" with =
=
 the one above.

 Ali
What tutorials is this? Here's another version: import std.stdio, std.string, std.conv; bool read_bool(in string message) { // in as we don't escape or modify t= he = input while(true) { // loop until we get true/false write(message, " (false or true): "); // avoid concat allocatio= n string input; do { // no need to check input on entering input =3D readln().chomp(); // UFCS } while(!input); // no need to check length try return input.to!bool; catch {} // don't care if it's not true/false. Keep nagging } } void main() { auto input =3D read_bool("Do it?"); writeln(input ? "Hell yeah, do it!" : "No way!"); }
Aug 03 2012
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 08/03/2012 09:12 AM, simendsjo wrote:

 What tutorials is this?
The function is at the very end of the following chapter: http://ddili.org/ders/d.en/logical_expressions.html Ali
Aug 03 2012
parent reply simendsjo <simendsjo gmail.com> writes:
On Fri, 03 Aug 2012 19:30:25 +0200, Ali =C3=87ehreli <acehreli yahoo.com=
 wrote:
 On 08/03/2012 09:12 AM, simendsjo wrote:

  > What tutorials is this?

 The function is at the very end of the following chapter:

    http://ddili.org/ders/d.en/logical_expressions.html

 Ali
Ah, ok. I still haven't read the chapters you've translated. Guess I should do this as you give away all your hard work for free. Do you mind me asking why you give away the translation for free btw? Yo= ur = selling the Turkish version, right?
Aug 03 2012
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 08/03/2012 10:58 AM, simendsjo wrote:

 Guess I should do this as you give away all your hard work for free.
Thank you! I am making it better little by little every day.
 Do you mind me asking why you give away the translation for free btw?
I think all of the following: This is my project for fun. I don't think the gains would make any difference anyway. It is not complete yet.
 Your selling the Turkish version, right?
No, the Turkish version is free as well. :) Some people complain that there is no way of showing material appreciation. There are some thoughts and efforts by others to make it a paper book. Ali -- D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html
Aug 03 2012
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Ali Çehreli:

 No, the Turkish version is free as well. :) Some people 
 complain that there is no way of showing material appreciation.
Human psychology is not linear :-) Sometimes if you pay some (a little) money you think of it as more important. Sometimes if you have a way to give back to the author you feel better, and this makes you appreciate more the work. Money isn't just for money. Money has also effects on the mind of people :-) Bye, bearophile
Aug 03 2012
parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Fri, Aug 3, 2012 at 8:59 PM, bearophile <bearophileHUGS lycos.com> wrote:

 Money isn't just for money. Money has also effects on the mind of people :-)
Well money is nothing but effect in our mind... I know that, for quite some time now, whenever I find a book as a free pdf and I like it, I make a point to buy it anew, as a paper version, so as to transmit some money to the author. Going back to the current subject, we cannot help Ali any, without knowing Turkish. I *guess* some of us could start a 2nd level translation (japanese, russian, italian, german and french are likely possibilities).
Aug 03 2012
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 08/03/2012 12:17 PM, Philippe Sigaud wrote:

 some of us could start a 2nd level
 translation (japanese, russian, italian, german and french are likely
 possibilities).
How timely! Crimson Sphere has just started the Korean translation: http://p-crimsonsphere.blogspot.kr/p/programming-in-d.html The "Hello World" chapter has some recognizable code: :) http://p-crimsonsphere.blogspot.kr/2012/08/hello-world.html Ali
Aug 03 2012
parent "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Friday, 3 August 2012 at 19:25:21 UTC, Ali Çehreli wrote:
 On 08/03/2012 12:17 PM, Philippe Sigaud wrote:

 some of us could start a 2nd level translation (japanese, 
 russian, italian, german and french are likely possibilities).
How timely! Crimson Sphere has just started the Korean translation: http://p-crimsonsphere.blogspot.kr/p/programming-in-d.html The "Hello World" chapter has some recognizable code: :) http://p-crimsonsphere.blogspot.kr/2012/08/hello-world.html
Ouch those pages just hurt my eyes. I had enough of the Korean language when I was in Korea. However it is a good thing it's getting translated and more coverage; I just hope most of the comments (and code) stay in english since it's kinda de-facto for most computer languages.
Aug 03 2012
prev sibling parent reply "Zeh" <zecacu yahoo.com.br> writes:
Thanks for the help, but i tryed both solutions posted and still 
not working. :/

I get erros to compile the code posted by simendsjo. I try modify 
at my own, but without success. The code suggest by Timon Gehr 
compiles, but not work.

Honestly speaking, i didn't have enough knowledge to understand 
the solutions offered (yet). What is the smallest way to use the 
"read_bool"?


Besides, one version of that tutorial in portuguese would be 
great =D
Aug 03 2012
next sibling parent reply simendsjo <simendsjo gmail.com> writes:
On Sat, 04 Aug 2012 03:01:31 +0200, Zeh <zecacu yahoo.com.br> wrote:

 Thanks for the help, but i tryed both solutions posted and still not  
 working. :/

 I get erros to compile the code posted by simendsjo. I try modify at my  
 own, but without success. The code suggest by Timon Gehr compiles, but  
 not work.

 Honestly speaking, i didn't have enough knowledge to understand the  
 solutions offered (yet). What is the smallest way to use the "read_bool"?


 Besides, one version of that tutorial in portuguese would be great =D
This works: import std.stdio, std.string, std.conv; bool read_bool(in string message) { while(true) { write(message, " (false or true): "); string input; do { input = readln().chomp(); } while(!input.length); writeln("INPUT: '", input, "'"); try return input.to!bool(); catch {} } } void main() { write("How many are we? "); int personCount; readf(" %s", &personCount); write("How many bicycles are there? "); int bicycleCount; readf(" %s", &bicycleCount); write("What is the distance to the beach? "); int distance; readf(" %s", &distance); bool existsCar = read_bool("Is there a car? "); bool existsLicense = read_bool("Is there a driver license? "); } $ rdmd read_bool How many are we? 10 How many bicycles are there? 1 What is the distance to the beach? 2 Is there a car? (false or true): true Is there a driver license? (false or true): false
Aug 04 2012
parent "Zeh" <zecacu yahoo.com.br> writes:
On Saturday, 4 August 2012 at 08:35:36 UTC, simendsjo wrote:
 On Sat, 04 Aug 2012 03:01:31 +0200, Zeh <zecacu yahoo.com.br> 
 wrote:

 Thanks for the help, but i tryed both solutions posted and 
 still not working. :/

 I get erros to compile the code posted by simendsjo. I try 
 modify at my own, but without success. The code suggest by 
 Timon Gehr compiles, but not work.

 Honestly speaking, i didn't have enough knowledge to 
 understand the solutions offered (yet). What is the smallest 
 way to use the "read_bool"?


 Besides, one version of that tutorial in portuguese would be 
 great =D
This works: import std.stdio, std.string, std.conv; bool read_bool(in string message) { while(true) { write(message, " (false or true): "); string input; do { input = readln().chomp(); } while(!input.length); writeln("INPUT: '", input, "'"); try return input.to!bool(); catch {} } } void main() { write("How many are we? "); int personCount; readf(" %s", &personCount); write("How many bicycles are there? "); int bicycleCount; readf(" %s", &bicycleCount); write("What is the distance to the beach? "); int distance; readf(" %s", &distance); bool existsCar = read_bool("Is there a car? "); bool existsLicense = read_bool("Is there a driver license? "); } $ rdmd read_bool How many are we? 10 How many bicycles are there? 1 What is the distance to the beach? 2 Is there a car? (false or true): true Is there a driver license? (false or true): false
Very interesting way to solve the problem (at least for a noobie like me ^^ ). Thanks for the help!
Aug 06 2012
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 08/04/2012 03:01 AM, Zeh wrote:
 Thanks for the help, but i tryed both solutions posted and still not
 working. :/

 I get erros to compile the code posted by simendsjo. I try modify at my
 own, but without success. The code suggest by Timon Gehr compiles, but
 not work.
Works for me. Maybe readln behaves differently on windows?
Aug 06 2012