www.digitalmars.com         C & C++   DMDScript  

D - I don't know much...

reply Carlos <Carlos_member pathlink.com> writes:
Hi. I'm 100% new in D, but I know C/C++. I've been reviewing the help and the
forum, and there's something that I still can't find: how can I read strings (or
an array of chars) from the keyboard. I know it sounds silly, but believe me, I
can't. Also, this code doesn't work:

import c.stdio;

void main()
{
float f;

scanf("%f",&f);
printf("You entered: %f\n",f);
}

It shows "You entered: 0.0000", doesn't matter what you enter. It works if f is
int or char, but not if float. Why? Is it that there're some easter-eggs in D?

Thanks
May 01 2002
parent reply "Walter" <walter digitalmars.com> writes:
"Carlos" <Carlos_member pathlink.com> wrote in message
news:aapkrj$24b0$1 digitaldaemon.com...
 Hi. I'm 100% new in D, but I know C/C++. I've been reviewing the help and

 forum, and there's something that I still can't find: how can I read

 an array of chars) from the keyboard.

At the moment, use the corresponding C functions.
 I know it sounds silly, but believe me, I
 can't. Also, this code doesn't work:

 import c.stdio;

 void main()
 {
 float f;

 scanf("%f",&f);
 printf("You entered: %f\n",f);
 }

 It shows "You entered: 0.0000", doesn't matter what you enter. It works if

 int or char, but not if float. Why? Is it that there're some easter-eggs

What's happening is that, unlike C, the f is passed to printf() as a float. It is interpreted by printf, however, as a double. You can fix it with: printf("You entered: %f\n", (double)f);
May 01 2002
parent reply Carlos <Carlos_member pathlink.com> writes:
How am I supposed to do the string reading? I've tried everything. This should
be the way, but doesn't work:

import c.stdio;

void main()
{
char[] str;
scanf("%s",str); //or should I use gets()? or &str?
printf("You wrote: %.*s\n",str);
}

---------

In article <aapnca$2958$1 digitaldaemon.com>, Walter says...
"Carlos" <Carlos_member pathlink.com> wrote in message
news:aapkrj$24b0$1 digitaldaemon.com...
 Hi. I'm 100% new in D, but I know C/C++. I've been reviewing the help and

 forum, and there's something that I still can't find: how can I read

 an array of chars) from the keyboard.

At the moment, use the corresponding C functions.
 I know it sounds silly, but believe me, I
 can't. Also, this code doesn't work:

 import c.stdio;

 void main()
 {
 float f;

 scanf("%f",&f);
 printf("You entered: %f\n",f);
 }

 It shows "You entered: 0.0000", doesn't matter what you enter. It works if

 int or char, but not if float. Why? Is it that there're some easter-eggs

What's happening is that, unlike C, the f is passed to printf() as a float. It is interpreted by printf, however, as a double. You can fix it with: printf("You entered: %f\n", (double)f);

May 01 2002
parent reply "Walter" <walter digitalmars.com> writes:
"Carlos" <Carlos_member pathlink.com> wrote in message
news:aaq2ep$15l$1 digitaldaemon.com...
 How am I supposed to do the string reading? I've tried everything. This

 be the way, but doesn't work:

 import c.stdio;

 void main()
 {
 char[] str;
 scanf("%s",str); //or should I use gets()? or &str?
 printf("You wrote: %.*s\n",str);
 }

The key is C doesn't do dynamic arrays, it does pointers. So: char[] str; char *s; scanf("%s", &s); str = s[0 .. c.strlen(s)];
May 01 2002
next sibling parent reply Russell Borogove <kaleja estarcion.com> writes:
Walter wrote:
 "Carlos" <Carlos_member pathlink.com> wrote in message
 news:aaq2ep$15l$1 digitaldaemon.com...
 
How am I supposed to do the string reading? I've tried everything. This

should
be the way, but doesn't work:

import c.stdio;

void main()
{
char[] str;
scanf("%s",str); //or should I use gets()? or &str?
printf("You wrote: %.*s\n",str);
}

The key is C doesn't do dynamic arrays, it does pointers. So: char[] str; char *s; scanf("%s", &s); str = s[0 .. c.strlen(s)];

