Jump to page: 1 2
Thread overview
How to use scanf in D ?
Jan 08, 2004
Örk
Jan 09, 2004
J C Calvarese
Jan 09, 2004
C
Jan 09, 2004
kinghajj
Jan 09, 2004
J C Calvarese
Jan 09, 2004
Robert
Jan 09, 2004
C
Re: <> Operator (was How to use scanf in D ?)
Jan 09, 2004
J C Calvarese
Jan 09, 2004
Andy Friesen
Jan 09, 2004
Andy Friesen
Jan 09, 2004
Robert
Jan 09, 2004
Ian Johnston
Jan 09, 2004
Vathix
January 08, 2004
How to use scanf in D ?

Or how to read integer and strings like in C with scanf ?

Örk
January 09, 2004
Örk wrote:
> How to use scanf in D ?
> Or how to read integer and strings like in C with scanf ?
> Örk

Here's some webpages that might be helpful:
http://www.digitalmars.com/rtl/stdio.html#fscanf
http://www.cplusplus.com/ref/cstdio/scanf.html


Here's an example in D:

import std.c.stdio;

int main()
{
	const int answer = 10;
	float guess;
	int i;
	char[80] name;


	printf("Yo! What's your name? ");
	scanf("%s", &name);

	printf("Pick a whole number: ");
	scanf("%d", &i);
	printf("You picked %d as a whole number.\n\n", i);
	
	printf("Guess a number: ");
	scanf("%f", &guess);

	printf("\nHey, %.*s, ", name);

	if(cast(int) guess <> answer)

	if(answer == guess)
		printf("answer = %f.", guess);
	else if(answer > guess)
		printf("answer > guess.");
	else if(answer < guess)
	{
		printf("answer < guess.");
	}
	else
		printf("Dude, that wasn't a number.");
	printf("\n");

	return 0;
}



-- 
Justin
http://jcc_7.tripod.com/d/
January 09, 2004
if(cast(int) guess <> answer)

?

is it vb uses that as a != operator ?

C

"J C Calvarese" <jcc7@cox.net> wrote in message news:btl20o$1id1$1@digitaldaemon.com...
> Örk wrote:
> > How to use scanf in D ?
> > Or how to read integer and strings like in C with scanf ?
> > Örk
>
> Here's some webpages that might be helpful: http://www.digitalmars.com/rtl/stdio.html#fscanf http://www.cplusplus.com/ref/cstdio/scanf.html
>
>
> Here's an example in D:
>
> import std.c.stdio;
>
> int main()
> {
> const int answer = 10;
> float guess;
> int i;
> char[80] name;
>
>
> printf("Yo! What's your name? ");
> scanf("%s", &name);
>
> printf("Pick a whole number: ");
> scanf("%d", &i);
> printf("You picked %d as a whole number.\n\n", i);
>
> printf("Guess a number: ");
> scanf("%f", &guess);
>
> printf("\nHey, %.*s, ", name);
>
> if(cast(int) guess <> answer)
>
> if(answer == guess)
> printf("answer = %f.", guess);
> else if(answer > guess)
> printf("answer > guess.");
> else if(answer < guess)
> {
> printf("answer < guess.");
> }
> else
> printf("Dude, that wasn't a number.");
> printf("\n");
>
> return 0;
> }
>
>
>
> --
> Justin
> http://jcc_7.tripod.com/d/


January 09, 2004
SQL does :\

In article <btlb1d$1vtl$1@digitaldaemon.com>, C says...
>
>if(cast(int) guess <> answer)
>
>?
>
>is it vb uses that as a != operator ?
>
>C
>
>"J C Calvarese" <jcc7@cox.net> wrote in message news:btl20o$1id1$1@digitaldaemon.com...
>> Örk wrote:
>> > How to use scanf in D ?
>> > Or how to read integer and strings like in C with scanf ?
>> > Örk
>>
>> Here's some webpages that might be helpful: http://www.digitalmars.com/rtl/stdio.html#fscanf http://www.cplusplus.com/ref/cstdio/scanf.html
>>
>>
>> Here's an example in D:
>>
>> import std.c.stdio;
>>
>> int main()
>> {
>> const int answer = 10;
>> float guess;
>> int i;
>> char[80] name;
>>
>>
>> printf("Yo! What's your name? ");
>> scanf("%s", &name);
>>
>> printf("Pick a whole number: ");
>> scanf("%d", &i);
>> printf("You picked %d as a whole number.\n\n", i);
>>
>> printf("Guess a number: ");
>> scanf("%f", &guess);
>>
>> printf("\nHey, %.*s, ", name);
>>
>> if(cast(int) guess <> answer)
>>
>> if(answer == guess)
>> printf("answer = %f.", guess);
>> else if(answer > guess)
>> printf("answer > guess.");
>> else if(answer < guess)
>> {
>> printf("answer < guess.");
>> }
>> else
>> printf("Dude, that wasn't a number.");
>> printf("\n");
>>
>> return 0;
>> }
>>
>>
>>
>> --
>> Justin
>> http://jcc_7.tripod.com/d/
>
>


