Re: how stdin stream works?

2020-08-23 Thread Flade via Digitalmars-d-learn

On Thursday, 20 August 2020 at 19:13:31 UTC, Ali Çehreli wrote:

On 8/19/20 11:46 AM, Flade wrote:


[...]


Thanks Steve! I will get the input a string then as you said 
and then I'll try to convert it! Thanks a lot, have a nice day!


In some cases clearerr() and readln() may be what is needed:

import std.stdio;

void main() {
  int x;

  bool accepted = false;

  while (!accepted) {
try {
  write("x: ");
  readf(" %d", x);
  accepted = true;

} catch (Exception msg) {

  writeln("Please give a right coordinate");
  stdin.clearerr();
  stdin.readln();
}
  }

  writeln("x is ", x);
}

Note that I used " %d" because "%d/n" would not clean when 
nothing was input by just hitting the Enter key. A space 
character in the format string means "read and ignore any 
whitespace at this point" and I like it. :)


Also note I changed the name of the variable as 'accepted'. :)

Ali


Wait! Oh man I just checked your name I'm currently reading 
your book and I wanted to say that you did an EXCELLENT work and 
I love it! Thanks a lot! You are great!!!


Re: how stdin stream works?

2020-08-23 Thread Flade via Digitalmars-d-learn

On Thursday, 20 August 2020 at 19:13:31 UTC, Ali Çehreli wrote:

On 8/19/20 11:46 AM, Flade wrote:


[...]


In some cases clearerr() and readln() may be what is needed:

import std.stdio;

void main() {
  int x;

  bool accepted = false;

  while (!accepted) {
try {
  write("x: ");
  readf(" %d", x);
  accepted = true;

} catch (Exception msg) {

  writeln("Please give a right coordinate");
  stdin.clearerr();
  stdin.readln();
}
  }

  writeln("x is ", x);
}

Note that I used " %d" because "%d/n" would not clean when 
nothing was input by just hitting the Enter key. A space 
character in the format string means "read and ignore any 
whitespace at this point" and I like it. :)


Also note I changed the name of the variable as 'accepted'. :)

Ali


Thanks a lot! Very useful info!!! And sorry for answering so 
late, I wasn't expecting someone to answer so I wasn't checking. 
Have a great day my friend!


Re: how stdin stream works?

2020-08-19 Thread Flade via Digitalmars-d-learn
On Wednesday, 19 August 2020 at 18:11:23 UTC, Steven 
Schveighoffer wrote:

On 8/19/20 1:44 PM, Flade wrote:
Hi everyone! I'm trying to do error handling (with the try 
block) and when I give a wrong value to the variable (it is an 
integer and I give a non-number value), then It doesn't let me 
re get input. The code:



int x;

bool not_accepted = false;

while (!not_accepted) {

     try {

     write("x: ");

     readf("%d\n", x);

     not_accepted = true;

} catch (Exception msg) {

     writeln("Please give a right coordinate");

     }


Probably readf stops as soon as it encounters an error, meaning 
that your input is still present.


Try instead getting a line via readln, and then trying to read 
that into your expected input.


-Steve


Thanks Steve! I will get the input a string then as you said and 
then I'll try to convert it! Thanks a lot, have a nice day!


how stdin stream works?

2020-08-19 Thread Flade via Digitalmars-d-learn
Hi everyone! I'm trying to do error handling (with the try block) 
and when I give a wrong value to the variable (it is an integer 
and I give a non-number value), then It doesn't let me re get 
input. The code:



int x;

bool not_accepted = false;

while (!not_accepted) {

try {

write("x: ");

readf("%d\n", x);

not_accepted = true;

} catch (Exception msg) {

writeln("Please give a right coordinate");

}


Re: can't access an alias created inside an if statement

2020-08-05 Thread Flade via Digitalmars-d-learn

On Wednesday, 5 August 2020 at 09:39:47 UTC, Simen Kjærås wrote:

On Wednesday, 5 August 2020 at 09:32:58 UTC, Flade wrote:
Thanks! You see it should work but the thing is. I'm using it 
inside a function. I'm checking for one of the function's 
parameter (if parameter == false) and it says that "the 
variable `parameter` cannot be read at compile time. Do you 
know if there is a way to fix this?


As the error message says, the value must be known at compile 
time. Most likely, you can simply pass it as a template 
parameter:


void fun(bool parameter)(int arg1, string arg2) {
static if (parameter) {
}
}

void main() {
fun!true(1, "foo");
fun!false(19, "bar");
}

--
  Simen


Thanks man! Works as expected! Have a great day!


Re: can't access an alias created inside an if statement

2020-08-05 Thread Flade via Digitalmars-d-learn

On Wednesday, 5 August 2020 at 09:25:23 UTC, Simen Kjærås wrote:

On Wednesday, 5 August 2020 at 09:05:36 UTC, Flade wrote:
I have used an if-else statement to create an alias to avoid 
code duplication but it doesn't let me access it outside the 
if statement. Is there a way to solve this?


You're probably looking for static if:

static if (useAlias) {
alias myAlias = getAlias!();
}

myAlias foo = getFoo();


What happens is a regular if statement introduces a scope, so 
anything declared inside it is unavailable outside. static if 
does not introduce a new scope, and so its contents can be 
accessed.


static if only works with compile-time constant conditions, but 
aliases are also compile-time constructs, so this should not 
pose a problem.


--
  Simen


Thanks! You see it should work but the thing is. I'm using it 
inside a function. I'm checking for one of the function's 
parameter (if parameter == false) and it says that "the variable 
`parameter` cannot be read at compile time. Do you know if there 
is a way to fix this?


can't access an alias created inside an if statement

2020-08-05 Thread Flade via Digitalmars-d-learn
I have used an if-else statement to create an alias to avoid code 
duplication but it doesn't let me access it outside the if 
statement. Is there a way to solve this?