noob question

Sergey Gromov snake.scaly at gmail.com
Thu Aug 6 18:22:31 PDT 2009


Thu, 06 Aug 2009 10:56:33 -0400, lllTattoolll wrote:

> 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 ) */

The format string must be "Hello %s %s" to display both operands.  Or
you can use writeln instead:

	writeln("Hello ", _name, " ", _lastname);

> 	writefln ("Year of birth: ");
> 	scanf ("%d", & _birth);

You use scanf here.  Scanf only reads the number, and leaves the <Enter>
symbol in the input stream.  When you later call readln() it sees that
<Enter> from the previous question and exits immediately.  You better
use readln() everywhere:

	_birth = std.conv.to!int(readln().chomp);

> 	_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 */
> }

The rest seems to work correctly.


More information about the Digitalmars-d-learn mailing list