D - How to use scanf in D ?
- Örk (3/3) Jan 08 2004 How to use scanf in D ?
- J C Calvarese (38/41) Jan 08 2004 Here's some webpages that might be helpful:
- C (6/46) Jan 08 2004 if(cast(int) guess <> answer)
- kinghajj (2/64) Jan 08 2004
- J C Calvarese (8/70) Jan 08 2004 Good catch. It should be "!=" rather than "<>". I wonder if the
- Robert (4/66) Jan 09 2004 "<>" is a valid D operator,
- C (13/85) Jan 09 2004 Hmm, when I compile this
- J C Calvarese (7/18) Jan 09 2004 It seems to me that the Specification is unclear about this
- Andy Friesen (5/26) Jan 09 2004 Now there's something people coming from BASIC will run into.
- Andy Friesen (3/9) Jan 09 2004 errrr wait. No, I'm having a Monday. (on Friday!)
- Robert (19/106) Jan 09 2004 Hmm. It seems "<>" is not supported in case of comparison of integers.
- Ian Johnston (3/20) Jan 09 2004 What happens here if the users enters a string longer than 80 characters...
- Vathix (5/17) Jan 09 2004 It would write past the end, try this:
How to use scanf in D ? Or how to read integer and strings like in C with scanf ? Örk
Jan 08 2004
Örk wrote:How to use scanf in D ? Or how to read integer and strings like in C with scanf ? ÖrkHere'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
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 ? ÖrkHere'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
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 ? ÖrkHere'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
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...-- Justin http://jcc_7.tripod.com/d/Örk wrote:How to use scanf in D ? Or how to read integer and strings like in C with scanf ? ÖrkHere'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; }
Jan 08 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 ? ÖrkHere'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
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 ? ÖrkHere'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
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
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. ? CNow 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
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) -- andyerrrr wait. No, I'm having a Monday. (on Friday!) -- andy
Jan 09 2004
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 ? ÖrkHere'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
In article <btl20o$1id1$1 digitaldaemon.com>, J C Calvarese says...Örk wrote:What happens here if the users enters a string longer than 80 characters? IanHow to use scanf in D ? Or how to read integer and strings like in C with scanf ? ÖrkHere'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);
Jan 09 2004
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);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