Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 08, 2004 How to use scanf in D ? | ||||
---|---|---|---|---|
| ||||
How to use scanf in D ? Or how to read integer and strings like in C with scanf ? Örk |
January 09, 2004 Re: How to use scanf in D ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Örk | Ö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 Re: How to use scanf in D ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to J C Calvarese | 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 Re: How to use scanf in D ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to C | 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 Re: How to use scanf in D ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to C | 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 Re: How to use scanf in D ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to C | "<>" 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 Re: How to use scanf in D ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to J C Calvarese | 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 Re: How to use scanf in D ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ian Johnston | > >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 Re: How to use scanf in D ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert | 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 Re: <> Operator (was How to use scanf in D ?) | ||||
---|---|---|---|---|
| ||||
Posted in reply to C | 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 |
Copyright © 1999-2021 by the D Language Foundation