digitalmars.D.learn - noob question
- llltattoolll (32/32) Aug 06 2009 hi im noob here and try learn D language, i try make this example but wh...
- Lars T. Kyllingstad (6/52) Aug 06 2009 Hello,
- Lars T. Kyllingstad (12/73) Aug 06 2009 I didn't notice your first question, but the answer is the same. To
- lllTattoolll (30/109) Aug 06 2009 hi Lars and thx for help. I fix a little the first problem, now I paste ...
- BCS (6/12) Aug 06 2009 // if all else fails, what are you getting
- Sergey Gromov (10/48) Aug 06 2009 The format string must be "Hello %s %s" to display both operands. Or
- llltattoolll (1/1) Aug 07 2009 thx sergey my example work correctly now :D
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
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
Lars T. Kyllingstad wrote:llltattoolll wrote: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; ... -Larshi 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
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: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; ... -Larshi 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
Reply to llltattoolll,writefln ("Your age is %d? \t YES \t NO", _age ); /* here is my secondproblem, 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 alwaysshow me because canīt validate _response */
Aug 06 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.
Aug 06 2009