www.digitalmars.com         C & C++   DMDScript  

D - How to use scanf in D ?

reply Örk <Örk_member pathlink.com> writes:
How to use scanf in D ?

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

Örk
Jan 08 2004
parent reply J C Calvarese <jcc7 cox.net> writes:
Ö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/
Jan 08 2004
next sibling parent reply "C" <dont respond.com> writes:
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/
Jan 08 2004
next sibling parent kinghajj <kinghajj_member pathlink.com> writes:
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/
Jan 08 2004
prev sibling next sibling parent J C Calvarese <jcc7 cox.net> writes:
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/
Jan 08 2004
prev sibling parent reply "Robert" <no spam.ne.jp> writes:
"<>" 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/
Jan 09 2004
parent reply "C" <dont respond.com> writes:
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/
Jan 09 2004
next sibling parent J C Calvarese <jcc7 cox.net> writes:
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
Jan 09 2004
prev sibling next sibling parent reply Andy Friesen <andy ikagames.com> writes:
C wrote:
 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
Now there's something people coming from BASIC will run into. The D expression 'x <> y', when applied to integers, is equivalent to 'x == y'! (the exact opposite of what they think it means) -- andy
Jan 09 2004
parent Andy Friesen <andy ikagames.com> writes:
Andy Friesen wrote:
 Now there's something people coming from BASIC will run into.
 
 The D expression 'x <> y', when applied to integers, is equivalent to
 'x == y'! (the exact opposite of what they think it means)
 
  -- andy
errrr wait. No, I'm having a Monday. (on Friday!) -- andy
Jan 09 2004
prev sibling parent "Robert" <no spam.ne.jp> writes:
Hmm.  It seems "<>" is not supported in case of comparison of integers.
So, the integers may be implicitly casted to floating points and compared,
and it actually works when one or both operands are variables.
However, it causes the error when both operands are integer literals.

/* in "Expression *CmpExp::constFold()" in "constfold.c" */
480:    switch (op)
481:    {
482:        case TOKlt:     n = n1 <  n2;   break;
483:        case TOKle:     n = n1 <= n2;   break;
484:        case TOKgt:     n = n1 >  n2;   break;
485:        case TOKge:     n = n1 >= n2;   break;
486:        default:
487:            assert(0);  /* <- Here! */
488:    }

I guess that this function analyses a relational expression of two integer
literals.
But, it doesn't take into account "<>", "<>=", etc.



"C" <dont respond.com> wrote in message
news:btmpmj$171k$1 digitaldaemon.com...
 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/
Jan 09 2004
prev sibling parent reply Ian Johnston <Ian_member pathlink.com> writes:
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
Jan 09 2004
parent "Vathix" <vathix dprogramming.com> writes:
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);
Jan 09 2004