www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - noob question

reply llltattoolll <alarrama gmail.com> writes:
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 );
}
 
Aug 06 2009
parent reply "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
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
Aug 06 2009
parent reply "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
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
Aug 06 2009
parent reply lllTattoolll <alarrama gmail.com> writes:
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
Aug 06 2009
next sibling parent BCS <ao pathlink.com> writes:
Reply to llltattoolll,

   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 all else fails, what are you getting writef(">>%s<<\n", cast(ubyte[])chomp(_response)); // might it be a case error? "YES" vs. "yes"?
   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 */
Aug 06 2009
prev sibling parent reply Sergey Gromov <snake.scaly gmail.com> writes:
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.
Aug 06 2009
parent llltattoolll <alarrama gmail.com> writes:
thx sergey my example work correctly now :D
Aug 07 2009