May 16, 2022

On Monday, 16 May 2022 at 16:44:13 UTC, SvGaming wrote:

>

On Monday, 16 May 2022 at 16:40:59 UTC, SvGaming wrote:

>

On Sunday, 15 May 2022 at 20:06:20 UTC, kdevel wrote:

>

On Sunday, 15 May 2022 at 15:27:32 UTC, SvGaming wrote:
[...]

>

[...]

Until you post your full programm ideally in a reduced form [1] everybody who is willing to help must guess wildly what the unposted parts of your program does and how it may cause the read to be seemingly skipped.

[...]

I use dmd but I tried befor with another compiler and got the same result. Not sure if it was GDC or LDC.
Here is the repository on GitHub i just made for the code(i know it is quite messy, I am a begginer in D):

https://github.com/svgaming234/ezusb
forgot to post it in the previous reply, I am kind of stupid

May 16, 2022

On Monday, 16 May 2022 at 16:53:15 UTC, SvGaming wrote:
[...]

>

https://github.com/svgaming234/ezusb
forgot to post it in the previous reply, I am kind of stupid

In main your program reads an integer:

   int n;
   writef("Pick an option: ");
   readf(" %s", &n);

but your console/Terminal is line buffered, i.e. you type 1 plus a newline ("\n"). Only after the newline the OS transfers control back to your program. Unfortunately your program does not consume the newline, as ltrace reveals the newline is put back to the input buffer with ungetch. Therefore the next read command

   string a;
   a = readln();
   writeln(a);

consumes that newline from your first input resulting in variable a containing the empty string ending with a newline. Following change fixes the issue:

   int n;
   writef("Pick an option: ");
   readf(" %s\n", &n); // explicitly read the newline

I am not sure, why %s fits here. I would have expected a %d format for parsing the integer.

May 16, 2022
On 5/16/22 15:25, kdevel wrote:

>     string a;
>     a = readln();
>     writeln(a);

> consumes that newline from your first input resulting in variable a
> containing the empty string ending with a newline.

Great catch! I should put this combination in my chapter.

> I am not sure, why %s fits here. I would have expected a %d format for
> parsing the integer.

I learned to read %s as "whatever the type of the argument is" (not "string").

Ali

May 17, 2022

On Monday, 16 May 2022 at 22:25:23 UTC, kdevel wrote:

>

On Monday, 16 May 2022 at 16:53:15 UTC, SvGaming wrote:
[...]

>

[...]

In main your program reads an integer:

   int n;
   writef("Pick an option: ");
   readf(" %s", &n);

[...]
Thanks for the help!

May 17, 2022

On Monday, 16 May 2022 at 22:25:23 UTC, kdevel wrote:

>

On Monday, 16 May 2022 at 16:53:15 UTC, SvGaming wrote:
[...]

>

[...]

In main your program reads an integer:

   int n;
   writef("Pick an option: ");
   readf(" %s", &n);

[...]

Just tried the solution and it works perfectly!
Thanks, Again!

1 2
Next ›   Last »