Ack, that 'taint right, is it? s is uninitialized there, and the scanf is getting a pointer to pointer! (Although, I have a whole closet full of ten foot poles with which I wouldn't touch scanf() in the first place...) -R
May 02 2002
parent reply "Walter" <walter digitalmars.com> writes:
"Russell Borogove" <kaleja estarcion.com> wrote in message
news:3CD21E39.7020500 estarcion.com...
     char[] str;
     char *s;
     scanf("%s", &s);
     str = s[0 .. c.strlen(s)];

and the scanf is getting a pointer to pointer!

scanf initializes the pointer.
 (Although, I have a whole closet full of ten foot poles
 with which I wouldn't touch scanf() in the first place...)

I usually find myself writing a real lexer when I need one <g>.
May 03 2002
parent reply Russell Borogove <kaleja estarcion.com> writes:
Walter wrote:
 "Russell Borogove" <kaleja estarcion.com> wrote in message
 news:3CD21E39.7020500 estarcion.com...
 
    char[] str;
    char *s;
    scanf("%s", &s);
    str = s[0 .. c.strlen(s)];

Ack, that 'taint right, is it? s is uninitialized there, and the scanf is getting a pointer to pointer!

scanf initializes the pointer.

Not in standard C: # s Matches a sequence of non-white-space characters. # The corresponding argument shall be a pointer to # the initial character of an array large enough to # accept the sequence and a terminating null # character... (Unless that's a D scanf and I'm missing something?) I think you want: char s[ DO_YOU_FEEL_LUCKY_LENGTH ]; scanf( "%s", s ); -R
May 03 2002
parent reply "Walter" <walter digitalmars.com> writes:
Oops! You're right. Yet another reason to use D!

"Russell Borogove" <kaleja estarcion.com> wrote in message
news:3CD2BE38.4000802 estarcion.com...
 Walter wrote:
 "Russell Borogove" <kaleja estarcion.com> wrote in message
 news:3CD21E39.7020500 estarcion.com...

    char[] str;
    char *s;
    scanf("%s", &s);
    str = s[0 .. c.strlen(s)];

Ack, that 'taint right, is it? s is uninitialized there, and the scanf is getting a pointer to pointer!

scanf initializes the pointer.

Not in standard C: # s Matches a sequence of non-white-space characters. # The corresponding argument shall be a pointer to # the initial character of an array large enough to # accept the sequence and a terminating null # character... (Unless that's a D scanf and I'm missing something?) I think you want: char s[ DO_YOU_FEEL_LUCKY_LENGTH ]; scanf( "%s", s ); -R

May 03 2002
parent Russell Borogove <kaleja estarcion.com> writes:
Walter wrote:
 Oops! You're right. Yet another reason to use D!
 

lol... IMO, scanf() is a fine reason to use COBOL or BASIC! -R
May 04 2002
prev sibling parent reply "Carlos" <carlos8294 msn.com> writes:
 The key is C doesn't do dynamic arrays, it does pointers. So:

     char[] str;
     char *s;
     scanf("%s", &s);
     str = s[0 .. c.strlen(s)];

Like this? import c.stdio; import string; void main() { char[] w; char *w1; printf("Write your name: "); scanf("%s",&w); w=w1[0..strlen(w1)]; printf("Hi, %.*s\n",w); } Doesn't work!
May 06 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Carlos" <carlos8294 msn.com> wrote in message
news:ab7in0$k5l$1 digitaldaemon.com...

 Like this?

Like this: import c.stdio; import string; int main() { char[] w; char[100] w1; printf("Write your name: "); scanf("%s", w1); w = w1[0 .. strlen(w1)]; printf("Hi, %.*s\n", w); }
May 06 2002
parent reply "Carlos" <carlos8294 msn.com> writes:
 Like this:

 import c.stdio;
 import string;

 int main()
 {
     char[] w;
     char[100] w1;

     printf("Write your name: ");
     scanf("%s", w1);
     w = w1[0 .. strlen(w1)];
     printf("Hi, %.*s\n", w);
 }

two spaces in memory. Isn't this redudant, absurd or something like that?
May 06 2002
parent "Pavel Minayev" <evilone omen.ru> writes:
"Carlos" <carlos8294 msn.com> wrote in message
news:ab7j5i$kj8$1 digitaldaemon.com...

 So, basically, every single time you want to read a string, you must

 two spaces in memory. Isn't this redudant, absurd or something like that?

It is. It's because the D run-time library, Phobos, is far from completion, so you have to use C functions. Of course, later versions will introduce a better way to read user input.
May 06 2002