noob question
lllTattoolll
alarrama at gmail.com
Thu Aug 6 07:56:33 PDT 2009
hi Lars and thx for help. I fix a little the first problem, now I paste a verion in english whit coment because i´m lost here.
the example is the same only this is in english for your compresion of my problem.
--------------------------------------------------
code
-------------------------------------------------
import std.stdio;
import std.string;
import std.c.stdio;
void main()
{
string _name, _lastname, _response;
int _age, _year, _birth;
_year = 2009;
writef ("Name: ");
_name = readln().chomp;
writef ("Lastname: ");
_lastname = readln().chomp;
writefln ("Hello %s ", _name, _lastname ); /* here is my first problem, dont show me the last name or show me in
two lines ( this fix whit .chomp ) */
writefln ("Year of birth: ");
scanf ("%d", & _birth);
_age = _year - _birth;
writefln ("Your age is %d? \t YES \t NO", _age ); /* here is my second problem, can´t into the response and program*/
_response = readln(); /* show me else line always */
if ( chomp(_response) == "yes")
writefln ("thank you %s", _name );
else
writefln ("Sorry %s you have %d", _name, _age - 1 ); /* this line always show me because can´t validate _response */
}
Lars T. Kyllingstad Wrote:
> 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