Jump to page: 1 2
Thread overview
I don't know much...
May 01, 2002
Carlos
May 01, 2002
Walter
May 02, 2002
Carlos
May 02, 2002
Walter
May 03, 2002
Russell Borogove
May 03, 2002
Walter
May 03, 2002
Russell Borogove
May 03, 2002
Walter
May 04, 2002
Russell Borogove
May 07, 2002
Carlos
May 07, 2002
Pavel Minayev
May 07, 2002
Carlos
May 07, 2002
Pavel Minayev
May 01, 2002
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
"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
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.

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
f is
> int or char, but not if float. Why? Is it that there're some easter-eggs
in D?

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 02, 2002
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
>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.
>
>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
>f is
>> int or char, but not if float. Why? Is it that there're some easter-eggs
>in D?
>
>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 02, 2002
"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)];


May 03, 2002
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 03, 2002
"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.

> (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
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
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 04, 2002
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 07, 2002
>
> 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!






« First   ‹ Prev
1 2