January 09, 2004
C wrote:
> if(cast(int) guess <> answer)
Good catch.  It should be "!=" rather than "<>". I wonder if the compiler should flag that as an error.

> 
> ?
> 
> is it vb uses that as a != operator ?
Yes, I often speak BASIC.  (I used to make all kinds of weird mistakes when I was programming in Pascal and QBasic at the same time.)

> 
> C
> 
> "J C Calvarese" <jcc7@cox.net> wrote in message
> news:btl20o$1id1$1@digitaldaemon.com...
> 
>>Örk wrote:
>>
>>>How to use scanf in D ?
>>>Or how to read integer and strings like in C with scanf ?
>>>Örk
>>
>>Here's some webpages that might be helpful:
>>http://www.digitalmars.com/rtl/stdio.html#fscanf
>>http://www.cplusplus.com/ref/cstdio/scanf.html
>>
>>
>>Here's an example in D:
>>
>>import std.c.stdio;
>>
>>int main()
>>{
>>const int answer = 10;
>>float guess;
>>int i;
>>char[80] name;
>>
>>
>>printf("Yo! What's your name? ");
>>scanf("%s", &name);
>>
>>printf("Pick a whole number: ");
>>scanf("%d", &i);
>>printf("You picked %d as a whole number.\n\n", i);
>>
>>printf("Guess a number: ");
>>scanf("%f", &guess);
>>
>>printf("\nHey, %.*s, ", name);
>>
>>if(cast(int) guess <> answer)
>>
>>if(answer == guess)
>>printf("answer = %f.", guess);
>>else if(answer > guess)
>>printf("answer > guess.");
>>else if(answer < guess)
>>{
>>printf("answer < guess.");
>>}
>>else
>>printf("Dude, that wasn't a number.");
>>printf("\n");
>>
>>return 0;
>>}

-- 
Justin
http://jcc_7.tripod.com/d/
January 09, 2004
"<>" is a valid D operator,
equivalent to "!=" unless one or both of operands is a NaN.

"C" <dont@respond.com> wrote in message news:btlb1d$1vtl$1@digitaldaemon.com...
> if(cast(int) guess <> answer)
>
> ?
>
> is it vb uses that as a != operator ?
>
> C
>
> "J C Calvarese" <jcc7@cox.net> wrote in message news:btl20o$1id1$1@digitaldaemon.com...
> > Örk wrote:
> > > How to use scanf in D ?
> > > Or how to read integer and strings like in C with scanf ?
> > > Örk
> >
> > Here's some webpages that might be helpful: http://www.digitalmars.com/rtl/stdio.html#fscanf http://www.cplusplus.com/ref/cstdio/scanf.html
> >
> >
> > Here's an example in D:
> >
> > import std.c.stdio;
> >
> > int main()
> > {
> > const int answer = 10;
> > float guess;
> > int i;
> > char[80] name;
> >
> >
> > printf("Yo! What's your name? ");
> > scanf("%s", &name);
> >
> > printf("Pick a whole number: ");
> > scanf("%d", &i);
> > printf("You picked %d as a whole number.\n\n", i);
> >
> > printf("Guess a number: ");
> > scanf("%f", &guess);
> >
> > printf("\nHey, %.*s, ", name);
> >
> > if(cast(int) guess <> answer)
> >
> > if(answer == guess)
> > printf("answer = %f.", guess);
> > else if(answer > guess)
> > printf("answer > guess.");
> > else if(answer < guess)
> > {
> > printf("answer < guess.");
> > }
> > else
> > printf("Dude, that wasn't a number.");
> > printf("\n");
> >
> > return 0;
> > }
> >
> >
> >
> > --
> > Justin
> > http://jcc_7.tripod.com/d/
>
>

