noob question

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Thu Aug 6 05:06:48 PDT 2009


Lars T. Kyllingstad wrote:
> llltattoolll wrote:
>> hi im noob here and try learn D language, i try make this example but 
>> when run have 2 problems.
>>
>> 1) when i show name and lastname show me in two lines and i wanna make 
>> in the same line.
>> 2) can´t validate option si ( yes ) or no ( no ) and always show the 
>> else line.
>>
>> what happend?
>> thx
>>
>> ------------------------------------------
>> code
>> ------------------------------------------
>> import std.stdio;
>> import std.string;
>> import std.c.stdio;
>>
>>
>>
>> void main()
>> {
>>
>> string _nombre, _apellido, _respuesta;
>> int _edad, _año, _nacimiento;
>> _año = 2009;
>>
>>     writefln ("Escribe tu nombre: ");
>>     _nombre = readln();
>>
>>     writefln ("Escribe tu apellido: ");
>>     _apellido = readln();
>>
>>     writefln ("Hola %s ", _nombre ~= _apellido);
>>
>>     writefln ("Ingresa el año de tu nacimiento: ");
>>     scanf ("%d", & _nacimiento);
>>
>>     _edad = _año - _nacimiento;
>>
>>     writefln ("Tu edad es %d? \t SI \t NO", _edad);
>>     _respuesta = readln();
>>
>>     if ( _respuesta == "si")
>>     writeln ("Muchas gracias %s", _nombre );
>>     else
>>     writeln ("Lo siento %s entonces tienes %d", _nombre, _edad - 1 );
>> }
>>  
> 
> 
> Hello,
> 
> I think std.stdio.readln() includes the trailing newline. Try this:
> 
>     if ( chomp(_respuesta) == "si")
> 
> The std.string.chomp() function removes trailing CR and LF characters.
> 
> -Lars


I didn't notice your first question, but the answer is the same. To 
solve both problems, you can do this instead:

   ...
   _nombre = chomp(readln());
   ...
   _apellido = chomp(readln());
   ...

Also, some people prefer this syntax, which is equivalent:

   _nombre = readln().chomp;
   ...

-Lars


More information about the Digitalmars-d-learn mailing list