January 09, 2004
In article <btl20o$1id1$1@digitaldaemon.com>, J C Calvarese says...
>
>Örk wrote:
>> How to use scanf in D ?
>> Or how to read integer and strings like in C with scanf ?
>> Örk
>
>Here's some webpages that might be helpful: http://www.digitalmars.com/rtl/stdio.html#fscanf http://www.cplusplus.com/ref/cstdio/scanf.html
>
>
>Here's an example in D:
>
>import std.c.stdio;
>
>int main()
>{
>	const int answer = 10;
>	float guess;
>	int i;
>	char[80] name;
>
>
>	printf("Yo! What's your name? ");
>	scanf("%s", &name);

What happens here if the users enters a string longer than 80 characters?

Ian


January 09, 2004
> >int main()
> >{
> > const int answer = 10;
> > float guess;
> > int i;
> > char[80] name;
> >
> >
> > printf("Yo! What's your name? ");
> > scanf("%s", &name);
>
> What happens here if the users enters a string longer than 80 characters?
>
> Ian
>

It would write past the end, try this:
char[80] name;
printf("Yo! What's your name? ");
scanf("%79s", cast(char*)name);
printf("Your name is %s\n", cast(char*)name);




January 09, 2004
Hmm, when I compile this

import std.c.stdio;

void main () {

 if ( 0 <> 1 ) puts("true");

}

it fails with :

Assertion failure: '0' on line 487 in file 'constfold.c'

abnormal program termination

however != works fine.

?

C

"Robert" <no@spam.ne.jp> wrote in message news:btlsfh$2roq$1@digitaldaemon.com...
> "<>" is a valid D operator,
> equivalent to "!=" unless one or both of operands is a NaN.
>
> "C" <dont@respond.com> wrote in message news:btlb1d$1vtl$1@digitaldaemon.com...
> > if(cast(int) guess <> answer)
> >
> > ?
> >
> > is it vb uses that as a != operator ?
> >
> > C
> >
> > "J C Calvarese" <jcc7@cox.net> wrote in message news:btl20o$1id1$1@digitaldaemon.com...
> > > Örk wrote:
> > > > How to use scanf in D ?
> > > > Or how to read integer and strings like in C with scanf ?
> > > > Örk
> > >
> > > Here's some webpages that might be helpful: http://www.digitalmars.com/rtl/stdio.html#fscanf http://www.cplusplus.com/ref/cstdio/scanf.html
> > >
> > >
> > > Here's an example in D:
> > >
> > > import std.c.stdio;
> > >
> > > int main()
> > > {
> > > const int answer = 10;
> > > float guess;
> > > int i;
> > > char[80] name;
> > >
> > >
> > > printf("Yo! What's your name? ");
> > > scanf("%s", &name);
> > >
> > > printf("Pick a whole number: ");
> > > scanf("%d", &i);
> > > printf("You picked %d as a whole number.\n\n", i);
> > >
> > > printf("Guess a number: ");
> > > scanf("%f", &guess);
> > >
> > > printf("\nHey, %.*s, ", name);
> > >
> > > if(cast(int) guess <> answer)
> > >
> > > if(answer == guess)
> > > printf("answer = %f.", guess);
> > > else if(answer > guess)
> > > printf("answer > guess.");
> > > else if(answer < guess)
> > > {
> > > printf("answer < guess.");
> > > }
> > > else
> > > printf("Dude, that wasn't a number.");
> > > printf("\n");
> > >
> > > return 0;
> > > }
> > >
> > >
> > >
> > > --
> > > Justin
> > > http://jcc_7.tripod.com/d/
> >
> >
>


January 09, 2004
It seems to me that the Specification is unclear about this (http://www.digitalmars.com/d/expression.html), but <> seemed to be intended to be used with floating-point numbers (rather than integers such as 0 and 1).

That error message seems very vague.  (Might be a candidate for a bug report based on the error message if nothing else.)

Justin


In article <btmpmj$171k$1@digitaldaemon.com>, C says...
>
>Hmm, when I compile this
>
>import std.c.stdio;
>void main () {
> if ( 0 <> 1 ) puts("true");
>}
>
>it fails with :
>
>Assertion failure: '0' on line 487 in file 'constfold.c'
>abnormal program termination
>however != works fine.
>?
>C


« First   ‹ Prev
1 2